OpenCV python:如何判断点和轮廓的位置 (pointPolygonTest)

理想很丰满,现实很骨感,常常以为实现一个算法不是很难,但在现实中场景更加复杂,硬件限制,速度要求,好吧,只能push LZ想方法解决,也就是说进入了PDCA(PLAN,DO,CHECK,ACTION)的模式。

下面介绍一个OpenCV的小接口,用来判断点和contour的位置关系

def pointPolygonTest(contour, pt, measureDist): # real signature unknown; restored from __doc__
    """
    pointPolygonTest(contour, pt, measureDist) -> retval
    .   @brief Performs a point-in-contour test.
    .   
    .   The function determines whether the point is inside a contour, outside, or lies on an edge (or
    .   coincides with a vertex). It returns positive (inside), negative (outside), or zero (on an edge)
    .   value, correspondingly. When measureDist=false , the return value is +1, -1, and 0, respectively.
    .   Otherwise, the return value is a signed distance between the point and the nearest contour edge.
    .   
    .   See below a sample output of the function where each image pixel is tested against the contour:
    .   
    .   ![sample output](pics/pointpolygon.png)
    .   
    .   @param contour Input contour.
    .   @param pt Point tested against the contour.
    .   @param measureDist If true, the function estimates the signed distance from the point to the
    .   nearest contour edge. Otherwise, the function only checks if the point is inside a contour or not.
    """
    pass

contour很好理解,使用接口cv2.findContours找到对应图片的contour,或者其他方式也可以

point,需要准备好需要的点,需要tuple类型

measureDist:measureDist如果为true,则该函数估计从点到最近的轮廓边缘的有符号距离。 否则,该功能仅检查该点是否在轮廓内。

这个按照我们的需要设置为False即可,当measureDist设置为false时,若返回值为+1,表示点在轮廓内部,返回值为-1,表示在轮廓外部,返回值为0,表示在轮廓上。

代码示例:

flag = cv2.pointPolygonTest(contours[i], test_point, False)

刚做完了一个demo,就整理一下啦(≧▽≦)/啦啦啦

发布了349 篇原创文章 · 获赞 237 · 访问量 65万+

猜你喜欢

转载自blog.csdn.net/Felaim/article/details/105122757