Lab 1
Lab 1
INTERNATIONAL UNIVERSITY
SCHOOL OF BIOMEDICAL ENGINEERING
Section 1:
Table of Contents
1 Drawing.......................................................................................................................................... 1
1.1 Theory......................................................................................................................................................... 1
1.2 Practice..................................................................................................................................................... 10
1.1 Theory
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.
import numpy as np
import cv2
cv2.imshow('Window', my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
For drawing a line cv2.line() function is used. This function takes five arguments
import numpy as np
import cv2
cv2.imshow('Window', my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
For drawing a rectangle cv2.rectangle() function is used. This function accepts five
input parameters.
import numpy as np
import cv2
# creating a rectangle
cv2.imshow('Window', my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
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
import numpy as np
import cv2
# creating circle
cv2.imshow('Window', my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
For drawing an ellipse, the cv2.ellipse() function is used. This function accepts eight
input parameters.
import numpy as np
import cv2
cv2.ellipse(my_img,(256,256),(100,50),0,0,180,255,-1)
cv2.imshow('Window', my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
For drawing a polygon, the cv2.polylines() function is used. This function needs five
arguments.
import numpy as np
import cv2
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()
A cv2.putText() function accepts several arguments for writing text with OpenCV.
import numpy as np
import cv2
# 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()
Write a program:
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.1 Theory
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
img = cv2.imread('Top-bike-wallpaper.jpg',0)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
On running above code, a screenshot of the window will look like this,
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
img = cv2.imread('Top-bike-wallpaper.jpg',0)
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
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.
Parameters:
Parameters:
Parameters:
2.2 Practice
Write a program: