[Reproduced] Drawing function in OpenCV of OpenCV-Python series (7)

We often need to draw a certain local feature in an image, such as object detection, object tracking, etc. Let's take a look at what fun drawing tools are there today!

Draw a line

First of all, to create an environment for the drawn lines, an empty black background image must be generated. We use numpy to experiment:

	view plaincopy to clipboardprint?
 1. import cv2   
 2. import numpy as np      
 3. img=np.zeros((512,512,3),np.uint8)   
 4. cv2.imshow("img",img)   
 5. cv2.waitKey(0)  
 6. cv2.destroyAllWindows()

image.png

This is the black background, our canvas, I called the window name img, np.zeros() has two parameters, one is the size of the created image matrix, the other is the data type, 512, 512 are pixels (the first 512 pixels High, the second is 512 pixels wide), 3 refers to three colors of BGR

uint8 uses 0-255 to represent all colors.

Let's look at the prototype of the line drawing function:

cv2.line(img,start,end,color,thickness)

img is the picture to be operated, start and end are the two ends of the line, color is the color of the line, and thickness is the thickness of the line.

Function example:

	view plaincopy to clipboardprint?
1.cv2.line(img,(0,0),(511,511),(255,0,0),5)  

This function has 5 parameters, namely: img is the image name, (0, 0) is the starting point coordinates, (511, 511) is the end point coordinates, (255, 0, 0) is blue, and 5 is the width of the line

Will draw a blue line from the upper left to the lower right corner, we see the experimental effect:

image.png

Draw rectangle

Drawing the rectangle function will be a very important link. In the actual operation projects in the future, we will basically find that the most used drawing function in OpenCV is the rectangle function. It has a wide range of applications, including drawing outlines and tracking targets in real time. At that time, we also use the rectangle function to mark the tracked target. Next we first understand the rectangular function:

cv2.rectangle(img,(50,50),(400,400),(0,0,255),3)

This function has 5 parameters, img is the image name, (50, 50) is the coordinates of the upper left vertex, (400, 400) is the coordinates of the lower right vertex, (0,255,0) is green, and the line width is 3.

Draw two elements of the rectangle: the coordinates of the upper left vertex and the lower right vertex.

Let's take a look at the code:

	view plaincopy to clipboardprint?
1.import cv2  
2.import numpy as np  
3.
4.img = np.zeros((512, 512, 3), np.uint8)  
5.cv2.rectangle(img,(50,50),(400,400),(0,0,255),3)  
6.
7.cv2.imshow("img", img)  
8.cv2.waitKey(0)  
9.cv2.destroyAllWindows()  

Experimental effect:

Insert picture description here

Draw a circle

function:

cv2.circle(img,(447,63), 63, (0,0,255), -1)

This function has 5 parameters: image name, center coordinates, radius 63, (0,0,255) red, line width is -1, when the line width is -1, it means the color filling of the closed figure.

Two elements of drawing a circle: specify the center point coordinates and radius of the circle

Code:

	view plaincopy to clipboardprint?
1.import cv2  
2.import numpy as np  
3.
4.img=np.zeros((512,512,3), np.uint8)  
5.cv2.circle(img,(256,256), 63, (0,0,255), -1)  
6.cv2.imshow("img",img)  
7.cv2.waitKey(0)  
8.cv2.destroyAllWindows() 

Demonstration effect:

image.png

Draw oval

Function prototype:

cv2.ellipse(img,center,axes,angle,start_angle,end_angle,color,thickness,line_type,shift)

img is the image of the operation, center is the center of the ellipse, axes is the major axis and minor axis of the ellipse, angle is the deflection angle, start_angle and end_angle are the starting and ending angles of the arc, color is the line color, and thickness is The thickness of the line, line_type is the type of line, shift is the accuracy of the center coordinate point and the number axis.

Give a function example:

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

This function has 8 parameters: img is the image name, (256, 256) is the center point coordinates, 100 is the length of the long axis, 50 is the length of the short axis, 0 rotation angle, the part of the image (the long axis starts in the clockwise direction) The angle and end angle) 0,180 is the lower half of the ellipse, the color array here is 255 is blue. -1 is solid, values ​​greater than 0 are hollow, and the line width is determined by yourself.

Several elements of drawing an ellipse: 1. It is the position coordinate of the center point 2. The length of the long axis and the short axis 3. The angle of the ellipse rotating in the counterclockwise direction 4. The starting and ending angle of the long axis in the clockwise direction

Code:

	view plaincopy to clipboardprint?
1.import cv2  
2.import numpy as np  
3.
4.img=np.zeros((512,512,3), np.uint8)  
5.cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)  
6.cv2.imshow("img",img)  
7.cv2.waitKey(0)  
8.cv2.destroyAllWindows()  

Demonstration effect:

image.png

Now we draw the entire ellipse and modify the third parameter from the bottom of the code from 180 to 360:

cv2.ellipse(img,(256,256),(100,50),0,0,360,255,-1)

image.png

If you want to draw a hollow circle, you only need to modify the last parameter to a positive value, which is the line width:

	view plaincopy to clipboardprint?
1.cv2.ellipse(img,(256,256),(100,50),0,0,360,255,4)  

effect:

image.png

Draw polygon

OpenCV can draw polygons through API. The function examples are:

pts=np.array([[20,20],[350,40],[420,400],[60,420]], np.int32)

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

img = cv2.polylines(img,[pts],True,(0,255,0),2)

cv2. polylines() has 5 parameters: image name, vertex list (this polygon has four vertices in the array), True means closed, (0,255,255) is yellow, and 3 is the line width. pts is an array of four vertices constructed using numpy. The data type of this array must be int32.

Code:

	view plaincopy to clipboardprint?
1.import cv2  
2.import numpy as np  
3.  
4.img = np.zeros((512, 512, 3), np.uint8)  
5.pts=np.array([[20,20],[350,40],[420,400],[60,420]], np.int32)  
6.pts=pts.reshape((-1,1,2))  
7.cv2.polylines(img,[pts],True,(0,255,0),2)  
8.  
9.cv2.imshow("img", img)  
10.cv2.waitKey(0)  
11.cv2.destroyAllWindows()  

Effect demonstration:

image.png

If I modify the order of the vertex array, I will get different results:

	view plaincopy to clipboardprint?
1.pts=np.array([[20,20],[350,40],[60,420],[420,400]], np.int32)  

effect:

image.png

Add text to the picture

Using OpenCV we can add text on the image, the function example is:

font=cv2.FONT_HERSHEY_SIMPLEX

cv2.putText(img,‘OpenCV’,(10,250), font, 4,(255,255,255),2)

This function has eight parameters, namely: image name, string, coordinates, font, font size, (255,255,255) is white, and line width is 2.

Of course, OpenCV does not support adding Chinese. If you want to add Chinese characters, you can add Chinese fonts, or you can use PIL to operate. The official explanation for not supporting Chinese characters is to keep the library simple and lightweight, and that it will not support Chinese in the future, so everyone should try to use English, after all, we will need to use English to read a lot of documents in the future.

Code:

	view plaincopy to clipboardprint?
1.import cv2  
2.import numpy as np  
3.  
4.img=np.zeros((512,512,3), np.uint8)  
5.font=cv2.FONT_HERSHEY_SIMPLEX  
6.cv2.putText(img,'OpenCV',(10,250), font, 4,(255,255,255),2)  
7.  
8.cv2.imshow("img",img)  
9.cv2.waitKey(0)  
10.cv2.destroyAllWindows() 

The effect is demonstrated as;

image.png

At this point, OpenCV's drawing functions are basically that, and you need to pay special attention to:

The return value of all drawing functions is None, so you cannot use something like: img =cv2.line(img,(0,0),(511,511),(255,0,0),5), the drawing function is called directly can.

View the article summary page https://blog.csdn.net/weixin_44237705/article/details/107864965

More openvino technical information can be exchanged in the group~
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44237705/article/details/107952927