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

本文详细介绍了如何使用OpenCV的pointPolygonTest接口来判断一个点与轮廓之间的位置关系,包括点在轮廓内部、外部还是轮廓上的情况,并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

理想很丰满,现实很骨感,常常以为实现一个算法不是很难,但在现实中场景更加复杂,硬件限制,速度要求,好吧,只能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,就整理一下啦(≧▽≦)/啦啦啦

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值