Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

first of all, transparency is not achieved by subtraction, but by setting values in the alpha channel.

//Mat mRgb = imread("sqr.png");

// first, split it into seperate channels:
Mat channels[4];
split(mRgb,channels);

// then calculate a mask, that will serve as an alpha channel:
Scalar min_color(0,0,0);
Scalar max_color(255,255,0);
// i know, the above looks counter-intuitive, 
// but the alpha chan will actually contain 'opacity' values, (so 0 is transparent)
inRange(mRgb, min_color,  max_color, channels[3]);

// finally merge them into a 4channel img with transparency: 
Mat rgba;
cv::merge(channels,4,rgba);

//imwrite("myrgba.png",rgba);