深圳财务小公司网站,微信营销策略,抖音关键词排名,做百度手机网站关键词OpenCV 例程200篇 总目录 【youcans 的 OpenCV 例程200篇】212. 绘制倾斜的矩形
7.1 绘图函数基本参数
OpenCV提供了绘图功能#xff0c;可以在图像上绘制直线、矩形、圆、椭圆等各种几何图形。 函数原型#xff1a;
函数 cv.rectangle() 用来在图像上绘制垂直于图像边界的…OpenCV 例程200篇 总目录 【youcans 的 OpenCV 例程200篇】212. 绘制倾斜的矩形
7.1 绘图函数基本参数
OpenCV提供了绘图功能可以在图像上绘制直线、矩形、圆、椭圆等各种几何图形。 函数原型
函数 cv.rectangle() 用来在图像上绘制垂直于图像边界的矩形。
cv.rectangle(img, pt1, pt2, color[, thickness1, lineTypeLINE_8, shift0]) → img参数说明
img输入输出图像允许单通道灰度图像或多通道彩色图像pt1矩阵第一个点的坐标(x1, y1) 格式的元组pt2与 pt1 成对角的矩阵第二个点的坐标(x2, y2) 格式的元组color绘图线条的颜色(b,g,r) 格式的元组或者表示灰度值的标量thickness绘制矩形的线宽默认值 1px负数表示矩形内部填充lineType绘制线段的线性默认为 LINE_8 例程 A4.3在图像上绘制倾斜的矩形
cv.rectangle 只能在图像上绘制垂直于边界的矩形。如果需要绘制倾斜的矩形则要获得倾斜矩形的各个顶点坐标通过绘制直线构造成为闭合的矩形。 # A4.3 在图像上绘制倾斜的矩形height, width, channels 600, 400, 3img np.ones((height, width, channels), np.uint8)*192 # 创建黑色图像 RGB0# 围绕矩形中心旋转x, y, w, h (100, 200, 200, 100) # 左上角坐标 (x,y), 宽度 w高度 hcx, cy xw//2, yh//2 # 矩形中心img1 img.copy()cv.circle(img1, (cx,cy), 4, (0,0,255), -1) # 旋转中心angle [15, 30, 45, 60, 75, 90] # 旋转角度顺时针方向for i in range(len(angle)):ang angle[i] * np.pi / 180x1 int(cx (w/2)*np.cos(ang) - (h/2)*np.sin(ang))y1 int(cy (w/2)*np.sin(ang) (h/2)*np.cos(ang))x2 int(cx (w/2)*np.cos(ang) (h/2)*np.sin(ang))y2 int(cy (w/2)*np.sin(ang) - (h/2)*np.cos(ang))x3 int(cx - (w/2)*np.cos(ang) (h/2)*np.sin(ang))y3 int(cy - (w/2)*np.sin(ang) - (h/2)*np.cos(ang))x4 int(cx - (w/2)*np.cos(ang) - (h/2)*np.sin(ang))y4 int(cy - (w/2)*np.sin(ang) (h/2)*np.cos(ang))color (30*i, 0, 255-30*i)cv.line(img1, (x1,y1), (x2,y2), color)cv.line(img1, (x2,y2), (x3,y3), color)cv.line(img1, (x3,y3), (x4,y4), color)cv.line(img1, (x4,y4), (x1,y1), color)# 围绕矩形左上顶点旋转x, y, w, h (200, 200, 200, 100) # 左上角坐标 (x,y), 宽度 w高度 himg2 img.copy()cv.circle(img2, (x, y), 4, (0,0,255), -1) # 旋转中心angle [15, 30, 45, 60, 75, 90, 120, 150, 180, 225] # 旋转角度顺时针方向for i in range(len(angle)):ang angle[i] * np.pi / 180x1, y1 x, yx2 int(x w * np.cos(ang))y2 int(y w * np.sin(ang))x3 int(x w * np.cos(ang) - h * np.sin(ang))y3 int(y w * np.sin(ang) h * np.cos(ang))x4 int(x - h * np.sin(ang))y4 int(y h * np.cos(ang))color (30 * i, 0, 255 - 30 * i)cv.line(img2, (x1, y1), (x2, y2), color)cv.line(img2, (x2, y2), (x3, y3), color)cv.line(img2, (x3, y3), (x4, y4), color)cv.line(img2, (x4, y4), (x1, y1), color)plt.figure(figsize(9, 6))plt.subplot(121), plt.title(img1), plt.axis(off)plt.imshow(cv.cvtColor(img1, cv.COLOR_BGR2RGB))plt.subplot(122), plt.title(img2), plt.axis(off)plt.imshow(cv.cvtColor(img2, cv.COLOR_BGR2RGB))plt.show()
例程结果 【本节完】 版权声明 参考文献 Use the Photoshop Levels adjustment (adobe.com) youcansxupt 原创作品转载必须标注原文链接(https://blog.csdn.net/youcans/article/details/125432101) Copyright 2022 youcans, XUPT Crated2022-6-20 欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列持续更新中 欢迎关注 『youcans 的 OpenCV学习课』 系列持续更新中 210. 绘制直线也会有这么多坑 211. 绘制垂直矩形 212. 绘制倾斜的矩形