利用mask_doit函数,实现批量裁剪。
compile_opt idl2
start_path = 'F:\Temporary\data\LC8 Results\'
cd,start_path
out_path = 'F:\Temporary\data\LC8 Results\ENVI_clip\temp\ROI\'
Files = FILE_Search(start_path + 'compo432.tif', count = Filecount)
; FileCount = N_ELEMENTS(Files)
If FileCount Eq 0 Then Begin
print, 'File not exist'
Return
Endif
For num = 0, FileCount - 1 do begin
FileName = Files[num]
envi_open_file, FileName , r_fid=fid, /no_realize, /invisible
if (fid eq -1) then begin
return
endif
Print, 'fid=', fid
ENVI_FILE_QUERY, fid, dims=dims, ns=ns, nl=nl, nb=nb, BNAMES= BNAMES
t_fid=LONARR(nb)+fid ;此句的作用?在以后实践中明确。
pos=LINDGEN(nb)
pos_num = strpos(file_basename(FileName),'.')
out_name = out_path + strmid(file_basename(FileName),0, pos_num) + "_roi.tif"
print, out_name
evf_file ='F:\Temporary\data\LC8 Results\Envi_polygon_.evf';
evf_id=ENVI_EVF_OPEN(evf_file)
ENVI_EVF_INFO, evf_id, num_recs = num_recs,data_type = data_type,$
projection = projection, layer_name = layer_name
roi_ids = LONARR(num_recs)
For i = 0, num_recs - 1 do begin
record = ENVI_EVF_READ_RECORD(evf_id, i)
ENVI_CONVERT_FILE_COORDINATES,fid,xmap,ymap,record[0,*],record[1,*]
roi_id = ENVI_CREATE_ROI(color = 4, ns = ns , nl = nl)
ENVI_DEFINE_ROI, roi_id, /polygon, xpts=REFORM(xMap), ypts=REFORM(yMap)
roi_ids = roi_id
IF i EQ 0 THEN BEGIN
xmin = ROUND(MIN(xMap,max = xMax))
yMin = ROUND(MIN(yMap,max = yMax))
ENDIF ELSE BEGIN
xmin = xMin < ROUND(MIN(xMap))
xMax = xMax > ROUND(MAX(xMap))
yMin = yMin < ROUND(MIN(yMap))
yMax = yMax > ROUND(MAX(yMap))
ENDELSE
ENDFOR
xMin = xMin > 0
xmax = xMax < ns ;- 1
yMin = yMin > 0
ymax = yMax < nl ;- 1
ENVI_MASK_DOIT,AND_OR = 1, /IN_MEMORY, $
ROI_IDS= roi_ids, ns = ns, nl = nl, $
/inside, r_fid = m_fid
out_dims = [-1,xMin,xMax,yMin,yMax]
ENVI_MASK_APPLY_DOIT, FID = fid, POS = pos, DIMS = out_dims, $
M_FID = m_fid, M_POS = m_pos, VALUE = 0, out_name = out_name, R_FID = r_fid, $
OUT_BNAME = BNAMES, IN_MEMORY = 0
; 应当理解 M_POS关键字的含义: specify the band position of the mask band.
; 因此,令M_POS = m_pos,就能实现对多波段图像的裁剪,而非以前令M_POS = [0]的简单赋值(对单波段图像的处理)。
;
ENVI_FILE_MNG, id =m_fid,/remove
Endfor
print,' Clip Done'