当前位置:网站首页>[200 opencv routines] 216 Draw polylines and polygons
[200 opencv routines] 216 Draw polylines and polygons
2022-07-01 15:40:00 【Xiaobai youcans】
Column address :『youcans Of OpenCV routine 200 piece 』
List of articles :『youcans Of OpenCV routine 200 piece - General catalogue 』
【youcans Of OpenCV routine 200 piece 】216. Draw polylines and polygons
function cv.polylines() Used to draw polygonal curves or polylines .
function cv.fillPoly() Used to draw one or more filled polygon areas .
function cv.fillConvexPoly() Used to draw a filled convex polygon .
The function prototype :
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
Parameter description :
- img: Input / output image , Allows single channel grayscale images or multi-channel color images
- pts: Polygon vertex coordinates , A two-dimensional Numpy List of arrays
- points: Polygon vertex coordinates , A two-dimensional Numpy Array
- isClosed: Closing sign ,True Represents a closed polygon ,False Indicates that the polygon is not closed
matters needing attention :
Pay special attention to polygon vertex coordinates pts The format of :
pts It's a list , The elements in the list are two-dimensional Numpy Array , Each element represents a set of vertex coordinates .
A two-dimensional Numpy The shape of the array is (m,2), Each row represents the coordinates of a vertex of the polygon (xi,yi), The data format should be integer .for example :
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 It's in the shape of (m,2) Two dimensions of Numpy Array .
For the function cv.fillConvexPoly, Use 2D Numpy Array points1 or points2 As a parameter .
For the function cv.polylines And cv.fillPoly, You can't directly put two dimensions Numpy Array points1 or points2 As a function parameter , Instead, use it as an element of the list , Such as : [points1]、[points2] or [points1, points2].function cv.polylines And cv.fillPoly You can draw or fill one or more polygons .
When the list pts There is only one element in , It's a two-dimensional Numpy Array time , Draw a polygon ;
When the list pts There are multiple elements in , That is, multiple two-dimensional Numpy Array time , Draw multiple polygons , Each two-dimensional Numpy The vertex coordinates in the array are treated as a polygon .The closing sign is True Draw a closed polygon , The line segment from the last vertex to the first vertex will be drawn ; The closing sign is False Draw a non closed polyline , There is no connection between the last vertex and the first vertex .
function cv.fillPoly And cv.fillConvexPoly You can draw filled polygons .
cv.fillConvexPoly Running speed ratio of cv.fillPoly Much faster , Especially suitable for filling convex polygons , Often used for contour processing .function cv.fillConvexPoly It can not only fill convex polygons , You can also fill any monotonic polygon that does not intersect itself , That is, the polygon whose contour intersects each horizontal line at most twice ( Although its top and / Or the bottom edge may be horizontal ).
This sentence comes from the official document , The contents in brackets “ Top and / Or the bottom edge may be horizontal ” Not easy to understand , You can combine routines 4.7 The bottom right figure understands . The author suggests that in addition to contour processing , Try not to use cv.fillConvexPoly To fill non convex polygons .
routine A4.7: Draw polylines and polygons
# A4.7 Draw polylines and polygons
img = np.ones((980, 400, 3), np.uint8)*224
img1 = img.copy()
img2 = img.copy()
img3 = img.copy()
img4 = img.copy()
# Polygon vertices
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)
# Draw polygon , Closed curve
pts1 = [points1] # pts1 Is a list of , The shape of the list element is (m,2) Of numpy Two dimensional array
cv.polylines(img1, pts1, True, (0,0,255)) # pts1 Is a list of
cv.polylines(img1, [points2, points3], 1, 255, 2) # You can draw multiple polygons
# Draw a polyline , The curve is not closed
cv.polylines(img2, [points1], False, (0,0,255))
cv.polylines(img2, [points2, points3], 0, 255, 2) # You can draw multiple polylines
# Draw filled polygons , Pay attention to the treatment of overlapping parts
cv.fillPoly(img3, [points1], (0,0,255))
cv.fillPoly(img3, [points2, points3], 255) # You can draw multiple filled polygons
# Draw a filled polygon , Pay attention to the overlapping part
cv.fillConvexPoly(img4, points1, (0,0,255))
cv.fillConvexPoly(img4, points2, 255) # You cannot draw polygons that have self intersections
cv.fillConvexPoly(img4, points3, 255) # You can draw concave polygons , But be careful with
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()

【 At the end of this section 】
Copyright notice :
[email protected] Original works , Reprint must be marked with the original link :(https://blog.csdn.net/youcans/article/details/125468148)
Copyright 2022 youcans, XUPT
Crated:2022-6-26
Welcome to your attention 『youcans Of OpenCV routine 200 piece 』 series , Ongoing update
Welcome to your attention 『youcans Of OpenCV Learning lessons 』 series , Ongoing update
210. There are so many holes in drawing a straight line ?
211. Draw vertical rectangle
212. Draw a slanted rectangle
213. Draw a circle
214. Detailed explanation of parameters for drawing ellipse
215. Draw approximate ellipses based on polylines
216. Draw polylines and polygons
边栏推荐
- ABAP-屏幕切换时,刷新上一个屏幕
- 如何写出好代码 - 防御式编程指南
- Connect the ABAP on premises system to the central inspection system for custom code migration
- Can I choose to open an account on Great Wall Securities? Is it safe?
- 【ROS进阶篇】第五讲 ROS中的TF坐标变换
- 雷神科技冲刺北交所,拟募集资金5.4亿元
- The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple
- Junda technology indoor air environment monitoring terminal PM2.5, temperature and humidity TVOC and other multi parameter monitoring
- [advanced ROS] lesson 5 TF coordinate transformation in ROS
- 使用 csv 导入的方式在 SAP S/4HANA 里创建 employee 数据
猜你喜欢
![[one day learning awk] function and user-defined function](/img/e1/a378211ef05fcc4d469363f3e509a7.png)
[one day learning awk] function and user-defined function

Implementation of wechat web page subscription message

"Qt+pcl Chapter 6" point cloud registration ICP Series 6

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

Tensorflow team: we haven't been abandoned

厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案

How to realize clock signal frequency division?

《性能之巅第2版》阅读笔记(五)--file-system监测

Wechat applet 02 - Implementation of rotation map and picture click jump
![[video memory optimization] deep learning video memory optimization method](/img/87/795429126aef284d55d2217f393ecb.png)
[video memory optimization] deep learning video memory optimization method
随机推荐
Rhcsa fourth day operation
Wechat official account subscription message Wx open subscribe implementation and pit closure guide
ABAP-屏幕切换时,刷新上一个屏幕
Tableapi & SQL and MySQL data query of Flink
Research on manually triggering automatic decision of SAP CRM organization model with ABAP code
【一天学awk】条件与循环
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
SAP s/4hana: one code line, many choices
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)
ThinkPHP进阶
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
三星率先投产3nm芯片,上海应届硕士生可直接落户,南开成立芯片科学中心,今日更多大新闻在此...
微信小程序02-轮播图实现与图片点击跳转
phpcms后台上传图片按钮无法点击
厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
Deep operator overloading (2)
[cloud trend] new wind direction in June! Cloud store hot list announced
她就是那个「别人家的HR」|ONES 人物
雷神科技冲刺北交所,拟募集资金5.4亿元
Qt+pcl Chapter 6 point cloud registration ICP series 3