The 0 point of the coordinates is the upper left corner.
When the width of the line is -1, it is padding
1. Straight line
cv2.line()
arg1: picture, arg2: start point of line, arg3: end point of line, arg4: color, arg5: width of line (pixels)
2. Rectangle
cv2.rectangle()
arg1: picture, arg2: vertex, arg3: opposite point of vertex, arg4: color, arg5: width of line, arg6: type of line, arg7: shift, role to be confirmed
3. Draw a circle
cv2.circle()
arg1: picture, arg2: coordinates of center of circle, arg3: radius, arg4: color, arg5: width of line
4. Draw an ellipse
cv2.ellipsis()
arg1: picture, arg2: center coordinate, arg3: length of axis (major axis, minor axis), arg4: angle of rotation, arg5: angle of start point, arg6: angle of end point, arg7: color, arg8: width of line
5. Draw Polygons
cv2.polylines()
arg1: image, arg2: vertex array of polygon, arg3: whether each point is connected, arg4: color, arg4: width of line
6. Add text
cv2.putText()
arg1: picture, arg2: text, arg3: position coordinates of the lower left corner of the text, arg4: font, arg5: word size, arg6: color, arg7: bold, arg8: line type
----- sample coding -----
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 5px
img = cv2.line(img, (0, 0), (512, 512), (255, 0, 0), 5, 1, 0)
# Draw rectangle
img = cv2.rectangle(img, (384, 0), (510, 129), (0, 255, 0), 3)
#draw cycle
img = cv2.circle(img, (447, 63), 63, (0, 0, 255), -1)
#draw ellipse
img = cv2.ellipse(img, (256, 256), (100, 50), 10, 0, 360, 255, 2)
#draw polygon
pts = np.array([[10, 5], [20, 30], [70, 20], [50, 10]], np.int32)
# pts = pts.reshape ((-1, 1,2))
img = cv2.polylines(img, [pts], True, (0, 255, 255), 3)
# add text
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'OpenCV', (10, 500), font, 4, (255, 255, 255), 2, cv2.LINE_AA)
cv2.imshow('Drawing', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
-----
result:
-- End --