Lab 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

VIETNAM NATIONAL UNIVERSITY – HOCHIMINH CITY

INTERNATIONAL UNIVERSITY
SCHOOL OF BIOMEDICAL ENGINEERING

BIOMEDICAL IMAGE PROCESSING

Section 1:

Basic image processing

Ho Chi Minh City


Semester 2, 2023 - 2024
International University School of Biomedical Engineering

Table of Contents

1 Drawing.......................................................................................................................................... 1
1.1 Theory......................................................................................................................................................... 1
1.2 Practice..................................................................................................................................................... 10

2 Reading image and basic processing.................................................................................. 11


2.1 Theory....................................................................................................................................................... 11
2.2 Practice..................................................................................................................................................... 14

Biomedical Image Processing BM058IU


International University School of Biomedical Engineering

LAB 1: Basis image processing


1 Drawing

1.1 Theory

1.1.1 Draw over image

The essential operation of OpenCV is to draw over images. It also allows you to add
different geometric shapes, such as lines, circles, rectangles, etc.

Often, when working with image analysis, we want to highlight a portion of the image,
for example, by adding a rectangle that defines that portion or an arrow to indicate
something.

cv2.line() − This function draws lines on an image.

cv2.rectangle() − This function draws a rectangle on an image.

cv2.circle() − This function draws a circle on an image.

cv2.putText() − This function writes text on the image.

cv2.ellipse() − This function is used to draw an ellipse on an image

import numpy as np

import cv2

my_img = np.zeros((350, 350, 3), dtype = "uint8")

cv2.imshow('Window', my_img)

cv2.waitKey(0)

cv2.destroyAllWindows()

Figure 1. Example code for creating a window for image presenting

Biomedical Image Processing Laboratory 1 BM058IU


International University School of Biomedical Engineering

Figure 2. Window created by the above code


1.1.2 Line drawing

For drawing a line cv2.line() function is used. This function takes five arguments

 Image object on which to draw


 Starting point coordinates (x, y)
 End point coordinates (x, y)
 Stroke color in BGR (not RGB, to be noted)
 Stroke thickness (in pixels)

import numpy as np

import cv2

my_img = np.zeros((350, 350, 3), dtype = "uint8")

# creating for line

cv2.line(my_img, (202, 220), (100, 160), (0, 20, 200), 10)

Biomedical Image Processing Laboratory 2 BM058IU


International University School of Biomedical Engineering

cv2.imshow('Window', my_img)

cv2.waitKey(0)

cv2.destroyAllWindows()

1.1.3 Rectangle drawing

For drawing a rectangle cv2.rectangle() function is used. This function accepts five
input parameters.

 Image object on which to draw


 Coordinates of the vertex at the top left (x, y)
 Coordinates of the lower right vertex (x, y)
 Stroke color in BGR (not RGB, to be noted)
 Stroke thickness (in pixels)

import numpy as np

import cv2

Biomedical Image Processing Laboratory 3 BM058IU


International University School of Biomedical Engineering

my_img = np.zeros((400, 400, 3), dtype = "uint8")

# creating a rectangle

cv2.rectangle(my_img, (30, 30), (300, 200), (0, 20, 200), 10)

cv2.imshow('Window', my_img)

# allows us to see image

# until closed forcefully

cv2.waitKey(0)

cv2.destroyAllWindows()

1.1.4 Circle drawing

For drawing a circle, cv2.circle() function is used. This function accepts five input
parameters.
Biomedical Image Processing Laboratory 4 BM058IU
International University School of Biomedical Engineering

 Image object on which to draw


 Center coordinates (x, y)
 Radius of the circle
 Stroke color in BGR (not RGB, to be noted)
 Stroke thickness (in pixels)

import numpy as np

import cv2

my_img = np.zeros((400, 400, 3), dtype = "uint8")

# creating circle

cv2.circle(my_img, (200, 200), 80, (0, 20, 200), 10)

cv2.imshow('Window', my_img)

cv2.waitKey(0)

cv2.destroyAllWindows()

Biomedical Image Processing Laboratory 5 BM058IU


International University School of Biomedical Engineering

1.1.5 Ellipse drawing

For drawing an ellipse, the cv2.ellipse() function is used. This function accepts eight
input parameters.

 Image object on which image to be drawn


 Center coordinates (x, y)
 Length of the minor and major axes (h, w)
 Rotation angle of the ellipse (calculated counterclockwise)
 Starting angle (calculated clockwise)
 Final angle (calculated clockwise)
 Stroke color in BGR (not RGB to be noted)
 Stroke thickness

import numpy as np

Biomedical Image Processing Laboratory 6 BM058IU


International University School of Biomedical Engineering

import cv2

my_img = np.zeros((400, 400, 3), dtype = "uint8")

# creating for rectangle

cv2.ellipse(my_img,(256,256),(100,50),0,0,180,255,-1)

cv2.imshow('Window', my_img)

# allows us to see image

# until closed forcefully

cv2.waitKey(0)

cv2.destroyAllWindows()

Biomedical Image Processing Laboratory 7 BM058IU


International University School of Biomedical Engineering

1.1.6 Polygon drawing

For drawing a polygon, the cv2.polylines() function is used. This function needs five
arguments.

 The image object on which to draw


 The array of coordinates
 True, if it is a closed line
 Stroke color
 Stroke thickness

import numpy as np

import cv2

my_img = np.zeros((400, 400, 3), dtype = "uint8")

pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)

pts = pts.reshape((-1,1,2))

cv2.polylines(my_img,[pts],True,(0,255,255))

cv2.imshow('Window', my_img)

cv2.waitKey(0)

cv2.destroyAllWindows()

Biomedical Image Processing Laboratory 8 BM058IU


International University School of Biomedical Engineering

1.1.7 Text drawing

To draw the text

A cv2.putText() function accepts several arguments for writing text with OpenCV.

 The image on which to draw


 The text to be written
 Coordinates of the text start point
 Font to be used
 Font size
 Text color
 The type of the line used

import numpy as np

import cv2

Biomedical Image Processing Laboratory 9 BM058IU


International University School of Biomedical Engineering

my_img = np.zeros((400, 400, 3), dtype = "uint8")

# Writing text

font = cv2.FONT_HERSHEY_SIMPLEX

cv2.putText(my_img, 'Tutorials Point,' (50, 50), font, 0.8, (255, 0, 0), 2, cv2.LINE_AA)

cv2.imshow('Window', my_img)

cv2.waitKey(0)

cv2.destroyAllWindows()

Figure 3. Output of the above code


1.2 Practice

Write a program:

Biomedical Image Processing Laboratory 10 BM058IU


International University School of Biomedical Engineering

 Construct a NumPy array using the np. The zeros method with 300 rows and 300
columns yields a 300 × 300 pixel image.
 Allocate space for three channels – one for Red, Green, and Blue, respectively.
 Draw a green line from point (0, 0) (the top-left corner of the image) to point (300,
300)
 Draw a red line from the top-right corner of the image to the bottom left with a
thickness of 3 pixels.
 Draw a green rectangle starting from point (10, 10) and ending at (60, 60)
 Draw a red rectangle that is 5 pixels thick, starting from point (50, 200) and ending
at (200, 225)
 Draw a blue rectangle, starting from (200, 50) and ending at (225, 125) –
specifying -1 as the thickness.
 For each requirement, show your image and wait for a keypress.

2 Reading image and basic processing

2.1 Theory

2.1.1 Read an image

We use cv2.imread() function to read an image. The image should be placed in the
current working directory, or else we must provide the absolute path.

import numpy as np

import cv2

# Load an color image in grayscale

img = cv2.imread('Top-bike-wallpaper.jpg',0)

2.1.2 Display an image

To display an image in a window, use cv2.imshow() function.

#Display the image

Biomedical Image Processing Laboratory 11 BM058IU


International University School of Biomedical Engineering

cv2.imshow('image',img)

#key binding function

cv2.waitKey(0)

#Destroyed all window we created earlier.

cv2.destroyAllWindows()

On running above code, a screenshot of the window will look like this,

2.1.3 Write an image

Use the function cv2.imwrite() to save an image.

The first argument is the file name, and the second argument is the image you want to
save.

cv2.imwrite('messigray.png',img)

Sum it up

import numpy as np

import cv2

Biomedical Image Processing Laboratory 12 BM058IU


International University School of Biomedical Engineering

#Read the Image

# Load an color image in grayscale

img = cv2.imread('Top-bike-wallpaper.jpg',0)

#Display the image

cv2.imshow('image',img)

#key binding function

k = cv2.waitKey(0)

# wait for ESC key to exit

if k == 27:

cv2.destroyAllWindows()

# wait for 's' key to save and exit

elif k == ord('s'):

cv2.imwrite('myBike.jpg',img)

cv2.destroyAllWindows()

Save the image by pressing the 's' or the 'ESC' key to exit without saving.

2.1.4 Image Rotation

Syntax: cv2.cv.rotate( src, rotateCode[, dst] )

Parameters:

 src: It is the image whose color space is to be changed.


 rotateCode: It is an enum to specify how to rotate the array.
 dst: It is the output image of the same size and depth as src image. It is an optional
parameter.
 Return Value: It returns an image.

2.1.5 Image resizing

Biomedical Image Processing Laboratory 13 BM058IU


International University School of Biomedical Engineering

Syntax: cv2.resize(source, dsize, dest, fx, fy, interpolation)

Parameters:

 source: Input Image array (Single-channel, 8-bit or floating-point)


 dsize: Size of the output array
 dest: Output array (Similar to the dimensions and type of Input image array)
[optional]
 fx: Scale factor along the horizontal axis [optional]
 fy: Scale factor along the vertical axis [optional]
 interpolation: One of the above interpolation methods [optional]

2.1.6 Image flipping

Syntax: cv2.cv.flip(src, flipCode[, dst] )

Parameters:

 src: Input array.


 dst: Output array of the same size and type as src.
 flip code: A flag to specify how to flip the array; 0 means flipping around the x-
axis, and a positive value (for example, 1) means flipping around the y-axis. A
negative value (for example, -1) means flipping around both axes. Return Value: It
returns an image.

2.2 Practice

Write a program:

 Read your favorite image.


 Rotate your image with 45, 90, and 180-degree angles.
 Resize your image with double length/ double width/ half width and length.
 Flipping your image horizontally, vertically, and both horizontally – vertically

Biomedical Image Processing Laboratory 14 BM058IU

You might also like