Draw Geometry代码注释:
import cv2 as cv
import numpy as np
image = np.zeros((512, 512, 3), dtype=np.uint8)
cv.rectangle(image, (100, 100), (300, 300), (255, 0, 0), 2, cv.LINE_8, 0)
cv.circle(image, (256, 256), 50, (0, 0, 255), 2, cv.LINE_8, 0)
cv.ellipse(image, (256, 256), (150, 50), 360, 0, 360, (0, 255, 0), 2, cv.LINE_8, 0)
cv.imshow("image", image)
cv.waitKey(0)
for i in range(100000):
image[:,:,:]= 0
x1 = np.random.rand() * 512
y1 = np.random.rand() * 512
x2 = np.random.rand() * 512
y2 = np.random.rand() * 512
b = np.random.randint(0, 256)
g = np.random.randint(0, 256)
r = np.random.randint(0, 256)
# cv.line(image, (np.int(x1), np.int(y1)), (np.int(x2), np.int(y2)), (b, g, r), 4, cv.LINE_8, 0)
cv.rectangle(image, (np.int(x1), np.int(y1)), (np.int(x2), np.int(y2)), (b, g, r), 1, cv.LINE_8, 0)
cv.imshow("image", image)
c = cv.waitKey(20)
if c == 27:
break # ESC不
cv.imshow("image", image)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如图:
演示片段的两个截图:
视频暂时不会传
1、绘制直线
cv.line(img,start,end,color,thickness)
参数:
- img:要绘制直线的图像
- Start,end: 直线的起点和终点
- color: 线条的颜色
- Thickness: 线条宽度
实例:
//画直线
Mat myLine=Mat::zeros(400,400,CV_8UC3);
Point p1, p2;
p1 = Point(10, 2);
p2.x = 160;
p2.y = 150;
line(myLine, p1, p2, Scalar(233, 233, 233),2,-1);
line(myLine, Point(20, 40), Point(99, 99), Scalar(130,140 , 150),1,4);
line(myLine, Point(50, 60), Point(20, 160), Scalar(55, 66, 33),3,8);
line(myLine, Point(300, 60), Point(20, 360), Scalar(34, 76, 99), 2, -1);
line(myLine, Point(330, 60), Point(50, 360), Scalar(34, 76, 99), 2, 4);
line(myLine, Point(360, 60), Point(80, 360), Scalar(34, 76, 99), 2, 8);
line(myLine, Point(390, 60), Point(110, 360), Scalar(34, 76, 99), 2, 16);
imshow("myLine", myLine);
2、绘制矩形
cv.rectangle(img,leftupper,rightdown,color,thickness)
参数:
- img:要绘制矩形的图像
- Leftupper, rightdown: 矩形的左上角和右下角坐标
- color: 线条的颜色
- Thickness: 线条宽度
实例
Mat myRectange=Mat::zeros(400,400,CV_8UC3);
Point p1, p2;
p1 = Point(50, 50);
p2.x = 350;
p2.y = 350;
rectangle(myRectange, p1, p2, Scalar(88, 55, 99), 2, 8);//空心
rectangle(myRectange, Rect(Point(20, 20), Point(300, 300)), Scalar(43, 33, 55), -1);//实心,Thickness为负数
imshow("myRectange", myRectange);
3.绘制圆形
cv.circle(img,centerpoint, r, color, thickness)
参数:
- img:要绘制圆形的图像
- Centerpoint, r: 圆心和半径
- color: 线条的颜色
- Thickness: 线条宽度,为-1时生成闭合图案并填充颜色
实例
Mat myCicle=Mat::zeros(400,400,CV_8UC3);
Point center;
center= Point(150, 150);
int radius;
radius = 80;
circle(myCicle, center, radius, Scalar(99, 123,145), 5);//空心圆,thickness为正数
circle(myCicle, Point(250, 250),80,Scalar(44,66,44), -1);//实心圆,thickness为负数
imshow("myCicle", myCicle);
4、向图像中添加文字
cv.putText(img,text,station, font, fontsize,color,thickness,cv.LINE_AA)
参数:
- img: 图像
- text:要写入的文本数据
- station:文本的放置位置
- font:字体
- Fontsize :字体大小
实例
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
# 1 创建一个空白的图像
img = np.zeros((512,512,3), np.uint8)
# 2 绘制图形
cv.line(img,(0,0),(511,511),(255,0,0),5)
cv.rectangle(img,(384,0),(510,128),(0,255,0),3)
cv.circle(img,(447,63), 63, (0,0,255), -1)
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv.LINE_AA)
# 3 图像展示
plt.imshow(img[:,:,::-1])
plt.title('匹配结果'), plt.xticks([]), plt.yticks([])
plt.show()



