Ask Your Question
2

How to change transparency of an 4 channels image?

asked 2015-10-13 22:17:48 -0600

realkill gravatar image

Give a alpha degree and set image transparency. I didn't find the API to do this.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-10-13 23:09:08 -0600

updated 2015-11-03 04:10:08 -0600

you can set transparency by changing alpha channel values as shown with the code below

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;

int main( int, char** argv )
{
    Mat image(400, 400, CV_8UC4, Scalar(0, 0, 200));
    Mat bgra[4];
    split(image,bgra);

    rectangle(bgra[3],Rect(100,100,200,200),Scalar(255),-1);

    merge(bgra,4,image);
    imwrite("transparent1.png",image);

    rectangle(bgra[3],Rect(150,150,100,100),Scalar(127),-1);
    merge(bgra,4,image);
    imwrite("transparent2.png",image);

    bgra[3] = bgra[3] * 0.5; // here you can change transparency %50
    // bgra[3] = bgra[3] + 50 // you can add some value 
    // bgra[3] = bgra[3] - 50 // you can subtract some value

    merge(bgra,4,image);
    imwrite("transparent3.png",image);

    waitKey(0);
    return(0);
}

result images :

image description image description image description

also take a look at your other question

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-10-13 22:17:48 -0600

Seen: 17,023 times

Last updated: Nov 03 '15