Program to simplify the classes in an Imagine classification image
This Help Article provides example code which will simplify, or reduce, the classes in an Imagine classification image to the unique classes contained in the dataset.
Some Imagine classification images contain a classification lookup table of 0-255 classes where there are many redundant and empty (0) classes. The following code will use the classification lookup table to reduce the number of classes to only those that have unique RGB triplets, thus simplifying the classification image to only the unique classes contained in the dataset.
------------------------------
pro simplify_img_classes
compile_opt idl2
file = envi_pickfile(filter='*.img', $
title='Select input Classification .img')
if (file eq '') then return
envi_open_file, file, /no_realize, r_fid=fid
envi_file_query, fid, nb=nb, ns=ns, nl=nl, dims=dims, num_classes=num_classes, lookup=lookup
pos=lindgen(nb)
;find redundant colors by converting RGB "triplets" into their 32-bit True Color
;values, then sorting those values looking for the UNIQ set. Convert the
;non-redundant colors back to sorted RGB triplets in a new ENVI lookup table."
r = lookup[0,*]
g = lookup[1,*]
b = lookup[2,*]
paletteIntsIn = ishft(long(b), 16) + ishft(long(g), 8) + r
sortedPal = sort(paletteIntsIn)
collapsedPaletteInts = paletteIntsIn[sortedPal[uniq(paletteIntsIn[sortedPal])]]
nClasses = n_elements(collapsedPaletteInts)
;create the new lookup table
new_lookup = bytarr(3, nClasses)
new_lookup[0,*] = collapsedPaletteInts and '0000FF'x
new_lookup[1,*] = ishft(collapsedPaletteInts, -8) and '0000FF'x
new_lookup[2,*] = ishft(collapsedPaletteInts, -16)
comb_lut = lindgen(num_classes)
for i = 0, n_elements(collapsedPaletteInts) -1 do $
comb_lut[where(paletteIntsIn eq collapsedPaletteInts[i])] = i
;user entered output file name
base = widget_auto_base(title='Select output file name')
wo = widget_outf(base, uvalue='outf', /auto)
result = auto_wid_mng(base)
if (result.accept eq 0) then return
out_name = string(result.outf)
;name classes
classes = 'Class '+ strtrim(bindgen(nClasses), 2)
class_names = ['Unclassified', classes]
;call the Combine Classes doit
envi_doit, 'com_class_doit', fid=fid, pos=pos, dims=dims, $
comb_lut=comb_lut, /remove_empty, out_name=out_name, r_fid=r_fid
;edit the header to include the new lookup table
envi_change_head, r_fid, lookup=new_lookup, class_names=class_names, /write
envi_file_mng, id=r_fid, /remove
envi_file_mng, id=fid,/remove
envi_open_file, out_name
end
Review on 12/31/2013 MM