X
4355

Band math function to reclass an RGB image

Sometimes classification images are saved in a data format that converts the data from a single band color class file to a 3-band RGB image (for example, saving a classification image to TIFF from the ENVI display). You can restore the classes by setting the various RGB ranges to a specific class value using Band Math. This Help Article shows an example of a simple band math function that can be used to 'reclass' an RGB image.
 
The below code is an example band math function that will assign various RGB combinations to specific classes. The example was used on a classification image saved as an RGB TIFF, which resulted in 3 bands ranging from 0 to 256. The band math function should be altered to represent the number of RGB combinations (classes) that exist in the image. The result will be a gray-scale image with a data type of byte. You can then change the file type in the ENVI header to 'ENVI Classification' and assign colors to the data values to recreate the classification image.

;*************************************************************************
function reclass_rgb, b1, b2, b3, c1, c2, c3, c4, c5, c6, c7
;
; Example function for use in Band Math.
;
;;
; find which pixels in each band  fall into each class
;
class1 = WHERE(b1 eq 0 and b2 eq 0 and b3 eq 0, count1)
class2 = WHERE(b1 eq 255 and b2 eq 0 and b3 eq 0, count2)
class3 = WHERE(b1 eq 0 and b2 eq 255 and b3 eq 0, count3)
class4 = WHERE(b1 eq 0 and b2 eq 0 and b3 eq 255, count4)
class5 = WHERE(b1 eq 255 and b2 eq 255 and b3 eq 0, count5)
class6 = WHERE(b1 eq 0 and b2 eq 255 and b3 eq 255, count6)
class7 = WHERE(b1 eq 255 and b2 eq 0 b3 eq 255, count7)

;reclassify to 7 classes
classified = BYTE(b1)
IF count1 NE 0 THEN classified(class1) = 0B
IF count2 NE 0 THEN classified(class2) = 1B
IF count3 NE 0 THEN classified(class3) = 2B
IF count4 NE 0 THEN classified(class4) = 3B
IF count5 NE 0 THEN classified(class5) = 4B
IF count6 NE 0 THEN classified(class6) = 5B
IF count7 NE 0 THEN classified(class7) = 6B

;
; return the classified image
;
RETURN, classified

end

Review on 12/31/2013 MM