X
5397

How to convert a TIFF to a classification image programmatically

This Help Article provides an example of how to open a single-band, grayscale TIFF and convert it into an 'ENVI Classification' image programmatically. This is sometimes necessary since classified images stored in TIFF do not always import as a classification image.

--------------------------------------------

pro tiff_2_classification

;select file and query it for file information
fname = envi_pickfile(title = 'Select single band file to convert to classification')
	if (name eq '') then return  
envi_open_file, fname, r_fid=fid
envi_file_query, fid, ns=ns, nl=nl, nb=nb, dims=dims, $
	interleave=interleave, data_type=data_type

;calculate the min/max of the band in order to generate 
;the correct number of classes based on DN
envi_doit, 'envi_stats_doit', fid=fid, dims=dims, pos=0, $
	dmin=dmin, dmax=dmax, comp_flag=1

;generate the number of classes, class names, and class colors
num_classes = dmax-dmin + 1
classes = strarr(num_classes - 1)
for i=0, n_elements(classes) - 1 do begin
	classes[i] = 'Class_'+ strtrim(string(i), 1)
end

class_names = ['Unclassified', classes]
lookup = envi_color_rgb([0,indgen(n_elements(classes))+2])

;generate the ENVI header with the new information
file_type = envi_file_type('ENVI Classification')
envi_setup_head, fname=fname, ns=ns, nl=nl, nb=nb, interleave=interleave, $
	data_type=data_type, file_type=file_type, class_names=class_names, $
	lookup=lookup, num_classes=num_classes, /write

;remove the open file so it can be reopened
envi_file_mng, id=fid, /remove
envi_open_file, fname

end
Review on 12/31/2013 MM