Ask Your Question
1

copy part of a image Mat to another one

asked 2013-10-03 01:19:16 -0600

nakano gravatar image

Hello,

Using the source code from this link (http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html) I have a CV_8UC3 mat with which contain a black background and the contour of my image (I've filled the contour using CV_FILLED parameter).

I would like to know it is it possible to copy/insert only the contour from my CV_8UC3 mat to the original image? (both image have the same size).

I've searched on the internet and tried function like copyTo(), ROI but without success.

I really thank you for your help

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2013-10-03 08:59:13 -0600

Michael Burdinov gravatar image

updated 2013-10-03 09:03:04 -0600

Many functions in OpenCV has 'mask' as one of their optional parameters. Mask is a binary image of the same size as image you are working with. The function will work only with pixels that are not zero in the mask. In your case you will need to do the following:

// create temporary image that will hold the mask
Mat mask_image( your_image.size(), CV_8U, Scalar(0));
// draw your contour in mask
drawContours(mask_image, contours, ind, Scalar(255), CV_FILLED);
// copy only non-zero pixels from your image to original image
your_image.copyTo(original_image, mask_image);

In documentation of copyTo you can see both ways of its use (with and without mask).

edit flag offensive delete link more

Comments

1

Thank you for your reply.

I think I'm pretty close but the result isn't as expected. When I launch my application my CV_UC3 image overlap my original image without doing any mask (I'm still seeing a full black screen with my filled rectangle).

Here is my source code:

Mat original_image = originalMat.clone(); //originalMatis the Mat image received from OpenCV Video Camera Manager

Mat drawing = Mat::zeros( original_image.size(), CV_8UC3 );//draw contours of my rectangle in red

drawContours( drawing, contours, maxAreaIdx, Scalar(255), -1, 8, hierarchy, 0, cv::Point() );

Mat mask_image( original_image.size(), CV_8U, Scalar(0));

drawContours(mask_image, contours, maxAreaIdx, Scalar(255), CV_FILLED);

drawing.copyTo(original_image, mask_image);

//display original_image

nakano gravatar imagenakano ( 2013-10-04 00:31:44 -0600 )edit

First, it is weird that your code is not updating original_image. I tested your code and it is working.

Second, why are you drawing your contour on 'drawing' image and than copying it to 'original_image'? If you want it on 'original_image' you should do so directly, i.e.

drawContours( original_image, contours, maxAreaIdx, Scalar(255), CV_FILLED);

Michael Burdinov gravatar imageMichael Burdinov ( 2013-10-06 06:45:46 -0600 )edit

I'm trying to do it this way (not drawing in the original image directly) because when I do it this way, I can set any color the result will be always the same : the filled color is always white.

I think that thanks to tutorial my method until findContour() should be fine but the next part of my code (detecting edge) may be wrong/ not correct which explain that my filled color is always white.

I've heard in another thread I made someone talking about dilate and convexHull method but I don't get how to use it (I've already saw the referential page), I guess I'll take more time for studying these method and search the best way for detecting image contour.

nakano gravatar imagenakano ( 2013-10-06 20:21:30 -0600 )edit

Question Tools

Stats

Asked: 2013-10-03 01:19:16 -0600

Seen: 49,529 times

Last updated: Oct 03 '13