Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

this should work:

    Mat m = imread("green.png",-1);      // -1 for "load as is", (keep alpha !)
    Mat c[4]; split(m,c);                // seperate channels
    Mat cs[4] = { c[0],c[2],c[1],c[3] }; // swap red & green
    Mat m2; merge(cs,4,m2);              // glue together again
    imwrite("red.png",m2);               // save

image description

actually, your image is not 'distorted', the green one already looks like this, if you import it into opencv as a 3 channel image. everything will be fine, if you care to load & process the alpha channel as well, this should work:

    Mat m = imread("green.png",-1);      // -1 for "load as is", (keep alpha !)
    Mat c[4]; split(m,c);                // seperate channels
    Mat cs[4] = { c[0],c[2],c[1],c[3] }; // swap red & green
    Mat m2; merge(cs,4,m2);              // glue together again
    imwrite("red.png",m2);               // save

image description