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.



5480 Rate this article:
1.0

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

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 »