Ask Your Question
1

Insert binary threshold image (CV_8UC1) into a ROI of a coloured mat (CV_8UC4)?

asked 2015-10-04 05:04:52 -0500

Grigory Ptashko gravatar image

updated 2015-10-06 10:34:20 -0500

Hello.

I've got a sequence of images of type CV_8UC4. It is of HD size 1280x720. I'm executing the bgfg segmentation (MOG2 specifically) on a ROI of the image. After the algo finished I've got the binary image of the size of ROI and of type CV_8UC1. I want to insert this binary image back to the original big image. Hwo can I do this?

Here's what I'm doing (the code is simplified for the sake of readability):

// cvImage is the big Mat coming from outside
cv::Mat roi(cvImage, cv::Rect(200, 200, 400, 400));
mog2 = cv::createBackgroundSubtractorMOG2();
cv::Mat fgMask;
mog2->apply(roi, fgMask); // Here the fgMask is the binary mat which corresponds to the roi size

So, how can insert the fgMask back to the original image? Hwo to do this CV_8UC1 -> CV_8UC4 conversion only for the ROI?

Thank you.

edit retag flag offensive close merge delete

Comments

you will need to convert your CV_8UC1 ROI image to CV_8UC3/4 before you insert it back into the original image. In order to achieve this use the .convertTo() functionality.

theodore gravatar imagetheodore ( 2015-10-04 05:30:05 -0500 )edit

maybe you don't want to 'insert' it but use it as a mask, like src.copyTo(dst, mask)

berak gravatar imageberak ( 2015-10-04 06:48:55 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2015-10-06 15:52:22 -0500

Grigory Ptashko gravatar image

I've got the answer here http://stackoverflow.com/questions/32.... It was exactly what I was asking for.

Thank you everybody.

edit flag offensive delete link more

Comments

2

IMHO you got enough information here but you did not understand!

sturkmen gravatar imagesturkmen ( 2015-10-06 19:12:10 -0500 )edit
1

That solutions is exactly like what we proposed, it is the code handled by that conversion parameter. Also I personally do not prefer people to link solutions. At least have the decency to add the complete code here, so that when SO disappears for some reason, this topic still has your solution.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-07 02:46:16 -0500 )edit
2

answered 2015-10-05 06:19:00 -0500

updated 2015-10-05 06:19:54 -0500

A simple code solution in C++. If you want only a 3 channel, then remove the fourth channel everywhere.

// Paste the single layer image into a multi layer image
Mat fgMask_color(fgMask.rows, fgMask.cols, CV_8UC4);
Mat in[] = { fgMask.clone(), fgMask.clone(), fgMask.clone(), fgMask.clone() };
int from_to[] = { 0,0, 1,1, 2,2, 3,3 };
mixChannels( in, 4, &fgMask_color, 1, from_to, 4 );
// Paste it back to the original image
fgMask_color.copyTo(cvImage(roi));
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-10-04 05:04:52 -0500

Seen: 718 times

Last updated: Oct 06 '15