python OpenCV drawing function

OpenCV can be used to draw different collection graphics, including lines, rectangles, circles, ellipses, polygons and adding text to images. The drawing functions used include CV2 line(),,cv2.rectangle() ,cv2.putText() and so on.

These drawing functions need to set parameters, such as:

• img: the image you want to draw.
• color: the color of the shape. For example: (0,255) a blue tuple is required, for example: (0,255). For grayscale images, you only need to pass in grayscale values.
• thickness: the thickness of the line. If a closed figure is set to - 1, the figure will be filled. The default value is 1
• linetype: type of line, 8 connection, anti aliasing, etc. The default is 8 connections. cv2.LINE_AA is anti aliasing, so it will look very smooth.

1. Draw lines
To draw a line, you just need to tell the function the start and end of the line. We will draw a blue line from the top left to the bottom right.

import numpy as np
import cv2

img = np.zeros((512,512,3), np.uint8)   # Create a black background
# np. The zeros () function returns an array filled with 0 for a given shape and type
# np. Zeros ((512512,3) constructs a 512 * 512 Numpy array and allocates three color spaces at the same time

cv2.line(img,(0,0),(511,511),(0,255,0),5)  # Specify two endpoints and draw a green line with 5 pixels

2. Draw a rectangle

To draw a rectangle, you need to tell the coordinates of the top left and bottom right vertices of the function. This time we will draw a blue rectangle in the upper right corner of the image.


3. Draw a circle
To draw a circle, you only need to specify the coordinates of the center point of the circle and the size of the radius. We draw a circle in the rectangle above.,(447,63), 63, (0,0,255), -1) #-1 indicates the fill color

4. Draw an ellipse
Drawing an ellipse is complicated. We need to input more parameters. One parameter is the position coordinate of the center point.
The next parameter is the length of the major and minor axes. The angle at which an ellipse rotates counterclockwise. The starting angle and ending angle of the elliptical arc in a clockwise direction. If it is 0 to 360, it is the whole ellipse. The following example is to draw an ellipse in the center of the picture.


Draw half an ellipse


5. Draw polygon
To draw a polygon, you need to point out the coordinates of each vertex. Use the coordinates of these points to build an array whose size is equal to the number of rows X1X2. The number of rows is the number of points. The data type of this array must be int32. Here, draw a white polygon with four vertices.

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

5. Add text to the picture

To draw text on a picture, you need to set the following parameters:
• the text you want to draw
• where you want to draw
• font type (find supported fonts by looking at the documentation of cv2.putText())
• font size
• general attributes of text, such as color, thickness, type of line, etc. For a better look, it is recommended to use linetype = CV2 LINE_ AA.
Draw a red OpenCV on the image.

cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)

import numpy as np
import cv2

img = np.zeros((512,512,3), np.uint8)
#Draw a straight line
#Draw rectangle
#Draw a circle,(447,63), 63, (0,0,255), -1)
#Draw an ellipse
#Draw polygon
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
#Add text to the picture
cv2.putText(img,'OpenCV',(10,500), font, 4,(0,0,255),2,cv2.LINE_AA)
#Result presentation
picture = 'example'
cv2.imshow(picture, img)

Tags: Python OpenCV image processing

Posted by drizzle on Mon, 09 May 2022 12:39:28 +0300