日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

cv2 画多边形不填充_OpenCV python: 任意多边形填充和凸多边形填充(fillPoly和fillConvexPoly的区别,有图有真相!)...

發(fā)布時間:2025/4/5 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cv2 画多边形不填充_OpenCV python: 任意多边形填充和凸多边形填充(fillPoly和fillConvexPoly的区别,有图有真相!)... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我們經常會使用contour或者自己圈出來的區(qū)域填上對應的值或者,所以經常會使用這樣兩個函數(shù):fillPoly和fillConvexPoly,至于這兩個函數(shù)有什么區(qū)別呢?小伙伴從題目中就可以看出來了,一個是任意多邊形進行填充(有可能是非凸的),一個是對凸多邊形進行填充。

兩個函數(shù)的使用方法和說明:

官方文檔:函數(shù)fillConvexPoly繪制一個填充的凸多邊形。 該函數(shù)比函數(shù)fillPoly快得多。 它不僅可以填充凸多邊形,而且可以填充任何不具有自相交的單調多邊形,即,其輪廓與每個水平線(掃描線)相交的多邊形最多兩次(盡管其最頂部和/或底部邊緣可能是 水平)。

def fillConvexPoly(img, points, color, lineType=None, shift=None): # real signature unknown; restored from __doc__

"""

fillConvexPoly(img, points, color[, lineType[, shift]]) -> img

. @brief Fills a convex polygon.

.

. The function cv::fillConvexPoly draws a filled convex polygon. This function is much faster than the

. function #fillPoly . It can fill not only convex polygons but any monotonic polygon without

. self-intersections, that is, a polygon whose contour intersects every horizontal line (scan line)

. twice at the most (though, its top-most and/or the bottom edge could be horizontal).

.

. @param img Image.

. @param points Polygon vertices.

. @param color Polygon color.

. @param lineType Type of the polygon boundaries. See #LineTypes

. @param shift Number of fractional bits in the vertex coordinates.

"""

pass

官方文檔:函數(shù)fillPoly填充由多個多邊形輪廓所界定的區(qū)域。 該功能可以填充復雜的區(qū)域,例如,帶有孔的區(qū)域,具有自相交的輪廓(部分輪廓)等等。

def fillPoly(img, pts, color, lineType=None, shift=None, offset=None): # real signature unknown; restored from __doc__

"""

fillPoly(img, pts, color[, lineType[, shift[, offset]]]) -> img

. @brief Fills the area bounded by one or more polygons.

.

. The function cv::fillPoly fills an area bounded by several polygonal contours. The function can fill

. complex areas, for example, areas with holes, contours with self-intersections (some of their

. parts), and so forth.

.

. @param img Image.

. @param pts Array of polygons where each polygon is represented as an array of points.

. @param color Polygon color.

. @param lineType Type of the polygon boundaries. See #LineTypes

. @param shift Number of fractional bits in the vertex coordinates.

. @param offset Optional offset of all points of the contours.

"""

pass

用起來也相當方便了,寫了個小例子

import cv2

img_star = cv2.imread("../test_imgs/star.jpeg")

img_star_gray = cv2.cvtColor(img_star, cv2.COLOR_BGR2GRAY)

kernel =cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))

retval, dst = cv2.threshold(img_star_gray, 0, 255, cv2.THRESH_OTSU)

cv2.imshow("bin: ", dst)

cv2.waitKey(0)

contours, _ = cv2.findContours(dst, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cv2.fillPoly(img_star, [contours[0]], (0,255,255))

#cv2.fillConvexPoly(img_star, contours[0], (0,255,255))

cv2.imshow("dst: ", img_star)

cv2.waitKey(0)

cv2.destroyAllWindows()

下載了一顆五角星,這個效果會比較明顯:

我們先進行二值化:

使用cv2.fillPoly進行填充:

使用cv2.fillConvexPoly進行填充:

這個例子應該相當明顯了(≧▽≦)/啦啦啦!

總結

以上是生活随笔為你收集整理的cv2 画多边形不填充_OpenCV python: 任意多边形填充和凸多边形填充(fillPoly和fillConvexPoly的区别,有图有真相!)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。