Hello, everyone. I want to batch subset images via ROI by IDL programing, and I also want to mask the data that outside the ROI at the meantime. I find an example on the website, however, this example is only subset the data by squire shape. The following is the my revised codes:
PRO Subset_via_roi
ENVI, /RESTORE_BASE_SAVE_FILES
ENVI_BATCH_INIT, LOG_FILE='batch.txt'
; define the image to be opened
szPath = 'E:\Rubber\IDL&ENVI\Subset'
filters = ['*.img','*.tif']
img_file = DIALOG_PICKFILE(PATH=szPath,TITLE='Select Image for Subset',FILTER=filters)
IF (img_file EQ "") THEN RETURN
ENVI_OPEN_FILE,img_file,R_FID=fid
PRINT, 'fid=',fid
; query the file and define the output file variables
ENVI_FILE_QUERY,fid,DIMS=dims,NS=ns,NL=nl,NB=nb
t_fid=LONARR(nb)+fid
pos=LINDGEN(nb)
out_name=szPath + '\subset.img'
;open the EVF file
evf_file = DIALOG_PICKFILE(PATH=szPath,TITLE='Select Vector File to create ROI',FILTER=['*.evf','*.shp'])
IF (evf_file EQ "") THEN RETURN
evf_id=ENVI_EVF_OPEN(evf_file)
;get the vector information
ENVI_EVF_INFO,evf_id,NUM_RECS=num_recs,DATA_TYPE=data_type,PROJECTION=projection,$
LAYER_NAME=layer_name
; print information about each record
PRINT, 'Number of records:',num_recs
FOR i=0,num_recs-1 DO BEGIN
record=ENVI_EVF_READ_RECORD(evf_id,i)
;PRINT, 'Number of nodes in Record '+STRTRIM(i+1,2)+':',N_ELEMENTS(record[0,*])
;PRINT, 'Record Info:'
;PRINT, record
ENDFOR
; Get record info (assuming one record in EVF)
record=ENVI_EVF_READ_RECORD(evf_id,0)
; Result[0, *] contains the x values, and Result[1, *] contains the y values
xMap=record[0,*]
yMap=record[1,*]
roi_id = ENVI_CREATE_ROI(COLOR=4, NAME='Danzhou roi', NS=ns, NL=nl)
; Define the roi
ENVI_DEFINE_ROI, roi_id, /POLYGON, XPTS=xMap, YPTS=yMap
;roi_data = ENVI_GET_ROI_DATA(roi_id,POS=pos,FID=t_fid)
;Convert record to xy pixel coordinates of the image to be subsetted
ENVI_CONVERT_FILE_COORDINATES,fid,xf,yf,xMap,yMap
;Prepare the subset dimensions
dims[0]= ENVI_GET_ROI_DIMS_PTR(roi_id) ;-1L ; A Pointer to the Opened ROI
dims[1]=min(xf)-1 ; The starting sample number, the first x pixel is 0
dims[2]=max(xf)-1 ; The Ending sample number
dims[3]=min(yf)-1 ; The starting line number, the first y pixel is 0
dims[4]=max(yf)-1 ; The ending line number.
;Use copyfile_doit to copy input file with new dimensions
ENVI_DOIT,'CF_DOIT',FID=t_fid,POS=pos,DIMS=dims,REMOVE=0,OUT_NAME=out_name,R_FID=r_fid
; Exit Envi
ENVI_BATCH_EXIT
END
The codes in red color is added for testing. However, it does not work, the output results is also squire shape.
Any help is appreciated.
|