OpenCV for Android: how to copy a Mat object (image) to another Mat?
10,199
To copy an image, you should use the clone()
member function like this:
capture.retrieve(mSnapshot, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
mRgba = mSnapshot.clone();
Another note, OpenCV stores information in BGR order; therefore, your line:
mRgba.setTo(new Scalar(0,0,0,255));
Edited for clarity : This command is setting each pixel to (0, 0, 0, 255), so channels 1-3 are set to 0, and channel 4 (alpha) is set to 255. What if you tried this:
mRgba.setTo(new Scalar(0, 255, 0, 0)); // should be set to green.
Also, note that you can only use setTo
once the matrix is allocated.
Hope that helps!
Author by
Michael Litvin
Updated on June 04, 2022Comments
-
Michael Litvin almost 2 years
Both Mat objects contain (different) images. I want to copy
mSnapshot
overmRgba
. I tried these (separately), but none of them seem to changemRgba
:mSnapshot.assignTo(mRgba); mSnapshot.copyTo(mRgba); mRgba = mSnapshot;
This throws an exception:
mRgba.setTo(mSnapshot);
And this does work, and sets
mRgba
to be a completely black image:mRgba.setTo(new Scalar(0,0,0,255));
What am I missing?
[Edit] The source files and exception can be found here.