How to change transparency of an 4 channels image?
Give a alpha degree and set image transparency. I didn't find the API to do this.
Give a alpha degree and set image transparency. I didn't find the API to do this.
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 :
also take a look at your other question
Asked: 2015-10-13 22:17:48 -0600
Seen: 16,938 times
Last updated: Nov 03 '15
Problem Reading PNG with Transparency layer
MatchTemplate on a single color [closed]
Image Background Transparency - OpenCV
How to set transparent background to grabcut output image in OpenCV (C++)?
Transparency (alpha) handling in cascade training?
Converting transparent pixels to white after mask proccess
Unexpected results while copying transparent image over another image. [closed]
How to show transparent images
Bend overlay image to the curvature of underlay image using displacement map filter