Chapter 3 - OpenCV Basics - 8 - Drawing Functions

front content

This content is not the content of this book, but it will be used later, so it is specially recorded.

It is inevitable to use various drawing functions in OpenCV, such as drawing face library and displaying face recognition information, then you need to use OpenCV drawing functions, these functions include cv2.line(),

cv2.circle(),cv2.rectangle(),cv2.ellipse(),cv2.putText()等等。

The parameter description that the above function needs to use:

  • img : the canvas to draw the image on (image)
  • color : the color of the shape, RGB is identified as the ancestor of (B, G, R), and the grayscale image only needs to pass in the grayscale value
  • thickness : the thickness of the line, the closed image is set to -1, which is responsible for displaying the thickness of the graphic line
  • linetype : line type, there are 8 connections/anti-aliasing, etc., the default is 8 connections. cv2.LINE_AA is anti-aliasing, the graphics will become smoother
  • draw a straight line

Just know the coordinates of the starting 2 points.

cv2.line (drawing layer, (starting point x, starting point y), (end point x, end point y), (b, g, r), thickness size)

  • draw a circle

The position of the circle can be determined by determining the origin and radius of the circle.

cv2.circle (drawing layer, (center x, center y), radius, (b, g, r), line thickness)

  • draw a rectangle

The size of the rectangle can be determined by the upper left corner and the lower right corner before.

cv2.rectangle (drawing layer, (upper left corner x, upper left corner y), (lower right corner x, lower right corner y), (b, g, r), line thickness)

  • draw an ellipse

cv2.ellipse (drawing layer, (ellipse center x, ellipse center y), (major axis length, minor axis length), ellipse overall rotation angle, ellipse drawing start angle, ellipse drawing stop angle, color, line thickness)

When the thickness of the line is -1, it means a solid ellipse. If the long and short axes are the same, it will become a circle, and then control the drawing angle to draw a semicircle

  • draw irregular images

Determine the size and style of the entire graphic through a series of point collections.

cv2.polylines (drawing layer, point set, closed or not, (b, g, r), line thickness)

  • draw text

cv.putText (drawing layer, content, (x in the lower left corner of the text, y in the lower left corner of the text), font style, font size, font color, line width)

An example program is as follows:

canvas = np.zeros((600, 600, 3), np.uint8)  # 全0的黑色背景
# canvas = np.ones((600, 600, 3), np.uint8) * 255  # 全白背景

# 绘制直线
cv.line(canvas, (0, 0), (200, 90), (255, 0, 0), 5)  # cv2.line(绘制图层,(起点x,起点y),(终点x,终点y),(b,g,r),粗细大小)

# 绘制矩阵
cv.rectangle(canvas, (250, 250), (300, 430), (0, 255, 255),
             2)  # cv2.rectangle(绘制图层,(左上角x,左上角y),(右下角x,右下角y),(b,g,r),划线粗细) 划线粗细为-1时表示实心矩阵

# 绘制圆形
cv.circle(canvas, (400, 400), 50, (0, 0, 255), -1)  # cv2.circle(绘制图层,(圆心x,圆心y),半径,(b,g,r),划线粗细) 划线粗细为负数时表示实心圆

#绘制椭圆
cv.ellipse(canvas, (256, 256), (100, 70), 30, 0, 360, 255, -1)#cv2.ellipse(绘制图层,(椭圆圆心x,椭圆圆心y),(长轴长,短轴长),椭圆整体旋转角度,椭圆绘制开始角度,椭圆绘制停止角度,颜色,划线粗细)划线粗细为-1时表示实心椭圆

# 绘制其他边形
pts = np.array([[30, 50], [30, 250], [130, 250], [130, 350], [230, 350]], np.int32)
cv.polylines(canvas, [pts], True, (100, 100, 100), 1)  # cv2.polylines(绘制图层,点集,是否闭合,(b,g,r),划线粗细)

# 绘制文字 cv.putText(绘制图层,内容,(文字左下角x,文字左下角y),字体样式,字体大小,字体颜色,线条宽度)
cv.putText(canvas, "HelloOpenCV", (100, 100), cv2.FONT_HERSHEY_TRIPLEX, 2, (66, 66, 66), 3)
cv.imshow("Canvas", canvas)
cv.waitKey()
cv.destroyAllWindows()

Run as follows:

 

Guess you like

Origin blog.csdn.net/sunguanyong/article/details/129306921