Issue with drawContours OpenCV c++

霸气de小男生 提交于 2019-12-06 04:20:30

The function drawContours() expects to receive a sequence of contours, each contour being a "vector of points".

The expression cv::Mat(hull) you use as a parameter returns the matrix in incorrect format, with each point being treated as a separate contour -- that's why you see only a few pixels.

According to the documentation of cv::Mat::Mat(const std::vector<_Tp>& vec) the vector passed into the constructor is used in the following manner:

STL vector whose elements form the matrix. The matrix has a single column and the number of rows equal to the number of vector elements.

Considering this, you have two options:

  • Transpose the matrix you've created (using cv::Mat::t()
  • Just use a vector of vectors of Points directly

The following sample shows how to use the vector directly:

cv::Mat output_image; // Work image

typedef std::vector<cv::Point> point_vector;
typedef std::vector<point_vector> contour_vector;

// Create with 1 "contour" for our convex hull
contour_vector hulls(1);

// Initialize the contour with the convex hull points
cv::convexHull(cv::Mat(contour), hulls[0]);

// And draw that single contour, filled
cv::drawContours(output_image, hulls, 0, 255, -1);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!