0

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.

Grigory
  • 992
  • 2
  • 18
  • 34

1 Answers1

0

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);
Miki
  • 40,887
  • 13
  • 123
  • 202