基本图形绘制
- 点和线:line
- 矩形:rectangle
- 圆:circle
- 椭圆:ellipse
- 填充:线宽值小于 0 就是填充
- PS: 基本图形绘制API中都有线条的颜色、线宽、线型和 shift :color: 线段的颜色,通过一个Scalar对象定义;thickness: 线条的宽度,默认是1;lineType: 线段的类型,可以取值8, 4, 和CV_AA, 分别代表8邻接连接线,4邻接连接线和反锯齿连接线。默认值为8邻接,为了获得更好地效果可以选用CV_AA(采用了高斯滤波);shift: 坐标点小数点位数,默认是0。
- C++ API
Mat canvas = Mat::zeros(Size(512,512), CV_8UC3);
namedWindow("canvas", WINDOW_AUTOSIZE);
line(canvas, Point(10,10), Point(400,400), Scalar(0,0,255), 1, LINE_8);
Rect rect(100,100,200,200);
rectangle(canvas, rect, Scalar(0, 255, 0), 1, LINE_8);
circle(canvas, Point(256, 256), 100, Scalar(255,0,0), 1, LINE_8);
RotatedRect rrt;
rrt.center = Point2f(256, 256);
rrt.angle = 45.0;
rrt.size = Size(100, 200);
ellipse(canvas, rrt, Scalar(0,255,0), 1, LINE_8 );
imshow("canvas", canvas);
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3ac54899fe065a3d3a03f1aa67071327.png#pic_center)
cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f06637c4a5af819ae59484111582fe87.png#pic_center)
cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8c3b1c5887a1b8c8ce2e9513577742fb.png#pic_center)
cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/72835a9ce6c7f67a4397186255bbd247.png#pic_center)
cv2.ellipse(img, center, axes, rotateAngle, startAngle, endAngle, color[, thickness[, lineType[, shift]]])
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/aa1ee14b209bf074fc1efc61a5e5e252.png#pic_center)
cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/316dfc40b7df38ab90a41bf2380a7b8b.png#pic_center)
import numpy as np
import cv2 as cv
img = np.zeros((320, 320, 3), np.uint8)
print(img.shape)
ptStart = (60, 60)
ptEnd = (260, 260)
point_color = (0, 255, 0)
thickness = 1
lineType = 4
cv.line(img, ptStart, ptEnd, point_color, thickness, lineType)
ptLeftTop = (60, 60)
ptRightBottom = (260, 260)
point_color = (255, 0, 0)
thickness = 1
lineType = 4
cv.rectangle(img, ptLeftTop, ptRightBottom, point_color, thickness, lineType)
cv.circle(img, (160, 160), 60, point_color, 0)
ptCenter = (160, 160)
axesSize = (100, 45)
rotateAngle = 90
startAngle = 0
endAngle = 360
point_color = (0, 0, 255)
thickness = 1
lineType = 4
cv.ellipse(img, ptCenter, axesSize, rotateAngle, startAngle, endAngle, point_color, thickness, lineType)
ptCenter = (160, 60)
axesSize = (100, 45)
rotateAngle = 0
startAngle = 180
endAngle = 360
point_color = (0, 0, 255)
thickness = 1
lineType = 4
cv.ellipse(img, ptCenter, axesSize, rotateAngle, startAngle, endAngle, point_color, thickness, lineType)
text = 'zcj'
org = (40, 80)
fontFace = cv.FONT_HERSHEY_COMPLEX
fontScale = 1
fontcolor = (0, 255, 0)
thickness = 1
lineType = 4
bottomLeftOrigin = 1
cv.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType)
cv.namedWindow("image")
cv.imshow('image', img)
cv.waitKey(0)
cv.destroyAllWindows()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ca6b7d6b7ed913c0db5243645506ff65.png#pic_center)
随机图形绘制
Mat image = Mat::zeros(Size(512, 512), CV_8UC3);
int x1 = 0, y1 = 0;
int x2 = 0, y2 = 0;
RNG rng(12345);
while (true) {
x1 = (int)rng.uniform(0, 512);
x2 = (int)rng.uniform(0, 512);
y1 = (int)rng.uniform(0, 512);
y2 = (int)rng.uniform(0, 512);
line(image, Point(x1, y1), Point(x2, y2), Scalar(rng.uniform(10,256), rng.uniform(10, 256), rng.uniform(10, 256)), 1, LINE_8);
int w = abs(x2 - x1);
int h = abs(y2 - y1);
Rect rect2;
rect2.x = x1;
rect2.y = y1;
rect2.width = w;
rect2.height = h;
rectangle(image, rect2, Scalar(255,0,0), 1, LINE_8);
imshow("image", image);
if ( (char)waitKey(1000) == 'q') {
break;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1b6d354f12d9f69fa04b353d1380c650.png#pic_center)
添加文字
putText(canvas, "hello opencv", Point(100, 50), FONT_HERSHEY_COMPLEX, 1.0, Scalar(0, 255, 0), 2, LINE_8);
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3d18a74b36f02d8f894e992db22294d9.png#pic_center)