[OpenCV-Python] 6 Drawing functions in OpenCV

OpenCV-Python: II Gui features in OpenCV

6 Drawing functions in OpenCV

Goal
  •Learn to use OpenCV to draw different geometric figures
  •You will learn these functions: cv2.line(), cv2.circle(), cv2.rectangle(), cv2.ellipse(), cv2.putText(), etc.

Code
All of the above drawing functions need to set the following parameters:
  • img: The image you want to draw.
  • color: The color of the shape. Take RGB as an example, you need to pass in a tuple, for example: (255,0,0) represents blue. For grayscale images, only grayscale values ​​need to be passed in.
  • thickness: The thickness of the line. If you set a closed figure to -1, then this figure will be filled. The default value is 1.
  • linetype: line type, 8 connection, anti-aliasing, etc. The default is 8 connections. cv2.LINE_AA is anti-aliasing, so it looks very smooth.

6.1 Line drawing

To draw a line, you only need to tell the function the start and end points of the line. Below we will draw a blue line from the upper left to the lower right corner.

import numpy as np
import cv2

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a diagonal blue line with thickness of 5 px
cv2.line(img,(0,0),(511,511),(255,0,0),5)

img

6.2 Draw a rectangle

To draw a rectangle, you need to tell the function the coordinates of the upper left vertex and the lower right vertex of the function. This time we will say a green rectangle in the upper right corner of the image.

cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

img

6.3 Draw a circle

To draw a circle, you only need to specify the center point coordinates and radius of the circle. We draw a circle in the upper rectangle.

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

img

6.4 Draw an ellipse

Drawing an ellipse is more complicated, so we have to enter a few more parameters. One parameter is the position coordinates of the center point.
  The next parameter is the length of the major axis and the minor axis. The angle at which the ellipse rotates in the counterclockwise direction. The starting angle and ending angle of the ellipse arc in the clockwise direction, if it is 0 to 360, it is the entire ellipse. See cv2.ellipse() for more information. The following example is to draw a half ellipse in the center of the picture.

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

img

6.5 Drawing polygons

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

pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,255))

img

# Here the first parameter of reshape is -1, indicating that the length of this dimension is calculated according to the following dimensions.
Note: If the third parameter is False, the polygon we get is not closed (the end is not connected).
Note: cv2.polylines() can be used to draw many lines. Just put the line you want to draw in a list and pass this list to the function. Each line will be drawn independently. This will be faster than drawing one by one with cv2.line().

6.6 Add text to pictures

To draw text on a picture, you need to set the following parameters:
  • the text
  you want to draw
  • the position you want to draw • font type (check the cv2.putText() documentation to find the supported fonts)
  • font size
  • text size General attributes such as color, thickness, line type, etc. For a better look, it is recommended to use linetype=cv2.LINE_AA.
Draw a white OpenCV on the image.

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

img

Warning: The return value of all drawing functions is None, so img =cv2.line(img,(0,0),(511,511),(255,0,0),5) cannot be used.

Result
The following is the final result, show it through the knowledge you learned in the previous sections.

winname = 'example'
cv2.namedWindow(winname)
cv2.imshow(winname, img)
cv2.waitKey(0)
cv2.destroyWindow(winname)

img
For more information, please pay attention to the official account:
img

Guess you like

Origin blog.csdn.net/yegeli/article/details/113405436