OpenCV - 如何确定给定点是否在轮廓内?

OpenCV - How to determine whether a given point is within a contour?

给定一个随机轮廓,如何判断给定的输入点是否位于轮廓内?很抱歉,如果它有一个简单的解决方案,但我无法弄清楚。

我的一个想法是使用线方程,连接点并查看它是更大还是更小,等等。但这并没有帮助我,因为它取决于线的位置。

您可以使用 OpenCV 找到此问题的完整解决方案 here

  /// Get the contours
  vector<vector<Point> > contours; vector<Vec4i> hierarchy;
  Mat src_copy = src.clone();

  findContours( src_copy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);

  /// Calculate the distances to the contour
  Mat raw_dist( src.size(), CV_32FC1 );

  for( int j = 0; j < src.rows; j++ )
     { for( int i = 0; i < src.cols; i++ )
          { raw_dist.at<float>(j,i) = pointPolygonTest( contours[0], Point2f(i,j), true ); }
     }