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

我们经常会使用contour或者自己圈出来的区域填上对应的值或者,所以经常会使用这样两个函数:fillPoly和fillConvexPoly,至于这两个函数有什么区别呢?小伙伴从题目中就可以看出来了,一个是任意多边形进行填充(有可能是非凸的),一个是对凸多边形进行填充。

两个函数的使用方法和说明:

官方文档:函数fillConvexPoly绘制一个填充的凸多边形。 该函数比函数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

官方文档:函数fillPoly填充由多个多边形轮廓所界定的区域。 该功能可以填充复杂的区域,例如,带有孔的区域,具有自相交的轮廓(部分轮廓)等等。

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进行填充:
在这里插入图片描述

这个例子应该相当明显了(≧▽≦)/啦啦啦!

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值