简体   繁体   中英

Converting contours from vector of cv::Point in cv::Mat

Suppose my objective is to extract contours from an initial image. I performed this operations using OpenCV:

cv::Mat gray, edges;
cv::cvtColor(image, gray, CV_BGRA2GRAY);
cv::Canny(gray, edges, 100, 300, 3);

std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(edges, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

Now the contour is contained in an array of Points. I want to rebuild a cv::Mat structure having the same dimensions as the initial image, so as to emphasize the contour by superimposing it to the image.

In particular, I am not interested in drawing immediately the contour. I will perform the following operations:

  • extract the contour
  • dilate the contour
  • superimpose the contour on the image (as you do in edge sharpening)

Thus, the contour has to be a matrix of the same size of the input image.

How can I do that?

Thanks in advance.

You can re-built a Matrix

Mat image_contours_grayscale = Mat::zeros(gray.size(),gray.type());
Scalar color(255);
drawContours( image_contours_grayscale, contours,-1,color,1);

I'm not sure if drawContours works with grayscale image, but if it doesn't, you can try this

Mat image_contours_color = Mat::zeros(image.size(),image.type());
Scalar color(255,255,255);
drawContours( image_contours_color, contours,-1,color,1);
Mat image_contours_grayscale;
cv::cvtColor(image_contours_color, image_contours_grayscale, CV_BGRA2GRAY);

image_contours_grayscale should be a grayscale image, all black with the contours in white.

Let me know if this works, I didn't have the occasion to test this solution !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM