[CV Direction] OpenCV Graphics Drawing Guide

introduction

Python OpenCV is a powerful computer vision library that provides rich functionality to draw various graphics in addition to image processing and computer vision tasks. Whether it is marking regions of interest in computer vision applications or drawing geometric shapes or text on images, OpenCV provides us with easy-to-use methods. This article will introduce how to use Python OpenCV for graphics drawing.

1. Create canvas

Before we start drawing graphics, we first need to create a blank canvas. In OpenCV, we can use cv2.imread()the function to load an image, or use np.zeros()to create a blank image as a canvas.

Sample code:

import cv2
import numpy as np

# 创建一张空白的画布
canvas = np.zeros((500, 500, 3), dtype=np.uint8)

cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code, we use np.zeros()the function to create an array of zeros of shape (500, 500, 3), which represents the width, height and number of channels of the canvas. Then, we use cv2.imshow()the function to display the canvas.

Insert image description here

2. Draw line segments

Drawing line segments is one of the basic operations in graphics drawing. In OpenCV, we can use cv2.line()the function to draw line segments.

Sample code:

import cv2

# 在画布上绘制一条线段
start_point = (100, 100)
end_point = (400, 400)
color = (0, 0, 255)  # 红色线段
thickness = 3

cv2.line(canvas, start_point, end_point, color, thickness)

cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code, we use cv2.line()the function to draw a red line segment from (100, 100) to (400, 400) on the canvas. We can also thicknessset the thickness of the line segment by adjusting the parameters.

Insert image description here

3. Draw a rectangle

Drawing a rectangle is one of the common graphics drawing operations. In OpenCV, we can cv2.rectangle()draw a rectangle using the function.

Sample code:

import cv2

# 在画布上绘制一个矩形
top_left = (200, 200)
bottom_right = (400, 400)
color = (0, 255, 0)  # 绿色矩形
thickness = 2

cv2.rectangle(canvas, top_left, bottom_right, color, thickness)

cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code, we use cv2.rectangle()the function to draw a green rectangle from (200, 200) to (400, 400) on the canvas. We can thicknessset the border thickness of the rectangle by adjusting the parameters.

Insert image description here

4. Draw a circle

Drawing a circle is also one of the common graphics drawing operations. In OpenCV, we can cv2.circle()draw a circle using the function.

Sample code:

import cv2

# 在画布上绘制一个圆形
center = (300, 300)
radius = 100
color = (255, 0, 0)  # 蓝色圆形
thickness = -1  # 填充圆形

cv2.circle(canvas, center, radius, color, thickness)

cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code, we use cv2.circle()the function to draw a blue filled circle with (300, 300) as the center and a radius of 100 on the canvas. We can thicknessset the border thickness of the circle by adjusting the parameter. A negative value means filling the circle.

Insert image description here

5. Draw an ellipse

Drawing ellipses is also one of the common graphics drawing operations. In OpenCV, we can cv2.ellipse()draw ellipses using the function.

Sample code:

import cv2

# 在画布上绘制一个椭圆
center = (250, 250)
axes = (150, 100)
angle = 0
start_angle = 0
end_angle = 360
color = (0, 255, 255)  # 黄色椭圆
thickness = 2

cv2.ellipse(canvas, center, axes, angle, start_angle, end_angle, color, thickness)

cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code, we use cv2.ellipse()the function to draw a yellow ellipse on the canvas with (250, 250) as the center, the major axis as 150, and the minor axis as 100. We can thicknessset the border thickness of the ellipse by adjusting the parameters.

Insert image description here

6. Draw polygons

Drawing polygons is a common operation for drawing complex shapes. In OpenCV, we can cv2.polylines()draw polygons using the function.

Sample code:

import cv2

# 在画布上绘制一个多边形
points = np.array([[100, 100], [200, 50], [300, 150], [250, 200]], np.int32)
points = points.reshape((-1, 1, 2))
color = (255, 255, 0)  # 青色多边形
thickness = 2

cv2.polylines(canvas, [points], True, color, thickness)

cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code, we use cv2.polylines()the function

A cyan polygon composed of multiple vertices is drawn. pointsis an array containing the coordinates of multiple vertices, we can add more vertices as needed. We can thicknessset the border thickness of the polygon by adjusting the parameters.

Insert image description here

7. Draw fonts

In graphics drawing, sometimes it is necessary to add text labels to images. In OpenCV, we can use cv2.putText()the function to draw text on an image.

Sample code:

import cv2

# 在画布上绘制文本
text = 'OpenCV'
position = (200, 250)
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1.5
color = (255, 255, 255)  # 白色文本
thickness = 2

cv2.putText(canvas, text, position, font, font_scale, color, thickness)

cv2.imshow('Canvas', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code, we use cv2.putText()the function to draw a white text "OpenCV" on the canvas, located at the position (200, 250). We can font_scaleset the size of the text by adjusting the parameters, and thicknessset the thickness of the text by adjusting the parameters.

Insert image description here

in conclusion

Through the introduction of this article, we have learned how to use Python OpenCV for graphics drawing. We can create a canvas and draw lines, rectangles, circles, ellipses, polygons and text using the cv2.line(), cv2.rectangle(), cv2.circle(), cv2.ellipse(), cv2.polylines()and functions. cv2.putText()These graph drawing operations are very useful in computer vision tasks and image processing. I hope this article can help you master the graphics drawing functions of Python OpenCV and apply them in actual projects.

Guess you like

Origin blog.csdn.net/qq_21484461/article/details/131333236