Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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);

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).