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

懵懂的女人 提交于 2020-04-30 07:36:27

问题


Given a random contour, how can I say if a given input point lies within the contour or not? I am sorry if it has a simple solution, but I am unable to figure it out.

One idea I had was to use equation of lines, connect points and see if it is greater or smaller, etc. But that doesn't get me anywhere, as it depends on the position of line.


回答1:


You can find a full solution to this problem using 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 ); }
     }


来源:https://stackoverflow.com/questions/29982971/opencv-how-to-determine-whether-a-given-point-is-within-a-contour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!