Python opencv common graphics drawing methods (line segment, rectangle, circle, ellipse, text)

I recently learned python opencv and shared the method of using opencv to draw common graphics on pictures.
insert image description here
In the case, the method of adding line segments, circles, rectangles, ellipses and adding text to the picture is realized, which is realized by using opencv2.

.

Implementation

1) Draw a line segment cv.line

Draw a straight line in the picture

# 绘制线段
# 参数1:图片
# 参数2:起点
# 参数3:终点
# 参数4:BGR颜色
# 参数5:宽度
cv2.line(img, (60, 40), (90, 90), (255, 255, 255), 2);

Parameter Description

parameter value illustrate
parameter 1 img original picture
parameter 2 (60, 40) Coordinates of the starting point of the line segment (pixels)
parameter 3 (90, 90) Coordinates of the end point of the line segment (pixels)
parameter 4 (255, 255, 255) BGR color (0 - 255)
parameter 5 2 line width

.

2) Draw a circle cv.circle

draw a circle in the picture

# 绘制圆形
# 参数1:图片
# 参数2:圆心
# 参数3:半径
# 参数4:BGR颜色
# 参数5:宽度 值为-1时填充
cv2.circle(img, (140, 120), 60, (0, 0, 255), 2);

Parameter Description

parameter value illustrate
parameter 1 img original picture
parameter 2 (140, 120) Coordinates of the center of the circle (pixels)
parameter 3 60 circle radius (pixels)
parameter 4 (0, 0, 255) BGR color (0 - 255)
parameter 5 2 Line segment width, fill when it is -1

.

3) Draw a rectangle cv.rectangle

Draw a rectangle in the picture

# 绘制矩形
# 参数1:图片
# 参数2:左上角
# 参数3:右下角
# 参数4:BGR颜色
# 参数5:宽度 值为-1时填充
cv2.rectangle(img, (220, 50), (380, 220), (0, 255, 0), 2);

Parameter Description

parameter value illustrate
parameter 1 img original picture
parameter 2 (220, 50) Coordinates (pixels) of the upper left corner
parameter 3 (380, 220) Coordinates (pixels) of the bottom right corner
parameter 4 (0, 255, 0) BGR color (0 - 255)
parameter 5 2 Line segment width, fill when it is -1

.

4) Draw an ellipse cv.ellipse

Draw an ellipse in the picture, there are more parameters here...

# 绘制椭圆
# 参数1:图片
# 参数2:圆心
# 参数3:横纵轴长
# 参数4:倾斜角度
# 参数5:绘制起点角度
# 参数6:绘制终点角度
# 参数7:BGR颜色
# 参数8:宽度 值为-1时填充
cv2.ellipse(img, (60, 20), (60, 20), 0, 0, 360, (255, 255, 255), 2);

Parameter Description

parameter value illustrate
parameter 1 img original picture
parameter 2 (60, 20) Ellipse center coordinates (pixels)
parameter 3 (60, 20) The horizontal and vertical distances of the ellipse (pixels)
parameter 4 0 Ellipse tilt angle (0-360)
parameter 5 0 Drawing start angle (0-360)
parameter 6 0 Draw end point angle (0-360)
parameter 7 (255, 255, 255) BGR color (0 - 255)
parameter 8 2 Line segment width, fill when it is -1

5) Add text cv.putText

Add text display content to pictures

# 添加文字
# 参数1:图片
# 参数2:文本内容
# 参数3:文字位置
# 参数4:文字类型
# 参数5:文字大小
# 参数6:BGR颜色
# 参数7:文字粗细
cv2.putText(img, "phone", (15, 30), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1);

Parameter Description

parameter value illustrate
parameter 1 img original picture
parameter 2 “phone” text content
parameter 3 (15, 30) The coordinate position of the upper left corner of the text (pixels)
parameter 4 cv2.FONT_HERSHEY_COMPLEX text type
parameter 5 1 font size
parameter 7 (255, 255, 255) BGR color (0 - 255)
parameter 8 1 font weight

The main text types are as follows:

1. cv.FONT_HERSHEY_SIMPLEX normal size sans serif
2. cv.FONT_HERSHEY_PLAIN small size sans serif
3. cv.FONT_HERSHEY_DUPLEX normal size (duplicate of type 1)
4. cv.FONT_HERSHEY_COMPLEX normal size serif
5. cv.FONT_HERSHEY_TRIPLEX normal Size with serif (complex version of type 4)
6, cv.FONT_HERSHEY_COMPLEX_SMALL (small size of type 4)
7, cv.FONT_HERSHEY_SCRIPT_SIMPLEX handwriting style
8, cv.FONT_HERSHEY_SCRIPT_COMPLEX (complex version of type 7)

You can try the above fonts, you can see different effects (^_ ^)

.

full code

import cv2

# 读取图片。
img = cv2.imread("img.jpeg");

# 绘制线段
# 参数1:图片
# 参数2:起点
# 参数3:终点
# 参数4:BGR颜色
# 参数5:宽度
cv2.line(img, (60, 40), (90, 90), (255, 255, 255), 2);

# 绘制圆形
# 参数1:图片
# 参数2:圆心
# 参数3:半径
# 参数4:BGR颜色
# 参数5:宽度 值为-1时填充
cv2.circle(img, (140, 120), 60, (0, 0, 255), 2);

# 绘制椭圆
# 参数1:图片
# 参数2:圆心
# 参数3:横纵轴长
# 参数4:倾斜角度
# 参数5:绘制起点角度
# 参数6:绘制终点角度
# 参数7:BGR颜色
# 参数8:宽度 值为-1时填充
cv2.ellipse(img, (60, 20), (60, 20), 0, 0, 360, (255, 255, 255), 2);

# 绘制矩形
# 参数1:图片
# 参数2:左上角
# 参数3:右下角
# 参数4:BGR颜色
# 参数5:宽度 值为-1时填充
cv2.rectangle(img, (220, 50), (380, 220), (0, 255, 0), 2);

# 添加文字
# 参数1:图片
# 参数2:文本内容
# 参数3:文字位置
# 参数4:文字类型
# 参数5:文字大小
# 参数6:BGR颜色
# 参数7:文字粗细
cv2.putText(img, "phone", (15, 30), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 1);
cv2.putText(img, "36.4C", (250, 250), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2);

# 显示图像
cv2.imshow("title", img);

# 进程不结束,一直保持显示状态
cv2.waitKey(0);

#销毁所有窗口
cv2.destroyAllWindows();

Guess you like

Origin blog.csdn.net/fujian87232/article/details/115556078