前言
OpenCV中基本图形的绘制。
一、基本图形绘制
1.画线
line(img, 开始点, 结束点,颜色,线宽,线型)
声明:void line(InputOutputArray img, Point pt1, Point pt2, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0)
参数:
img:需要绘制直线的图像
pt1:直线起点在图像中的坐标
pt2:直线终点在图像中的坐标
color:直线的颜色,用三通道表示
thickness :线宽
lineType:线的类型,可取值为-1、4、8、16
shift:坐标缩放比例
代码案例:
cv2.line(img, (x1,y1), (x2,y2), (255, 255, 255), 3, 4)
2.画矩形
rectangle(img, 左上角的点, 右下角的点, 颜色, 线宽, 线型)
声明:void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0)
参数:
img:需要绘制矩形形的图像
pt1:矩形左上角的点在图像中的坐标
pt2:矩形右下角的点在图像中的坐标
代码案例:
cv2.rectangle(img, (x1,y1), (x2,y2), (0,0,255), 3) # 后面两个参数为颜色和线长
3.画圆
circle(img,圆心,半径,颜色,线宽,线型)
声明:void circle(InputOutputArray img, Point center, int radius, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift = 0)
参数:
img:需要绘制圆形的图像
center:圆形的圆心位置坐标
radius:圆形的半径,单位为像素
color:圆形的颜色
thickness:轮廓的宽度,如果数值为负,则绘制一个实心圆
代码案例:
# 函数
cv2.circle(img, (x,y), 半径长度, (0,0,255), -1) // 画一个实心圆(点),最后参数设置为负数
cv2.circle(img, (x,y), 半径长度, (0,0,255), 4) // 画一个空心圆
4.画椭圆
ellipse(img, 中心点,长宽的一半,角度,开始角度,结束角度)
void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar & color, int thickness = 1, int lineType = LINE_8, int shift)
参数:
img:需要绘制椭圆的图像
center:椭圆的圆心位置坐标
axes:椭圆的半径
angle:椭圆整体旋转角度(绕圆心顺时针旋转角度)
startAngle:椭圆开始的角度(顺时针计算角度)
endAngle:椭圆结束的角度
color:颜色
thickness:线宽,如果数值为负,则绘制一个实心椭圆
代码案例:
cv2.ellipse(img, (400, 100), (100, 50), 45, -90, 90, (255, 255, 255), -1)
5.画多边形
polylines(img,点集,是否闭合,颜色) 不能填充
声明:void polylines(InputOutputArray img, const Point* const* pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0 );
参数:
img:需要绘制的图像
pts:多边形各点的集合,元素类型必须为np.int32
isClosed:是否闭合
fillPoly(img, 点集,颜色) 填充多边形
声明:void fillPoly(InputOutputArray img, const Point ** pts, const int * npts, int ncontours, const Scalar & color, int lineType = LINE_8, int shift = 0, Point offset = Point())
参数:与 polylines() 相同
代码案例:
pts1 = np.array([(300,10), (150,100), (450, 100)], np.int32)
cv2.polylines(img, [pts1], True, (255, 255, 255))
pts2 = np.array([(300,120), (150,220), (450, 220)], np.int32)
cv2.fillPoly(img, [pts2], (0, 255, 255))
6.绘制文本
putText(img,字符串,起始点,字体,字号,颜色)
声明:void putText( InputOutputArray img, const String& text, Point org, int fontFace, double fontScale, Scalar color, int thickness = 1, int lineType = LINE_8, bool bottomLeftOrigin = false );
参数:
img:需要文本的图像
text:字符串
org:起始点
fontFace:字体(0~7、16)
fontScale:字号
代码案例:
cv2.putText(img, "Draw", (500,400), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 255, 0))
全部图形绘制代码:
import cv2
import numpy as np
img = np.zeros((480, 640, 3), np.uint8)
cv2.line(img, (10, 20), (300, 400), (255, 255, 255), 3, 4)
cv2.rectangle(img, (100, 50), (200, 300), (0, 0, 255), 3) # 后面两个参数为颜色和线长
cv2.circle(img, (300, 400), 30, (0,0,255), -1) # 画一个实心圆(点),最后参数设置为负数
cv2.circle(img, (300, 400), 70, (0,0,255), 4) # 画一个空心圆
cv2.ellipse(img, (500, 100), (100, 50), 45, -90, 90, (255, 255, 255), -1) #画椭圆
# 画多边形
pts1 = np.array([(300,10), (150,100), (450, 100)], np.int32)
cv2.polylines(img, [pts1], True, (255, 255, 255))
pts2 = np.array([(300,120), (150,220), (450, 220)], np.int32)
cv2.fillPoly(img, [pts2], (0, 255, 255))
#绘制文本
cv2.putText(img, "Draw", (500,400), cv2.FONT_HERSHEY_TRIPLEX, 1, (0, 255, 0))
cv2.imshow('img', img)
key = cv2.waitKey(0)
if(key & 0xFF ==ord('q') ):
cv2.destroyAllWindows()
展示
二、综合实战:鼠标绘制基本图形
代码案例:
'''
基本功能:
可以通过鼠标进行基本图形的绘制
1、划线:当用户按下l,即选择了划线,此时滑动鼠标即可画线
2、画矩形:当用户按下r,即选择了划矩形,此时滑动鼠标即可画矩形
3、画圆:当用户按下c键,即选择了划圆,此时滑动鼠标即可画圆
'''
import cv2
import numpy as np
# curshape:0-line, 1-rectangle, 2-circle
curshape = 0
startpos = (0, 0) # 起始点
# 创建一个全黑的图像
img = np.zeros((480, 640, 3), np.uint8)
# 鼠标回调函数
def mouse_callback(event, x, y, flag, userdata):
# print(event, x, y, flag, userdata )
global startpos
if (event & cv2.EVENT_LBUTTONDOWN == cv2.EVENT_LBUTTONDOWN):
startpos = (x, y)
elif (event & cv2.EVENT_LBUTTONUP == cv2.EVENT_LBUTTONUP):
if curshape == 0:
cv2.line(img, startpos, (x, y), (0, 0, 255))
elif curshape == 1:
cv2.rectangle(img, startpos, (x, y), (0, 0, 255))
elif curshape == 2:
rudius =int(np.sqrt(np.power(x - startpos[0], 2) + np.power(y - startpos[1], 2)))
cv2.circle(img, startpos, rudius, (0, 0, 255))
else:
print('error: no shape')
cv2.namedWindow('mousedraw', cv2.WINDOW_NORMAL)
cv2.resizeWindow('mousedraw', 640, 480)
# 设置鼠标回调函数
cv2.setMouseCallback('mousedraw', mouse_callback, "12345")
while True:
cv2.imshow('mousedraw', img)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('l'):
curshape = 0
elif key == ord('r'):
curshape = 1
elif key == ord('c'):
curshape = 2
cv2.destroyAllWindows()
注:
Numpy库相关的算术运算可以在此链接文章中查看。