Grigory
Grigory

Reputation: 1024

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

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

Upvotes: 0

Views: 683

Answers (1)

Miki
Miki

Reputation: 41765

You need to make fgMask a 4 channel image:

Mat4b fgMask4ch;
cvtColor(fgMask, fgMask4ch, COLOR_GRAY2BGRA);

and then copy this into the original cvImage at the correct position, given by roi:

fgMask4ch.copyTo(roi);

Upvotes: 0

Related Questions