This article is from the huawei cloud community “[Python from zero to one] 35. Image Processing basics: OpenCV drawing all kinds of geometry”, by Eastmount.

Draw a line

In OpenCV, to draw a line, we need to obtain the coordinates of the starting point and ending point of the line. Call cv2.line() to achieve this function. The function prototype looks like this:

  • img = line(img, pt1, pt2, color[, thickness[, lineType[, Shift]]]) — img — pt1 — pt2 — color — line color For example, (255,0,0) is blue, it is thickness of a line, lineType is the lineType, and shift is the decimal number in the point coordinate

The following code draws a line, creating a black image with np.zeros() and then calling cv2.line() to draw the line with the starting coordinates and the color and thickness.

# -*- coding: UTF-8 -*- # By: Cv2. line(img, (0,0), Cv2.imshow ("line", img) cv2.waitKey(0) cv2.destroyallWindows ()Copy the code

Figure 3-1 shows the output. A line is drawn from coordinates (0,0) to (255,255). The color of the line is (55,255,155) and the thickness is 5.

Draw a rectangle

In OpenCV, drawing a rectangle is done using the cv2.Rectangle () function, which looks like this:

  • img = rectangle(img, pt1, pt2, color[, thickness[, lineType[, Shift]]]) — img represents the image to be drawn — pt1 represents the upper left corner of the rectangle — pt2 represents the lower right corner of the rectangle — color represents the color of the rectangle — thickness represents the border — lineType represents the lineType — Shift represents the number of decimal places in a point coordinate

The following code draws a rectangle, creating a black image with Np.Zeros () and then calling cv2.Rectangle () to draw the rectangle.

# -*- coding: UTF-8 -*- # By: = = = = = = = = = = = = = Cv2.imshow (" Rectangle ", img)Copy the code

The output result is shown in figure 3-2. The coordinates from the upper left corner are (20,20) and the coordinates from the lower right corner are (150,250). The color of the rectangle is blue (255,0,0) and the thickness is 2.

Draw a circle

In OpenCV, drawing a rectangle is done using the cv2.Rectangle () function, which looks like this:

  • img = circle(img, center, radius, color[, thickness[, lineType[, Shift]]]) — img indicates the image of the circle to be drawn — center indicates the coordinates of the circle — radius indicates the radius of the circle — color indicates the color of the circle — thickness if it is positive, it indicates the thickness of the circle outline. Negative thickness indicates that a fill circle is to be drawn – lineType indicates the boundary type of the circle – Shift indicates the center coordinate and decimal number in the radius value

The following code draws a circle.

# -*- coding: UTF-8 -*- # By: Img = np.zeros((256,256,3), np.uint8) Imshow ("circle", img) # wait to show cv2.waitKey(0) cv2.DestroyallWindows ()Copy the code

The output result is shown in figure 3-3. It draws a circle with a radius of 50, a color of (255,255,0) and a thickness of 4 at the position with a circle of (100,100).

Note that if the thickness is set to “-1”, the drawn circle is solid, as shown in Figure 3-4.

  • Cv2. circle(img, (100,100), 50, (255,255,0), -1)

Draw the ellipse

In OpenCV, ellipse drawing is more complex, to input several parameters, such as the location of the center point coordinates, the length of the major axis and the short axis, the Angle of the ellipse along the counterclockwise direction and so on. The cv2.ellipse() function prototype looks like this:

  • img = ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, Shift]]]) — img indicates the image of the ellipse to be drawn — Center indicates the coordinates of the ellipse — axes indicates the length of the axis (short radius and long radius) — Angle indicates the Angle of deflection (counterclockwise rotation) — startAngle indicates the Angle of the starting Angle of the arc (counterclockwise rotation) — EndAngle indicates the endAngle of the arc (counterclockwise) — color indicates the color of the line — thickness if it is positive, it indicates the thickness of the outline of the ellipse. A negative value indicates that a fill ellipse is to be drawn – lineType indicates the boundary type of the circle – shift indicates the decimal number in the center and axis values

Here is the code to draw an ellipse.

# -*- coding: UTF-8 -*- # By: Eastmount import cv2 import numpy as np # img = np.zeros((256,256,3), Uint8) # draw ellipse # center of ellipse(120,100) # deflection Angle 20 # start Angle of arc 0 end Angle of arc 360 # color (255,0,255) line thickness 2 cv2.ellipse(img, Cv2.imshow ("ellipse", "ellipse") Cv2.waitkey (0) cv2.DestroyallWindows ()Copy the code

The output result is shown in Figure 3-5. The center of the ellipse is (120,100), the long axis is 100, the short axis is 50, the deflection Angle is 20, the Angle of the beginning Angle of the arc is 0, and the Angle of the end Angle of the arc is 360, indicating a complete ellipse. The color is (255,0,255) and the thickness is 2.

Draw polygons

In OpenCV, call cv2.polylines() to draw polygons. It needs to specify the coordinates of each vertex. Build polygons from these points.

  • img = polylines(img, pts, isClosed, color[, thickness[, lineType[, Shift]]]) — img indicates the image to be drawn — center indicates the polygon curve array — isClosed indicates whether the polygon to be drawn isClosed. False indicates no closing – color indicates the color of a line – thickness indicates the thickness of a line – lineType indicates the boundary type – Shift indicates the decimal number in the vertex coordinates

Here is the code to draw a polygon.

# -*- coding: UTF-8 -*- # By: Img = np.zeros((256,256,3), np.uint8) # draw polygon PTS = np.array([[10,80], [120,80], [120,200], [30,250]]) cv2.polylines(img, [PTS], True, (255, 255, 255), 5) # Cv2.waitkey (0) cv2.DestroyallWindows ()Copy the code

Figure 3-6 shows the output. The drawn polygon is a closed white figure.

The following code draws a pentagram polygon.

# -*- coding: UTF-8 -*- # By: Img = np.zeros((512,512,3), np.uint8) # draw polygon PTS = np.array([[50, 190], [380, 420], [255, 50], [120, 420], [450, 190]]) cv2.polylines(img, [pts], True, (0, 255, 255), Cv2.imshow ("ellipse", img) cv2.waitKey(0) cv2.DestroyallWindows ()Copy the code

The output is shown in Figure 3-7, which connects the left side of the five vertices to form a yellow five-pointed star.

Draw text

In OpenCV, call cv2.puttext () to add the corresponding text. The function prototype looks like this:

  • img = putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, BottomLeftOrigin]]]) — img indicates the image to be drawn — text indicates the text to be drawn — org indicates the position to be drawn, the bottom left corner of the string in the image — fontFace indicates the font type, See CV ::HersheyFonts — fontScale It is calculated as the scale factor multiplied by the specific basic size of the font – Color indicates the color of the font – Thickness indicates the thickness of the font – lineType indicates the boundary type – bottomLeftOrigin If it is true, the origin of the image data is at the lower left corner, otherwise it is at the upper left corner

Here is the code to draw the text.

# -*- coding: UTF-8 -*- # By: Eastmount import cv2 import numpy as np # img = np.zeros((256,256,3), Font = cv2.font_hershey_simplex cv2.puttext (img, 'I love Python! I love Huawei! ', (10, 100), font, 0.5, (255, 255, 0), 2) # Cv2.waitkey (0) cv2.DestroyallWindows ()Copy the code

Figure 3-8 shows the output. The text “I love Python! I love Huawei!” .

7. To summarize

In this paper, the method of drawing geometric figures in OpenCV is introduced in detail, which is realized by using cv2.line(), v2.circle(), cv2.Rectangle (), cv2.ellipse(), cv2.polylines(), and cv2.puttext () functions. In this article, beginners will understand the basic usage of basic graphing for subsequent applications. At the same time, it is suggested that readers combine this article to achieve a drawing software, can you?

Click to follow, the first time to learn about Huawei cloud fresh technology ~