QT--Opencv drawing

Reminder: This article is for learning content. If there are any mistakes, please contact the author and be taught with humility.


foreword

As long as the future can be expected, today is worth rejoicing.


1. Related parameters

  1. img: the image you want to draw the shape color: the color of the shape. For BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale,
  2. Just pass the scalar value. Thickness: The thickness of a line or circle, etc. If -1 is passed for a closed shape (such as a circle), it will fill the shape. default thickness = 1
  3. lineType: the type of the line, whether it is an 8-connection line, an anti-aliased line, etc. By default, there are 8 connection lines.
  4. cv.LINE_AA gives anti-aliased lines that look great for curves.

2. Use steps

1. Draw lines

To draw a line, you need to pass the start and end coordinates of the line. We'll create a black image and draw a blue line across it from the top left corner to the bottom right corner.

# 创建黑色的图像
img = np.zeros((512,512,3), np.uint8)
# 绘制一条厚度为5的蓝色对角线
cv.line(img,(0,0),(511,511),(255,0,0),5)

2. Draw a rectangle

The shape of the drawing, the upper left corner of the rectangle, the lower right corner, the color, the thickness

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

3. Draw a circle

To draw a circle, you need its center coordinates and radius. We will draw a circle inside the rectangle drawn above.

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

4. Draw an ellipse

One parameter is the center position (x, y). The next parameter is the axis length (major
axis length, minor axis length). angle is the angle by which the ellipse is rotated counterclockwise. startAngle and endAngle represent the start and end of the arc of the ellipse measured clockwise from the main axis. i.e. giving 0 and 360 gives the full ellipse.

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

5. Draw polygons

To draw a polygon, you first need the coordinates of the vertices. Form these points into an array of shape ROWSx1x2, where ROWS is the number of vertices and should be of type int32.

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

If false, what is drawn is the line connecting the points.

6. Add text

Text data written - the coordinates of where to place it (ie the bottom left corner where the data starts). - font type (check cv.putText docs for supported fonts) - font scale (specify font size) - general stuff like color, thickness, line type, etc. For better appearance, it is recommended to use lineType = cv.LINE_AA.

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

Summarize

Good at summarizing, go further.

Guess you like

Origin blog.csdn.net/m0_51988927/article/details/131663472