Ask Your Question

Copy Mat at Contour/Mask into another Mat.

asked 2018-09-21 09:22:56 -0500

LuisK gravatar image

Hello guys.

Is there a possibility, to copy a mat at a pixels bounded by a specific contour (not it´s boundRect!) resp. a binary Mask into another mat?

Thanks for helping.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2018-09-21 09:32:07 -0500

berak gravatar image

updated 2018-09-21 10:10:17 -0500

in short - NO. only rectangular areas can be copied.

but , what you can do is: make a mask from your contour, and mask out (blacken or such) anything inside the boundingRect but outside the contour:

Rect roi = boundingRect(contours[i])
Mat mask = Mat::zeros(roi.size(), img.type());
drawContours(mask,contours,i,Scalar::all(255), -1); // -1 == filled
Mat final;
img(roi).copyTo(final, mask);
edit flag offensive delete link more


The issue is, that there must be no black areas in the image, i wanna "copy" the contour in. I really want only the contour to be put in that image.

LuisK gravatar imageLuisK ( 2018-09-21 09:51:10 -0500 )edit

you obviously did not try it.

it will only copy, wher the mask in ON.

if it's still unclear -- the problem is in your question, then..

berak gravatar imageberak ( 2018-09-21 10:11:07 -0500 )edit

Thanks mate, your edits really helped me out! Thank you very much!

LuisK gravatar imageLuisK ( 2018-09-21 10:37:32 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-09-21 09:22:56 -0500

Seen: 368 times

Last updated: Sep 21 '18