当前位置:网站首页>【OpenCV 例程200篇】216. 绘制多段线和多边形
【OpenCV 例程200篇】216. 绘制多段线和多边形
2022-07-01 15:34:00 【小白YouCans】
专栏地址:『youcans 的 OpenCV 例程200篇』
文章目录:『youcans 的 OpenCV 例程200篇-总目录』
【youcans 的 OpenCV 例程200篇】216. 绘制多段线和多边形
函数 cv.polylines() 用来绘制多边形曲线或多段线。
函数 cv.fillPoly() 用来绘制一个或多个填充的多边形区域。
函数 cv.fillConvexPoly() 用来绘制一个填充的凸多边形。
函数原型:
cv.polylines(img, pts, isClosed, color[, thickness=1, lineType=LINE_8, shift=0]) → img
cv.fillPoly(img, pts, color[, lineType=LINE_8, shift=0, offset=Point()]) → img
cv.fillConvexPoly(img, points, color[, lineType=LINE_8, shift=0]) → img
参数说明:
- img:输入输出图像,允许单通道灰度图像或多通道彩色图像
- pts:多边形顶点坐标, 二维 Numpy 数组的列表
- points:多边形顶点坐标,二维 Numpy 数组
- isClosed: 闭合标志,True 表示闭合多边形,False 表示多边形不闭合
注意事项:
特别注意多边形顶点坐标 pts 的格式:
pts 是一个列表,列表中的元素是二维 Numpy 数组,每个元素表示一组顶点坐标。
二维 Numpy 数组的形状为 (m,2),每行表示多边形的一个顶点的坐标 (xi,yi),数据格式应为整型。例如:
points1 = np.array([[200,100], [295,169], [259,281], [141,281], [105,169]], np.int) points2 = np.array([[200,400], [259,581], [105,469], [295,469], [141,581]])points1、points2 是形状为 (m,2) 的二维 Numpy 数组。
对于函数 cv.fillConvexPoly,使用二维 Numpy 数组 points1 或 points2 作为参数。
对于函数 cv.polylines 与 cv.fillPoly,不能直接把二维 Numpy 数组 points1 或 points2作为函数参数,而要将其作为列表的元素,如: [points1]、[points2] 或 [points1, points2]。函数 cv.polylines 与 cv.fillPoly 可以绘制或填充一个或多个多边形。
当列表 pts 中只有一个元素,即一个二维 Numpy 数组时,绘制一个多边形;
当列表 pts 中有多个元素,即多个二维 Numpy 数组时,绘制多个多边形,每个二维 Numpy 数组中的顶点坐标被作为一个多边形处理。闭合标志为 True 时绘制闭合多边形,将绘制从最后一个顶点到第一个顶点之间的线段;闭合标志为 False 时绘制非闭合的多段线,最后一个顶点与第一个顶点之间不连接。
函数 cv.fillPoly 与 cv.fillConvexPoly 都可以绘制填充多边形。
cv.fillConvexPoly 的运行速度比 cv.fillPoly 快得多,特别适合填充凸多边形,经常用于轮廓处理。函数 cv.fillConvexPoly 不仅可以填充凸多边形,还可以填充任何没有自相交的单调多边形,即轮廓最多与每条水平线相交两次的多边形(尽管其最顶部和/或底部边缘可能是水平的)。
这句话来自官方文档,括号中内容“最顶部和/或底部边缘可能是水平的”不容易理解,可以结合例程 4.7 右下图理解。作者建议除轮廓处理外,尽量不要用 cv.fillConvexPoly 来填充非凸的多边形。
例程 A4.7:绘制多段线和多边形
# A4.7 绘制多段线和多边形
img = np.ones((980, 400, 3), np.uint8)*224
img1 = img.copy()
img2 = img.copy()
img3 = img.copy()
img4 = img.copy()
# 多边形顶点
points1 = np.array([[200,100], [295,169], [259,281], [141,281], [105,169]], np.int)
points2 = np.array([[200,400], [259,581], [105,469], [295,469], [141,581]]) # (5,2)
points3 = np.array([[200,700], [222,769], [295,769], [236,812], [259,881],
[200,838], [141,881], [164,812], [105,769], [178,769]])
print(points1.shape, points2.shape, points3.shape) # (5, 2) (5, 2) (10, 2)
# 绘制多边形,闭合曲线
pts1 = [points1] # pts1 是列表,列表元素是形状为 (m,2) 的 numpy 二维数组
cv.polylines(img1, pts1, True, (0,0,255)) # pts1 是列表
cv.polylines(img1, [points2, points3], 1, 255, 2) # 可以绘制多个多边形
# 绘制多段线,曲线不闭合
cv.polylines(img2, [points1], False, (0,0,255))
cv.polylines(img2, [points2, points3], 0, 255, 2) # 可以绘制多个多段线
# 绘制填充多边形,注意交叉重叠部分处理
cv.fillPoly(img3, [points1], (0,0,255))
cv.fillPoly(img3, [points2, points3], 255) # 可以绘制多个填充多边形
# 绘制一个填充多边形,注意交叉重叠部分
cv.fillConvexPoly(img4, points1, (0,0,255))
cv.fillConvexPoly(img4, points2, 255) # 不能绘制存在自相交的多边形
cv.fillConvexPoly(img4, points3, 255) # 可以绘制凹多边形,但要慎用
plt.figure(figsize=(9, 6))
plt.subplot(141), plt.title("closed polygon"), plt.axis('off')
plt.imshow(cv.cvtColor(img1, cv.COLOR_BGR2RGB))
plt.subplot(142), plt.title("unclosed polygo"), plt.axis('off')
plt.imshow(cv.cvtColor(img2, cv.COLOR_BGR2RGB))
plt.subplot(143), plt.title("fillPoly"), plt.axis('off')
plt.imshow(cv.cvtColor(img3, cv.COLOR_BGR2RGB))
plt.subplot(144), plt.title("fillConvexPoly"), plt.axis('off')
plt.imshow(cv.cvtColor(img4, cv.COLOR_BGR2RGB))
plt.tight_layout()
plt.show()

【本节完】
版权声明:
[email protected] 原创作品,转载必须标注原文链接:(https://blog.csdn.net/youcans/article/details/125468148)
Copyright 2022 youcans, XUPT
Crated:2022-6-26
欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列,持续更新中
欢迎关注 『youcans 的 OpenCV学习课』 系列,持续更新中
210. 绘制直线也会有这么多坑?
211. 绘制垂直矩形
212. 绘制倾斜的矩形
213. 绘制圆形
214. 绘制椭圆的参数详解
215. 基于多段线绘制近似椭圆
216. 绘制多段线和多边形
边栏推荐
- Logical analysis of automatic decision of SAP CRM organization model
- Wechat official account subscription message Wx open subscribe implementation and pit closure guide
- Description | Huawei cloud store "commodity recommendation list"
- 【显存优化】深度学习显存优化方法
- Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
- STM32ADC模拟/数字转换详解
- 【云动向】6月上云新风向!云商店热榜揭晓
- Introduction to MySQL audit plug-in
- TS报错 Don‘t use `object` as a type. The `object` type is currently hard to use
- 华为发布HCSP-Solution-5G Security人才认证,助力5G安全人才生态建设
猜你喜欢

做空蔚来的灰熊,以“碰瓷”中概股为生?

MySQL service is starting. MySQL service cannot be started. Solution
![Opencv Learning Notes 6 -- image feature [harris+sift]+ feature matching](/img/50/5c8adacea78e470c255070c8621ddd.png)
Opencv Learning Notes 6 -- image feature [harris+sift]+ feature matching

Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation

Tiantou village, Guankou Town, Xiamen special agricultural products Tiantou Village special agricultural products ant new village 7.1 answer

Hardware design guide for s32k1xx microcontroller

MySQL高级篇4

雷神科技冲刺北交所,拟募集资金5.4亿元

MySQL backup and restore single database and single table

Opencv learning note 4 -- bank card number recognition
随机推荐
Task. Run(), Task. Factory. Analysis of behavior inconsistency between startnew() and new task()
An intrusion detection model
go-zero实战demo(一)
What are the test items of juicer ul982
Basic use process of cmake
phpcms后台上传图片按钮无法点击
[advanced ROS] lesson 5 TF coordinate transformation in ROS
Stm32f4-tft-spi timing logic analyzer commissioning record
厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
The difference between arrow function and ordinary function in JS
Returning to the top of the list, the ID is still weak
[target tracking] |stark
[stm32-usb-msc problem help] stm32f411ceu6 (Weact) +w25q64+usb-msc flash uses SPI2 to read out only 520kb
22-06-26周总结
[leetcode] 16. The sum of the nearest three numbers
Flink 系例 之 TableAPI & SQL 与 MYSQL 插入数据
Wechat applet 03 - text is displayed from left to right, and the block elements in the line are centered
Day 3 of rhcsa study
Lean Six Sigma project counseling: centralized counseling and point-to-point counseling
Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all