# Reclassify and colorize a grayscale image in several regions according to given pixel value ranges

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:

Manipulate[
Show[ColorReplace[
ColorReplace[Binarize[ThermalImage, {ImageMin, RangeMin}], Black],
White -> Blue],
ColorReplace[
ColorReplace[Binarize[ThermalImage, {RangeMin, RangeMax}], Black],
White -> Black],
ColorReplace[
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:

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.

• Can you share your original image? – MarcoB Jun 22 '18 at 0:49
• @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 – Pablo Leautaud Jun 22 '18 at 1:45

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;

Manipulate[
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"}
]


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

• 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. – Pablo Leautaud Jul 2 '18 at 1:03