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

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.

使用例子如下:

扫描二维码关注公众号,回复: 3442613 查看本文章
  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

猜你喜欢

转载自blog.csdn.net/Touch_Dream/article/details/82950170
今日推荐