First time here? Check out the FAQ!

Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered Sep 21 '18

berak gravatar image

in short - NO. only rectangular areas can be copied. (it's a silly demand, if you think of it)

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);
click to hide/show revision 2
No.2 Revision

in short - NO. only rectangular areas can be copied. (it's a silly demand, if you think of it)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);
click to hide/show revision 3
No.3 Revision

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);
binary_or(img(roi), mask, final); // EDIT !
click to hide/show revision 4
No.4 Revision

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;
binary_or(img(roi), mask, final); // EDIT !
img(roi).copyTo(final, mask);