Here, we will cover some functions that draw shapes or add text to an image, which are particularly useful for making it so humans can see what a computer is trying to analyze.
NOTE: Before starting this module, please import the numpy library.
cv2.rectangle has 4 different arguments aside from its image matrix argument. Argument 2 and 3 are the upper left corner and bottom right corner coordinates of the rectangle respectively, while argument 4 is the color of the rectangle (don't forget this is BGR format.). Argument 5 is the thickness of said rectangle, but if set to -1, it will actually fill the rectangle.
1st Argument-Image Matrix
2nd Argument-Upper Left Coordinate
3rd Argument-Bottom Right Coordinate
4th Argument-Color
5th Argument-Line Thickness
Standard Rectangle
Filled Rectangle
cv2.circle has 4 arguments as well in addition to the image matrix argument. Argument 2 is the center of the circle while argument 3 is its radius. Argument 4 is the color, and argument 5 is the thickness. For arguments 4 and 5, the same rules apply here from the rectangle.
1st Argument-Image Matrix
2nd Argument-Center Coordinate
3rd Argument-Radius
4th Argument-Color
5th Argument-Line Thickness
cv2.line is the last of the drawing functions. Arguments 2 and 3 are the start and end coordinates of the line, whereas arguments 4 and 5 are the color and thickness respectively. However, you cannot use -1 as a thickness here, as it will error the program.
1st Argument-Image Matrix
2nd Argument-Start Coordinate
3rd Argument-End Coordinate
4th Argument- Color
5th Argument-Line Thickness
Lastly, cv2.putText will place text on an image, that can be used to clarify something. Argument 2 is the text you want to place in an image, whereas argument 3 is the coordinate of the text. The constant on the 4th argument is the font style. You can use the font cv.FONT_HERSHEY_TRIPLEX as a font. Argument 5 is the scale factor, and argument 6 and 7 are color and thickness respectively.
1st Argument-Image Matrix
2nd Argument-Text String
3rd Argument-Text Coordinate
4th Argument-Font Style
5th Argument-Scale Factor
6th Argument- Color
7th Argument Thickness
Remember, OpenCV stores all of its images as matrices, so cropping is not a library exclusive feature. Simply crop the matrix from one x coordinate to another and one y coordinate to another.
i.e. img=img[200:300,400:500]
where 200:300 represents only pixels on this x axis range to be displayed.
where 400:500 represents only pixels on this y axis range to be displayed.
.shape, when attached to an image matrix will yield the width and height of the image.
i.e img.shape[0] will yield the image's width and img.shape[1] will yield the image's height
NOTE: .shape is actually a numpy function, which is why you need to import the libarary.
Original image
Cropped image
You can also use some of these draw functions in more unorthodox ways, such as setting the radius of a circle to a radius of one pixel so that it can indicate a single pixel.
Like the last module, these windows will open and close one at a time so make sure to press a key to close it. And yes, you can use these on regular images and not just blank numpy matrices.
blank = np.zeros((500,500,3),dtype='uint8')
cv.imshow('Blank',blank)
cv.waitKey(0)
blank[200:300,300:400]=[0,0,255]
cv.imshow('Green',blank)
cv.waitKey(0)
##for the following functions, you may remove the first argument with your image matrix (img) from the previous modules.
cv.rectangle(blank, (0,0), (250,250), (0,255,0), thickness=2)
cv.imshow('rectangle',blank)
cv.waitKey(0)
cv.rectangle(blank,(0,0),(250,250),(0,255,0),thickness=cv.FILLED)
cv.imshow('rect',blank)
cv.waitKey(0)
cv.circle(blank,(250,250),40,(0,0,255),thickness=3)
cv.imshow('circ', blank)
cv.waitKey(0)
cv.line(blank, (0,0), (250,250), (255,255,255), thickness=3)
cv.imshow('line',blank)
cv.waitKey(0)
cv.putText(blank,'Hello',(img.shape[1]//2,img.shape[0]//2),cv.FONT_HERSHEY_TRIPLEX,1.0,(255,255,255),thickness=2)
cv.imshow('text', blank)
cv.waitKey(0)
Legend:
img=your specified image matrix
x= an integer value
y= an integer value
z=an integer value
'text'=the text you want to type in
cv2.CONSTANT= a value that tells OpenCV what to do specifically with the said function. Think of it as a setting if you will.
Functions:
cv2.rectangle(img,(x,y),(x,y),(x,y,z),thickness=x)
cv2.circle(img,(x,y),x,(x,y,z),thickness=x)
cv2.line(img,(x,y),(x,y),(x,y,z),thickness=x)
cv2.putText(img,'text',(x,y),cv.CONSTANT, z,(x,y,z),thickness=x)
width, height=img.shape