在opencv中,如何获得contour所包围的范围内的值

如果你想使用opencv区分边界的话,可以使用findContours,但是在确认边界后,

你又想知道边界里面或者外面是什么,你可以使用pointPolygonTest方法来判断。

如下例子:

  cv::Point2f findPointInContour(cv::Rect& rect, std::vector< cv::Point > contour)
  {
    cv::Point2f onTheEdge1;
    int where = 0;

    for (int i = rect.x; i < rect.x + rect.width; i++) {
      for (int j = rect.y; j < rect.y + rect.height; j++) {
        where = (int) (cv::pointPolygonTest(contour, cv::Point2f(i, j), false));

        //cout << "x = " << i << " y = " << j << "distance " << where << endl;
        if (where > 0) { //inside
          return cv::Point2f(i, j);
        } else if (where == 0) { //on the edge
          onTheEdge1 = cv::Point2f(i, j);
        }
      }
    }

    return onTheEdge1;
  }


你可能感兴趣的:(C/C++/Linux)