3642
How to programmatically convert EVF files to ROIs
Topic:
Currently there is no established function or procedure to convert an ENVI Vector File (EVF) to a Region of Interest (ROI) programmatically. However, it can be accomplished using a small number of established routines. This Help Article discusses how to go about the conversion and provides a working example program.
Discussion:
The following steps are recommended to programmatically convert an EVF file to an ROI:
1) Have an image available that you would like to associate with the ROI since ROIs are created based on available image dimensions. You can provide specific values for the number of samples and lines for the ROI if an image is unavailable.
2) Have all of your EVF files in the same directory, or at least be able to programmatically specify their locations if you are converting multiple files in batch mode.
3) Take each record in an EVF (polygon in this example), retrieve its defining vertices in map coordinates, convert the map coordinates to image coordinates, and then use the image-based vertices to build the ROI.
4) To differentiate ROIs from one another, retrieve the EVF layer name (one per file) and assign it to the ROI, along with a unique color starting with ENVI's default color index of 2 (Red).
pro evf_to_roi
compile_opt idl2
;Prompt user to select an image to associated with the ROIs
base_file = dialog_pickfile(title='Select image file for ROI generation')
;Prompt user to provide directory containing all EVF files to convert
evf_files = file_search(dialog_pickfile(title='Select folder with EVF files', /directory), '*.evf')
;Prompt user to select output directory for ROI file
output_path = dialog_pickfile(title='Select output path for ROI file', /directory)
;Open the image, retrieve number of samples and lines
envi_open_file, base_file, r_fid=base_fid
envi_file_query, base_fid, ns=ns, nl=nl
;Removes any existing ROIs from the current ENVI session so ;that they are not included in the output .roi file
roi_cleanup = envi_get_roi_ids()
envi_delete_rois, roi_cleanup
;Loop through each record in an EVF file and use the records to
;build ROIs vertex by vertex
for i = 0, n_elements(evf_files)-1 do begin
evf_id = envi_evf_open(evf_files[i])
;Use original layer name as ROI name, when present
envi_evf_info, evf_id, num_recs=num_recs, layer_name=name
;Start ROI color assignment with Red
roi_color = 2 + i
roi_id = envi_create_roi(color=roi_color, ns=ns, nl=nl, name=name)
for j = 0, (num_recs - 1) do begin
rec = envi_evf_read_record(evf_id, j)
xmap = transpose(rec[0,*])
ymap = transpose(rec[1,*])
;Convert EVF map coordinates for each vertex to image
;coordinates for use in the ROI
envi_convert_file_coordinates, base_fid, xpts, ypts, xmap, ymap
envi_define_roi, roi_id, /polygon, xpts=xpts, ypts=ypts
endfor
envi_evf_close, evf_id
endfor
;Retrieve all generated ROIs and output a file that preserves associated
;colors and names
roi_ids = envi_get_roi_ids(roi_names=roi_names, /short_name, roi_colors=colors)
envi_save_rois, output_path+path_sep()+file_basename(base_file)+'.roi', roi_ids
end
Review 9/11/20104 by CS