I am switching from IDL (from exelis) to Mathematica and want to do the following:

  1. thersholding a image (all values above a certain threshold are set 1, the rest is 0 )
  2. colorizing the thresholded image from 1. with a specific color corresponding to a specific color table.
  3. Superposing a certain number of thresholded and colorized images from 2. on top each other (whereby all colors should be seen, the color with the
    highest value from the corresponding color table should be on the top)

When I do this with IDL and superpose 100 consecutive images, I get the following result:

enter image description here

To receive the same results with Mathematica I did the following:

For 1.:

inputImage = Import@"https://i.sstatic.net/ruNFL.png";
threshold = 0.18;
binImage = Binarize[inputImage, threshold]

This works.

For 2. I tried:

imageOut = Colorize[binImage, 
           ColorRules -> {1 -> RGBColor[255, 0, 0], _ -> Black}]

This works and produces red pixels for pixel above threshold, the rest is set to 0:

Mathematica graphics

What I would like to is: selecting a certain color table, e.g. ColorData["TemperatureMap"] and pick a certain value from this color table and colorize binImage with this value (between 0 and 255).

Also (for later) I would like to be able to produce my own color table corresponding to certain RGB colors.

For 3.: I tried ImageCompose, Overlap, but in both cases only the last used image is resulting.

I would be very happy, if somebody could give me a hint on how to continue.

PS: 3 gray-scale images as an example are attached (threshold can be 0.18):

enter image description here

enter image description here

enter image description here

  • $\begingroup$ Dear Öska, that you very much for you help. I have tested the code with just two images (one red, one green). The resulting image should overlap the red image pixels by the green image pixels or vice versa. ImageAdd acts different: ImageAdd gives an image in which each pixel is the sum of the corresponding pixels in the single images. In IDL I used imageOut=image1 > image2; $\endgroup$
    – mrz
    Commented Dec 2, 2014 at 15:03

2 Answers 2


I guess the crucial point in your question is how to combine the images. When I look at the IDL output image, I guess that what IDL does is to assume that black is transparent when combining two images.

It looks to me as if the red points are completely drawn over other colors which suggests that this was the last image layer that was added. Let me give a short example how you can achieve this with Mathematica.

First, let us create a table of differently binarized images (here, you would use your different images)

lena = ImageResize[
   ColorConvert[ExampleData[{"TestImage", "Lena"}], "Grayscale"], 128];
imgs = Table[Binarize[lena, t], {t, .1, .9, .05}]

Now, I will make two things. First, I will apply Colorize as you did, but I'll use a (constantly growing) color of a ColorData scheme as you wanted. Secondly, I'm setting the alpha-channel of the image to make black transparent.

init = 0;
imgsCol = With[{col = (init += .06)},
      ColorRules -> {1 -> ColorData["PlumColors", col], 0 -> Black}], #]
    ] & /@ imgs

Mathematica graphics

The important part is, that you set the alpha-channel with SetAlphaChannel[col,bin] where col is the colorized binary image and bin is the binary image.

After that, you can use ImageCompose repeatedly to combine all images

Fold[(ImageCompose[#1, #2]) &, First[imgsCol], Rest[imgsCol]]

Mathematica graphics


Here is a revision that (maybe) combines things as you ask. The SetAlphaChannel command makes the black pixels clear in each of the images. Now you can combine the images directly using Show, and the order of precedence in the colors is given by the order in which they appear in the Show command.

rchan = Import["https://i.sstatic.net/ruNFL.png"];
gchan = Import["https://i.sstatic.net/l3otV.png"];
bchan = Import["https://i.sstatic.net/qN4wS.png"];
black = ImageMultiply[rchan, 0];
rimg = ColorCombine[{rchan, black, black}];
gimg = ColorCombine[{black, gchan, black}];
bimg = ColorCombine[{black, black, bchan}]; 
 ralpha = SetAlphaChannel[rimg, Binarize[rchan, t]];
 galpha = SetAlphaChannel[gimg, Binarize[gchan, t]];
 balpha = SetAlphaChannel[bimg, Binarize[bchan, t]];
 Show[black, galpha, balpha, ralpha, ImageSize -> 600], {{t, 0.156}, 0, 1}]

enter image description here

  • $\begingroup$ Dear Bill, ColorCombine produces when the colors are superposed (the same pixel) another different color. Is it possible that image2 covers image1, and image 3 covers image 2? $\endgroup$
    – mrz
    Commented Dec 4, 2014 at 14:45
  • $\begingroup$ If image 2 covers image 1 then you won't see 1 at all. So this can't be right. halirutan shows how to make black transparent... if this isn't what you need either, then you'll have to explain more clearly. $\endgroup$
    – bill s
    Commented Dec 4, 2014 at 14:54
  • $\begingroup$ Image 1 is different from image 2. If Image 1 consists of green spots on black barkground, and image 2 of red spots on black background and if I want to put Image 2 on Image 1, then the following should happen: for each pixel in the two images the order of pixels should be red over green over black. $\endgroup$
    – mrz
    Commented Dec 5, 2014 at 12:59
  • $\begingroup$ 1. if image 2 has a red color at pixel n and image 1 has a green or black color at pixel n then the result should be red. 2. if image 2 has a black color at pixel n and image 1 has a green color at pixel n then the result should be green. 3. if image 2 has a black color at pixel n and image 1 has a black color at pixel n then the result should be black. $\endgroup$
    – mrz
    Commented Dec 5, 2014 at 13:14
  • $\begingroup$ Please see above revision. $\endgroup$
    – bill s
    Commented Dec 5, 2014 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.