How to Combine Image Channels
Posted: 2016-01-18T11:57:57-07:00
Hey, I had something of an issues with the API's annotate method, with a black border appearing around its text, when it was composited over another images. As a result, I decided to create my own image, using the text as an alpha channel. This was a real pain, as how to combine channels in this API (lordy knows I searched the internet...). So here was the plan.
* Create a black opaque image
* Write the text onto it in white.
This gives us our alpha channel. Next...
* Create an opaque white image
* Use this white image as the R,G and B of a new combined image. And use the Black/White text image as its alpha.
Here's how I did this.
later I discovered that if you do not supply each channel of the image to be composited, separately, the combineImages method, uses (guessing) the red channel from each image (it could be the luma average of all?). So in the example above, in the imagesList.push, you'll see .separate() being used to do this...
Hope this is of help to someone
* Create a black opaque image
* Write the text onto it in white.
This gives us our alpha channel. Next...
* Create an opaque white image
* Use this white image as the R,G and B of a new combined image. And use the Black/White text image as its alpha.
Here's how I did this.
Code: Select all
Magick::Image txtImage;
txtImage.magick("RGBA");
Magick::Image whiteImage;
whiteImage.magick("RGBA");
std::vector<Magick::Image> imagesList; // The list of images that will form the new combined image
imagesList.push_back(whiteImage.separate(Magick::RedChannel)); // Will be used for R
imagesList.push_back(whiteImage.separate(Magick::GreenChannel)); // Will be used for G
imagesList.push_back(whiteImage.separate(Magick::BlueChannel)); // Will be used for B
imagesList.push_back(txtImage); // Will be used for Alpha
Magick::Image combinedImages;
combinedImages.magick("RGBA");
Magick::combineImages( &combinedImages, imagesList.begin(), imagesList.end( ), Magick::AllChannels );
Hope this is of help to someone