Python-OpenCV-drawing (drawing lines, rectangles, circles, adding text)

table of Contents

Parameter Description

1. Draw a line 

2.Rectangle

3. Circle

4. Ellipse

5. Polygon

6. Add text to text or rectangle


Parameter Description

The function for drawing the shape has some common parameters:

  • img: the picture of the shape to be drawn
  • color: the color to be drawn
    • The color image is passed into a set of BGR values, such as blue is (255,0,0)
    • Grayscale image, just pass in a grayscale value
  • thickness: line width, the default is 1; for closed shapes such as rectangles/circles, pass in -1 to indicate filled shapes
  • lineType: line type, three parameters are optional cv2.LINE_4 , cv2.LINE_8 , cv2.LINE_AA

1. Draw a line 

cv2.line(img, ps, pe, color, thickness, lineType, shift)

ps: the starting point of the straight line, note that this is a coordinate point, similar to (X, Y), pe: the ending point of the straight line, the same as above

2.Rectangle

cv2.rectangle(img, ps, pe, color, thickness, lineType, shift)

ps: the coordinates of the upper left corner of the rectangle, pe: the coordinates of the lower right corner of the rectangle, shift: the number of decimal places of the coordinate point

3. Circle

cv2.circle(img, center, radius, color, thickness, lineType, shift)

center: center coordinates, radius: radius value of the circle, shift: center coordinates and the number of decimal places of the radius

4. Ellipse

cv2.ellipse(img, center, axes, rotateAngle, startAngle, endAngle, color, thickness, lineType, shift)

center: the coordinates of the center of the ellipse, note that this is a coordinate value, axes: the length of the major and minor axes of the ellipse, this is a tuple of information

rotateAngle: the angle of the ellipse to rotate, startAngle: the starting angle of the elliptical arc, endAngle: the ending angle of the elliptical arc

The origin in OpenCV is in the upper left corner, so the angle here is calculated in a clockwise direction

5. Polygon

cv2.polylines(img, pts, isClosed, color, thickness, lineType, shift)

pts: A list of coordinate points on each side of the polygon, which is a numpy array type

isClosed: the value is True or False, if it is True, it means a closed polygon, if it is False, it will not be closed

To draw a polygon, you first need the coordinates of the vertices. Group these points into ROWSx1x2an array of shape , where ROWSis the number of vertices, and its type should be int32 . Here, we have drawn a small yellow polygon with four vertices.

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 you need to draw multiple lines, using cv2.polylines() is much more efficient than cv2.line() , for example:

# 使用cv2.polylines()画多条直线
line1 = np.array([[100, 20],  [300, 20]], np.int32).reshape((-1, 1, 2))
line2 = np.array([[100, 60],  [300, 60]], np.int32).reshape((-1, 1, 2))
line3 = np.array([[100, 100],  [300, 100]], np.int32).reshape((-1, 1, 2))
cv2.polylines(img, [line1, line2, line3], True, (0, 255, 255))

6. Add text to text or rectangle

cv2.putText(img, text, org, fontFace, fontScale, color, thickness, lineType, bottomLeftOrigin)

text: text content, org: the coordinates of the lower left corner of the text in the image

fontFace: font type, optional parameters are as follows

FONT_HERSHEY_SIMPLEX,FONT_HERSHEY_PLAIN,FONT_HERSHEY_DUPLEX,FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, orFONT_HERSHEY_SCRIPT_COMPLEX

fontScale: the scaling ratio, multiply the value by the default size of the program font to get the font size

bottomLeftOrigin: The default is true, which means that the origin of the image data is in the lower left corner; if it is False, it means that the origin of the image data is in the upper left corner.

import cv2 as cv
import numpy as np

img=np.zeros([512,512,3],np.uint8) ##创建一副黑色的图片

"""画线"""
cv.line(img,(0,32),(512,89),(255,0,0),3,cv.LINE_8)

"""画矩阵"""
tangle=cv.rectangle(img, (84, 45), (210, 228), (0, 255, 0), 3,cv.LINE_4)

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

"""画椭圆"""
cv.ellipse(img, (256, 256), (100, 50), 90, 0, 360, (255, 0, 0), 2)

"""画多边形"""
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))

"""在图片上加文字"""
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img,'OpenCV',(10,500), font, 2,( 0,255,0),2,cv.LINE_AA)

"""矩形框上加文字"""
cv.putText(tangle,'OpenCV',(84, 40), font, 0.5,( 0,255,0),1,cv.LINE_AA)

"""使用cv.polylines()画多条直线"""
line1 = np.array([[100, 20],  [300, 20]], np.int32).reshape((-1, 1, 2))
line2 = np.array([[100, 60],  [300, 60]], np.int32).reshape((-1, 1, 2))
line3 = np.array([[100, 100],  [300, 100]], np.int32).reshape((-1, 1, 2))
cv.polylines(img, [line1, line2, line3], True, (0, 255, 255))

cv.imshow('img',img)
cv.waitKey(0)
cv.destroyAllWindows()

Guess you like

Origin blog.csdn.net/zangba9624/article/details/105959943