5466
Using Training Data From One Image to Classify Another Image
It is possible in ENVI to set up training data on one image, and then use those training data when classifying other images. You can do it interactively (within the ENVI GUI) or in batch code.
To do it in the GUI, first search for Endmember Collection in the ENVI toolbox. When you double click on that tool, you will see a file selection dialog, in which you select the image to classify. After you've chosen your input file and clicked OK, you will see the Endmember Collection dialog. You choose your classification algorithm under the Algorithms menu. Under the Import menu, notice that there is an option called "from ROI/EVF from other file". This is where you can indicate a ROI or a vector layer defined on another image to use as training data for the currently selected classification input image.
You can set up the same thing in batch code, which you can then loop over any number of input images, using the CLASS_DOIT routine. The way you set up the training data with CLASS_DOIT depends on the classification method you are using. For example, if you are using a Maximum Likelihood classification algorithm, then you provide the training data as an array of covariance matrices, and array of standard deviations, and an array of spectral means. If you have regions of interest defined on one image, you can calculate their statistics and use those during classifications of other images. ENVI won't even know you are doing that! You just pass the same statistics variables into the CLASS_DOIT routine for each input image.
Here is an example batch mode procedure, using the ENVI Classic batch mode API, that calculates statistics for a set of Regions of Interest defined on one dataset, then uses those statistics to define the classes for a maximum likelihood classification of another image.
pro test_classify_roi
compile_opt IDL2
; Set a variable to contain the names of the two images, then open the first of those images.
File = ['C:\Data\Testing\tile_34_8bit.TIF', 'C:\Users\User\Desktop\IDL_Practice\tile_24_8bit.TIF']
envi_open_file, file[0], r_fid=fid
;query the file for information you need
envi_file_query, fid, nb=nb, dims=dims
pos=lindgen(nb)
envi_restore_rois, 'C:\Data\Testing\tile_34_ROIs.roi'
roi_ids = envi_get_roi_ids(fid=fid, $
roi_colors=roi_colors, roi_names=class_names)
class_names = ['Unclassified', class_names]
num_classes = n_elements(roi_ids)
lookup = bytarr(3, num_classes + 1)
; Set the unclassified class to black and use roi colors
lookup = bytarr(3,num_classes+1)
lookup[0,1] = roi_colors
mean = fltarr(n_elements(pos), num_classes)
stdv = fltarr(n_elements(pos), num_classes)
cov = fltarr(n_elements(pos),n_elements(pos),num_classes)
;Loop over each ROI and calculate its statistics
for j=0, num_classes-1 do begin
; get the statistics for each selected class
roi_dims=[envi_get_roi_dims_ptr(roi_ids[j]), 0, 0, 0, 0]
envi_doit, 'envi_stats_doit', fid=fid, pos=pos, $
dims=roi_dims, comp_flag=4, mean=c_mean, $
stdv=c_stdv, cov=c_cov
mean[0,j] = c_mean
stdv[0,j] = c_stdv
cov[0,0,j] = c_cov
endfor
;remove rois
roi_ids = envi_get_roi_ids()
envi_delete_rois, roi_ids
;Now open the second image
envi_open_file, file[1], r_fid=fid2
;Set up an output filename for the maximum likelihood classification of this image
out_name= STRMID(file[1], 0, STRPOS(file[1],".",/reverse_search))+"_max_like.dat"
;run max likelihood classifier on the second image,
;using the stats from the ROIs defined on the first image
envi_doit, 'class_doit', fid=fid2, pos=pos, dims=dims, $
out_bname='Max Like', out_name=out_name, method=2, $
mean=mean, stdv=stdv, std_mult=st_mult, r_fid=r_fid, $
lookup=lookup, cov=cov, class_names=class_names, $
num_classes=num_classes, in_memory=0, m_fid=m_fid, m_pos=0
end
Review on 8/29/14 PS, KK