Creating Square ROIs Around Known Points in ENVI Classic
Users sometimes need to create buffer areas around points of interest. While the buffer zone calculator in ENVI is capable of this, the result is a rounded buffer. In the case that a user would like a square ROI, the buffer zone process is ineffective.
The following code will create square ROIs around known points. The point information must be contained in an ASCII file as output from the ENVI ROI Tool. If the points are originally contained in an ASCII file of a different format, the points can be input to the ROI Tool via ROI_Type > Input Points from ASCII... and then exported via File > Output ROIs to ASCII...
pro ROI_size_def
;
; This code creates an ROI of square areas of defined side length
; around points contained in an ASCII file previously exported
; from ENVI.
;
close, /all
compile_opt IDL2
; Restore all ENVI base save files
envi, /restore_base_save_files
;
;Initialize ENVI and send all errors and warnings to the file batch.txt
envi_batch_init, log_file='batch.txt'
;
;Open ASCII points file as exported from ENVI ROI Tool:
openr, 1, 'path\pointascii.txt'
;
; Declare list for pixel coords of points:
LL=list()
;
; While there is text in file, test lines for lat/lon info and add to list:
while ~ EOF(1) do begin
line = '' & READF, 1, line
line = strsplit(line,' ', /EXTRACT)
if (line[0] eq '1') then begin
lineone=fltarr(2)
lineone[0]=float(line[1])
lineone[1]=float(line[2])
LL.add, lineone
endif
endwhile
;print, LL
;
;Open the input files
envi_open_file, 'inputimage.dat', r_fid=file1_fid
;
; Obtain file dims:
envi_file_query, file1_fid, ns=ns, nl=nl
;print, ns, nl
;
; Obtain pixel size:
map_info=envi_get_map_info(fid=file1_fid)
xsize=map_info.ps[0]
ysize=map_info.ps[1]
;print, xsize, ysize
; Units of pixel size are assumed to be meters
;
; Convert square side length / 2 to pixels:
xlen = 500./xsize
ylen = 500./ysize
;
; Create array of ROI IDs
roiids=lindgen(LL.count())
;
; Create and define ROIs for each point:
for i=0,LL.count()-1 do begin
loc=LL[i]
roi_id = envi_create_roi(ns=ns, nl=nl)
xpts=[loc[0]-xlen, loc[0]+xlen, loc[0]+xlen, loc[0]-xlen, loc[0]-xlen]
ypts=[loc[1]+ylen, loc[1]+ylen, loc[1]-ylen, loc[1]-ylen, loc[1]+ylen]
envi_define_roi, roi_id, /polygon, xpts=xpts, ypts=ypts
roiids[i]=roi_id
endfor
;
; Save ROIs
envi_save_rois, 'path\test.roi', roiids
;
;exit ENVI
envi_batch_exit
close, /all
end