Python: opencv draws points, circles, lines, polygons, rectangles

Introduction : The visual direction of machine learning generally needs to add a label box to the image. The label box is very useful, especially to delineate some features that need attention in the image, so as to facilitate the processing of feature selection.

Related strategies:

Machine Learning: Basic Process
Python: Call the camera and use the cv2 library to record video
Python: Split the video into frame-by-frame pictures Python: Use the cv2 module to
recognize gestures
Perform multiple face detection
Python: use cv2 module for face recognition Demo
Python: deal with cv2 module putText Chinese cannot be recognized
Python: use cv2 module to quickly generate sketches

draw points:

cv2.circle(img, point coordinates, point size, color, border line size)

Draw a circle:

cv2.circle(img, center coordinates, radius, color, border line size)

draw line:

cv2.line(img, (start coordinates), (end coordinates), color=(255, 0, 0), thickness=border line size)

Draw polygons: such as triangles

cv2.line(img, (200, 200), (200, 300), color=(255, 0, 255), thickness=2)
cv2.line(img, (200, 300), (300, 300), color=(255, 0, 255), thickness=2)
cv2.line(img, (300, 300), (200, 200), color=(255, 0, 255), thickness=2)

Draw a rectangle:

cv2.rectangle(img, (top left corner), (bottom right corner), color=(0, 0, 255), thickness=2)

source code:

# -*- coding: utf-8 -*-
import cv2.cv2 as cv2

img = cv2.imread(r"a.png")  # 读取图片

point_size = 1
point_color = (0, 0, 255)  # BGR
thickness = 2

# 画点
point = (100, 50)  # 点的坐标。画点实际上就是画半径很小的实心圆。
cv2.circle(img, point, point_size, point_color, thickness)

# 画圆
circle_point = (100, 100)
cv2.circle(img, circle_point, 20, point_color, thickness)

# 画线
cv2.line(img, (0, 0), (100, 100), color=(255, 0, 0), thickness=2)

# 画三角形:本质是多边形,即首尾相连的三条线。
cv2.line(img, (200, 200), (200, 300), color=(255, 0, 255), thickness=2)
cv2.line(img, (200, 300), (300, 300), color=(255, 0, 255), thickness=2)
cv2.line(img, (300, 300), (200, 200), color=(255, 0, 255), thickness=2)

# 矩形
cv2.rectangle(img, (715, 415), (830, 600), color=(0, 0, 255), thickness=2)


cv2.imshow("img", img)  # 展示结果
cv2.imwrite("label_data.png", img)  # 另存为
cv2.waitKey(4000)  # 展示多久后关闭。4000=4秒
cv2.destroyAllWindows()

operation result:

picture

WeChat public account: Play test development
Welcome to pay attention and make progress together, thank you!

insert image description here

Guess you like

Origin blog.csdn.net/hzblucky1314/article/details/123896460