Colorizing thresholded images with different colors

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

thersholding a image
(all values above a certain threshold are set 1, the rest is 0 )
colorizing the thresholded image from 1. with a specific color corresponding
to a specific color table.
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:

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

For 1.:

inputImage = Import@”http://i.stack.imgur.com/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:

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):

=================

  

 

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;
– mrz
Dec 2 ’14 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)},
SetAlphaChannel[
Colorize[#,
ColorRules -> {1 -> ColorData[“PlumColors”, col], 0 -> Black}], #]
] & /@ imgs

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]]

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[“http://i.stack.imgur.com/ruNFL.png”];
gchan = Import[“http://i.stack.imgur.com/l3otV.png”];
bchan = Import[“http://i.stack.imgur.com/qN4wS.png”];
black = ImageMultiply[rchan, 0];
rimg = ColorCombine[{rchan, black, black}];
gimg = ColorCombine[{black, gchan, black}];
bimg = ColorCombine[{black, black, bchan}];
Manipulate[
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}]

  

 

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?
– mrz
Dec 4 ’14 at 14:45

  

 

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.
– bill s
Dec 4 ’14 at 14:54

  

 

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.
– mrz
Dec 5 ’14 at 12:59

  

 

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.
– mrz
Dec 5 ’14 at 13:14

  

 

Please see above revision.
– bill s
Dec 5 ’14 at 14:12