opencv 函数pointPolygonTest 检测一个点是否在多边形内

85 篇文章 1 订阅

opencv函数

pointPolygonTest:

C++: double pointPolygonTest(InputArray contour, Point2f pt, bool measureDist)

Performs a point-in-contour test.

C++: double pointPolygonTest(InputArray contour, Point2f pt, bool measureDist)

Python: cv2.pointPolygonTest(contour, pt, measureDist) → retval

C: double cvPointPolygonTest(const CvArr* contour, CvPoint2D32f pt, int measure_dist)

Python: cv.PointPolygonTest(contour, pt, measure_dist) → float

Parameters:
  • contour – Input contour.
  • pt – Point tested against the contour.
  • 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.

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.

使用例子如下:

 

  1. /// 得到轮廓

  2. std::vector<std::vector<cv::Point> > contours;

  3. std::vector<cv::Vec4i> hierarchy;

  4. cv::Mat src; //src为图像

  5.  
  6. //contours为函数findContours计算得到的轮廓点分布值

  7. cv::findContours( src_copy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);

  8.  
  9. // 计算到轮廓的距离

  10. cv::Mat raw_dist( src.size(), CV_32FC1 );

  11.  
  12. for( int j = 0; j < src.rows; j++ ){

  13. for( int i = 0; i < src.cols; i++ ){

  14. raw_dist.at<float>(j,i) = cv::pointPolygonTest( contours[0], Point2f(i,j), true );

  15. }


--------------------- 本文来自 周作才 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/xiaxiazls/article/details/48392875?utm_source=copy

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值