cv2 画多边形不填充_OpenCV python: 任意多边形填充和凸多边形填充(fillPoly和fillConvexPoly的区别,有图有真相!)...
我們經(jīng)常會使用contour或者自己圈出來的區(qū)域填上對應(yīng)的值或者,所以經(jīng)常會使用這樣兩個函數(shù):fillPoly和fillConvexPoly,至于這兩個函數(shù)有什么區(qū)別呢?小伙伴從題目中就可以看出來了,一個是任意多邊形進(jìn)行填充(有可能是非凸的),一個是對凸多邊形進(jìn)行填充。
兩個函數(shù)的使用方法和說明:
官方文檔:函數(shù)fillConvexPoly繪制一個填充的凸多邊形。 該函數(shù)比函數(shù)fillPoly快得多。 它不僅可以填充凸多邊形,而且可以填充任何不具有自相交的單調(diào)多邊形,即,其輪廓與每個水平線(掃描線)相交的多邊形最多兩次(盡管其最頂部和/或底部邊緣可能是 水平)。
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ū)域。 該功能可以填充復(fù)雜的區(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
用起來也相當(dāng)方便了,寫了個小例子
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()
下載了一顆五角星,這個效果會比較明顯:
我們先進(jìn)行二值化:
使用cv2.fillPoly進(jìn)行填充:
使用cv2.fillConvexPoly進(jìn)行填充:
這個例子應(yīng)該相當(dāng)明顯了(≧▽≦)/啦啦啦!
總結(jié)
以上是生活随笔為你收集整理的cv2 画多边形不填充_OpenCV python: 任意多边形填充和凸多边形填充(fillPoly和fillConvexPoly的区别,有图有真相!)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 53岁“最帅康熙”考公上岸?本人回应:我
- 下一篇: ios模拟器装ipa包_在iOS开发的时