Ask Your Question
1

OpenCV how to Retain Icon Property

asked 2013-10-01 03:11:52 -0600

Haris gravatar image

updated 2020-10-24 02:16:50 -0600

I have an icon image like this,

Source:-

image description

And I need to convert this image to red I am using the following code.

Mat src=imread("green_button.png");
   for(int i=0;i<src.rows;i++){
   for(int j=0;j<src.cols;j++){
    Vec3b pixel = src.at<Vec3b>(i, j);
     int tmp=pixel[1];
     pixel[1]=pixel[2];
     pixel[2]=tmp;
     src.at<Vec3b>(i,j) = pixel;
   }
   }
  imwrite("red_button.png",src);

But the result look like distorted at side.

Result:-

image description

I am getting the distorted icon image even if I directly save the image after imread() . How can I retain the result as source while using OpenCV ?. Actually I want use these icon for my android application.

Thanks....

edit retag flag offensive close merge delete

Comments

the green one, that's your actual image ? it seems to have an alpha channel

berak gravatar imageberak ( 2013-10-01 03:28:33 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-10-01 03:41:39 -0600

berak gravatar image

updated 2013-10-01 04:13:07 -0600

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

edit flag offensive delete link more

Comments

Hi thanks for reply. Really it's a new information to me.

Haris gravatar imageHaris ( 2013-10-01 04:20:17 -0600 )edit

Question Tools

Stats

Asked: 2013-10-01 03:11:52 -0600

Seen: 1,654 times

Last updated: Oct 01 '13