First of all, my apologies if this is a duplicate question, I did my best at trying to find a previous possible answers.

I’m trying to find a more efficient way to reclass and colorize a single channel grayscale image into several classes (three or more) delimited by preestablished pixel value ranges.

In this particular case, my interest is to dynamically partition a thermal image into three regions considering a specific range of minimum and maximum temperatures, the regions being; (1) areas with temperatures colder than the stablished minimum colored in blue, (2) areas corresponding to the temperature range of interest in black, and (3) areas hotter than the stablished maximum colorized in red.

For the moment I have this working code:

   ColorReplace[Binarize[ThermalImage, {ImageMin, RangeMin}], Black], 
   White -> Blue], 
   ColorReplace[Binarize[ThermalImage, {RangeMin, RangeMax}], Black], 
   White -> Black], 
   ColorReplace[Binarize[ThermalImage, {RangeMax, ImageMax}], Black], 
   White -> Red]], {{RangeMin, range1, "Range minimum"}, ImageMin, 
  ImageMax, 0.1}, {{RangeMax, range2, "Range maximum"}, ImageMin, 
  ImageMax, 0.1}]

A sample of the output of this code:

sample output

I’m almost certain there is a more efficient way to obtain the same results with fewer lines of code.

Also, I’m looking for a method that could be easily adapted to reclassify at n number of bins (not only three).

Thank you so much for your assistance.

  • $\begingroup$ Can you share your original image? $\endgroup$
    – MarcoB
    Jun 22, 2018 at 0:49
  • $\begingroup$ @MarcoB Sure! I should mention the image was capture by a Seek Reveal Pro at a resolution of 240x320 pixels, this sensor outputs; a PNG that includes a gradient graphic indicating the min. and max. temperatures registered, and a TIFF file with the “raw” data in Byte form at an 8bit depth. I also include another tiff (with a “_t” suffix), this is a processed image that contains the transformed pixel values from DN to Celsius degrees, this file has a bit depth of 32bits. You can download these files as a ZIP container here: drive.google.com/open?id=1Gf9owk5q4V9-mpSZGpNy9XdljZ0dH87z $\endgroup$ Jun 22, 2018 at 1:45

1 Answer 1


I think you are reproducing the functionality of Colorize. You can that function together with an appropriate custom ColorFunction to assign a color to a particular range of intensities in your image. See the example below, where thermimg is the IMGT0495.TIFF file you included in your comment:

imagemin = 0.3;
imagemax = 0.7;

 cf = Piecewise[{
     {Blue, imagemin <= # < min},
     {Black, min <= # < med},
     {Red, med <= #}
   }] &;

 Colorize[thermimg, ColorFunction -> cf],

 {{min, 0.45}, imagemin, med, 0.01, Appearance -> "Open"},
 {{med, 0.67}, min, 1, 0.01, Appearance -> "Open"}

Mathematica graphics

I will leave it to you to optimize the thresholds as appropriate.

  • $\begingroup$ Definitely a more elegant approach than my own. And I think the Piecewise function will make it easier to segment and image on more than three classes. Thank you so much for your insight @MarcoB. $\endgroup$ Jul 2, 2018 at 1:03

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.