X

Help Articles are product support tips and information straight from the NV5 Geospatial Technical Support team developed to help you use our products to their fullest potential.



3298 Rate this article:
No rating

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

Please login or register to post comments.
Featured

End-of-Life Policy Enforcement for ENVI 5.3 / IDL 8.5 and Earlier Versions

5/6/2024

April 1, 2024 Dear ENVI/IDL Customer,  We are reaching out to notify you of our supported... more »

How to Upgrade licenses to ENVI 6.x / IDL 9.x

12/5/2023

What is the new Upgrade function? Starting with ENVI 6.0 and IDL 9.0, we have implemented an... more »

What to do if the 'License Administrator - License Server' for the Next-Generation License Server does not start?

6/13/2023

Background: With the release of ENVI 5.7 & IDL 8.9 and the corresponding Next-Generation licensing... more »

Next-Generation Licensing FAQ

4/28/2023

  NV5 Geospatial has adopted a new licensing technology for all future releases of our ENVI, IDL... more »

The IDL Virtual Machine

6/6/2013

What is the IDL Virtual Machine? An IDL Virtual Machine is a runtime version of IDL that can... more »