How to Combine Image Channels

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
cthomas
Posts: 10
Joined: 2016-01-18T10:55:03-07:00
Authentication code: 1151

How to Combine Image Channels

Post by cthomas »

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.

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 );
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
Last edited by cthomas on 2016-01-20T08:36:09-07:00, edited 1 time in total.
Chris Thomas
Visualisation Engineer
SOASTA Inc.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to Combine Image Channels

Post by fmw42 »

In command line, you would use -compose copy_opacity -composite to put an image into the alpha channel. Sorry, I do not know how to do that in your API. But I would suspect it is done through the use of the composite equivalent in your API.
cthomas
Posts: 10
Joined: 2016-01-18T10:55:03-07:00
Authentication code: 1151

Re: How to Combine Image Channels

Post by cthomas »

"In command line, you would use -compose copy_opacity -composite to put an image into the alpha channel. Sorry, I do not know how to do that in your API. But I would suspect it is done through the use of the composite equivalent in your API."

Please see above, it shows how to do the operation in the c++ API.

To be clear, this is combining the RGB of image A, with image B supplying the Alpha channel, to produce a new image C.
Chris Thomas
Visualisation Engineer
SOASTA Inc.
Post Reply