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!

Share:
10,199
Michael Litvin
Author by

Michael Litvin

Updated on June 04, 2022

Comments

  • Michael Litvin
    Michael Litvin almost 2 years

    Both Mat objects contain (different) images. I want to copy mSnapshot over mRgba. I tried these (separately), but none of them seem to change mRgba:

    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.