0

I've mRgba Matrix and a Rect r (something recognized in the frame)

I want a sub-matrix of this part of the frame which is defined by the Rect r.

when I use it like this:

sub = mRgba.submat(r);

I get the right sub-matrix, but I've a problem with the next steps, I want to change this part of the frame and then copy it back to the original.

For example:

 Imgproc.cvtColor(sub, sub, Imgproc.COLOR_RGBA2GRAY, 1); //make it gray
 Imgproc.cvtColor(sub, sub, Imgproc.COLOR_GRAY2RGBA, 4); //change to rgb

How can I copy this changed sub-matrix back to the original. or how can I get/create a Mask same size as mRgba with all zeros except the Rect r part?

RobinHood
  • 10,897
  • 4
  • 48
  • 97
ddd
  • 481
  • 6
  • 17

2 Answers2

0

Your code does not work as you expected because it is impossible to change number of colors in-place. You need a temporary matrix to make it work:

Mat tmp;
Imgproc.cvtColor(sub, tmp, Imgproc.COLOR_RGBA2GRAY); //make it gray
Imgproc.cvtColor(tmp, sub, Imgproc.COLOR_GRAY2RGBA); //change to rgb
Andrey Kamaev
  • 29,582
  • 6
  • 94
  • 88
  • hi.. thx for your answer.. actually that seems to be no problem and/or changes nothing. is the only way to make a mask step-by-step with for loops? – ddd Jun 06 '12 at 15:52
  • sub.copyTo(mRgba.submat(r)); ok this seems to do the trick :) it copies the changed subpicture/matrix back in the region of the original image. – ddd Jun 06 '12 at 18:57
0
sub = mRgba.submat(r);

Imgproc.cvtColor(sub, sub, Imgproc.COLOR_RGBA2GRAY, 1); //make it gray
Imgproc.cvtColor(sub, sub, Imgproc.COLOR_GRAY2RGBA, 4); //change to rgb

sub.copyTo(mRgba.submat(r));

ok this seems to do the trick :) it copies the changed subpicture/matrix back in the region of the source.. (what is normally done with setROI and copyto)

ddd
  • 481
  • 6
  • 17
  • Another thing to keep in mind, if you want to copy two submats `a`, `b` from different sources `A`, `B` it's important that `A` and `B` have the same type (e.g `CV_32F`) – auraham Aug 01 '13 at 20:39