;+ ;ENVI二次开发功能代码 ; ;Author: DYQ ;问题讨论: ; http://hi.baidu.com/dyqwrp ; http://bbs.esrichina-bj.cn/ESRI/?fromuid=9806 ;描述: ; 大气校正 ; ;调用方法: ;cal_QUAC,inputfile,outputfile ; ;- PRO CAL_QUAC,inputfile,outputfile COMPILE_OPT idl2 CATCH, Error_status errorshow = 'Sorry to see the error,'+ $ ' please send the error Information to "dongyq@esrichina-bj.cn"' IF Error_status NE 0 THEN BEGIN tmp = DIALOG_MESSAGE(errorshow+STRING(13b)+$ !ERROR_STATE.MSG,/error,title = '错误提示!') return ENDIF ENVI_OPEN_FILE, inputfile, r_fid=fid IF (fid EQ -1) THEN BEGIN RETURN ENDIF
PRO CAL_SHARPEN,inputfileMulti,inputfilePan,outputfile,method COMPILE_OPT idl2 CATCH, Error_status errorshow = 'Sorry to see the error,'+ $ ' please send the error Information to "dongyq@esrichina-bj.cn"' IF Error_status NE 0 THEN BEGIN tmp = DIALOG_MESSAGE(errorshow+STRING(13b)+$ !ERROR_STATE.MSG,/error,title = '错误提示!') return ENDIF ;打开全色影像 ENVI_OPEN_FILE, inputfilePan, r_fid=h_fid IF (h_fid EQ -1) THEN BEGIN ENVI_BATCH_EXIT RETURN ENDIF ;获取影像参数 ; ENVI_FILE_QUERY, h_fid, ns=h_ns, nl=h_nl,$ dims = h_dims,nb = h_nb
; 打开多光谱影像 ENVI_OPEN_FILE, inputfileMulti, r_fid=m_fid IF (m_fid EQ -1) THEN BEGIN ENVI_BATCH_EXIT RETURN ENDIF ;获取影像参数 ENVI_FILE_QUERY, m_fid, dims=m_dims, $ bnames=m_bnames,nb = m_nb
IF method LT 2 THEN BEGIN ; Set the keywords f_dims = [-1l, 0, h_ns-1, 0, h_nl-1] f_pos = [0] ; rgb_fid = [m_fid,m_fid,m_fid] out_bname = ['3','2','1'] ;ENVI的融合功能 ENVI_DOIT, 'sharpen_doit', $ fid=rgb_fid, pos=lindgen(m_nb), f_fid=h_fid, $ f_dims=f_dims, f_pos=f_pos, $ out_name=outputfile, method=1, $ interp=0, out_bname=out_bname RETURN ENDIF
IF KEYWORD_SET(dims) THEN $ IF N_ELEMENTS(fids) NE N_ELEMENTS(dims[0,*]) THEN dims=0 ; IF N_ELEMENTS(fids) LT 2 THEN BEGIN xsize = -1 ysize = -1 x0 = -1 y0 = -1 RETURN ENDIF ; if no DIMS passed in ; nfiles = N_ELEMENTS(fids) IF (KEYWORD_SET(dims) EQ 0) THEN BEGIN dims = FLTARR(5, nfiles) FOR i=0, nfiles-1 DO BEGIN ENVI_FILE_QUERY, fids[i], ns=ns, nl=nl dims[*,i] = [-1L, 0, ns-1, 0, nl-1] ENDFOR ENDIF UL_corners_X = DBLARR(nfiles) UL_corners_Y = DBLARR(nfiles) east = -1e34 west = 1e34 north = -1e34 south = 1e34 FOR i=0,nfiles-1 DO BEGIN pts = [ [dims[1,i], dims[3,i]], $ ; UL [dims[2,i], dims[3,i]], $ ; UR [dims[1,i], dims[4,i]], $ ; LL [dims[2,i], dims[4,i]] ] ; LR ENVI_CONVERT_FILE_COORDINATES, fids[i], pts[0,*], pts[1,*], xmap, ymap, /to_map UL_corners_X[i] = xmap[0] UL_corners_Y[i] = ymap[0] east = east > MAX(xmap) west = west < MIN(xmap) north = north > MAX(ymap) south = south < MIN(ymap) ENDFOR xsize = east - west ysize = north - south ; xsize_pix = FIX( xsize/out_ps[0] )+1 ysize_pix = FIX( ysize/out_ps[1])+1 ; proj = ENVI_GET_PROJECTION(fid=fids[0]) map_info = ENVI_MAP_INFO_CREATE(proj=proj, mc=[0,0,west,north], ps=out_ps) temp = BYTARR(10,10) ENVI_ENTER_DATA, temp, map_info=map_info, /no_realize, r_fid=tmp_fid ; find the x and y offsets for the images ; x0 = LONARR(nfiles) y0 = LONARR(nfiles) FOR i=0,nfiles-1 DO BEGIN ENVI_CONVERT_FILE_COORDINATES, tmp_fid, xpix, ypix, UL_corners_X[i], UL_corners_Y[i] x0[i] = xpix y0[i] = ypix ENDFOR ; delete the tmp file ENVI_FILE_MNG, id=tmp_fid, /remove, /no_warning END
; PRO mosaic_files,info,pos=pos,rasterfilenames=rasterfilenames,output=output,crop=crop,$ file_pattern=file_pattern COMPILE_OPT idl2
IF N_ELEMENTS(background) EQ 0 THEN background=0 IF N_ELEMENTS(crop) NE 4 THEN crop=[0,0,0,0,0] ELSE crop=[0,crop]*[0,-1,1,-1,1]
IF N_ELEMENTS(rasterfilenames) EQ 0 AND N_ELEMENTS(file_pattern) EQ 0 THEN BEGIN IF rasterfilenames[0] EQ '' THEN RETURN ENDIF IF N_ELEMENTS(file_pattern) NE 0 THEN BEGIN rasterfilenames=FILE_SEARCH(file_pattern) ENDIF numfiles=N_ELEMENTS(rasterfilenames) rasterfids=LONARR(numfiles)
IF N_ELEMENTS(output) EQ 0 THEN BEGIN output=ENVI_PICKFILE(title='Output Mosaick Filename:') IF output EQ '' THEN RETURN ENDIF ; tlb = WIDGET_AUTO_BASE(title='镶嵌参数设置') we = WIDGET_PARAM(tlb, dt=4, field=3, $ default=-999., uvalue='param', /auto, $ PROMPT ='忽略数据值') result = AUTO_WID_MNG(tlb) IF (result.accept EQ 0) THEN RETURN ignoreValue = result.param
ENVI_OPEN_FILE, rasterfilenames[0], r_fid=tempfid rasterfids[0]=tempfid ENVI_FILE_QUERY,tempfid,nb=nb,ns=tempns,nl=tempnl,data_type=data_type map_info = ENVI_GET_MAP_INFO(fid=tempfid) out_ps=map_info.ps[0:1] IF N_ELEMENTS(pos) EQ 0 OR N_ELEMENTS(pos) GT nb THEN pos=LINDGEN(nb) posarr=LONARR(N_ELEMENTS(pos),numfiles) FOR i=0,numfiles-1 DO posarr[*,i]=pos dimsarr=LONARR(5,numfiles) dimsarr[*,0]=[-1,0, tempns-1,0, tempnl-1]-crop use_see_through = LONARR(numfiles) FOR i=1,numfiles-1 DO BEGIN ENVI_OPEN_FILE, rasterfilenames[i], r_fid=tempfid rasterfids[i]=tempfid ENVI_FILE_QUERY,tempfid,nb=nb,ns=tempns,nl=tempnl dimsarr[*,i]=[-1,0, tempns-1,0, tempnl-1]-crop ENDFOR georef_mosaic_setup, fids=rasterfids, out_ps=out_ps, dims=dimsarr, xsize=xsize, ysize=ysize,$ x0=x0, y0=y0, map_info=map_info ; USE_SEE_THROUGH = INTARR(N_ELEMENTS(rasterfids))+1 seeTv = MAKE_ARRAY(N_ELEMENTS(rasterfids),value =ignoreValue ) ENVI_DOIT, 'mosaic_doit', fid=rasterfids, pos=posarr, $ dims=dimsarr, out_name=output, xsize=xsize, $ ysize=ysize, x0=x0, y0=y0, georef=1, $ out_dt=data_type, pixel_size=out_ps, $ background=ignoreValue,SEE_THROUGH_VAL=seeTv,$ USE_SEE_THROUGH = USE_SEE_THROUGH,$ map_info=map_info END
PRO cal_mosaic, txtFile COMPILE_OPT idl2 CATCH, Error_status errorshow = 'Sorry to see the error,'+ $ ' please send the error Information to "dongyq@esrichina-bj.cn"' IF Error_status NE 0 THEN BEGIN tmp = DIALOG_MESSAGE(errorshow+STRING(13b)+$ !ERROR_STATE.MSG,/error,title = '错误提示!') return ENDIF
;如果文件不存在则返回 IF ~FILE_TEST(txtFile) THEN RETURN; ;解析 nFiles = FILE_LINES(txtFile) ; filenames = STRARR(nFiles) OPENR,lun,txtfile,/get_ READF,lun,filenames FREE_LUN,lun ; mosaic_files,rasterfilenames = filenames[0:nFiles-2],output = filenames[nFiles-1] END
CATCH, Error_status errorshow = 'Sorry to see the error,'+ $ ' please send the error Information to "dongyq@esrichina-bj.cn"' IF Error_status NE 0 THEN BEGIN tmp = DIALOG_MESSAGE(errorshow+STRING(13b)+$ !ERROR_STATE.MSG,/error,title = '错误提示!') return ENDIF
COMPILE_OPT idl2 CATCH, Error_status errorshow = 'Sorry to see the error,'+ $ ' please send the error Information to "dongyq@esrichina-bj.cn"' IF Error_status NE 0 THEN BEGIN tmp = DIALOG_MESSAGE(errorshow+STRING(13b)+$ !ERROR_STATE.MSG,/error,title = '错误提示!') return ENDIF ;输入数据预处理 ENVI_OPEN_FILE, inputfile, r_fid=fid IF (fid EQ -1) THEN BEGIN RETURN ENDIF ;获取文件信息 ENVI_FILE_QUERY, fid, dims=dims, nb=nb pos = LINDGEN(nb) out_name = outputfile CASE method OF ;-IsoData 4 6: BEGIN IF ~KEYWORD_SET(CHANGE_THRESH) THEN CHANGE_THRESH = .05 IF ~KEYWORD_SET(NUM_CLASSES) THEN NUM_CLASSES = 10 IF ~KEYWORD_SET(ITERATIONS) THEN ITERATIONS = 1 IF ~KEYWORD_SET(ISO_MERGE_DIST) THEN ISO_MERGE_DIST = 1 IF ~KEYWORD_SET(ISO_MERGE_PAIRS) THEN ISO_MERGE_PAIRS = 2 IF ~KEYWORD_SET(ISO_MIN_PIXELS) THEN ISO_MIN_PIXELS = 1 IF ~KEYWORD_SET(ISO_SPLIT_SMULT) THEN ISO_SPLIT_SMULT = 1 IF ~KEYWORD_SET(ISO_SPLIT_STD) THEN ISO_SPLIT_STD = 1 IF ~KEYWORD_SET(MIN_CLASSES) THEN MIN_CLASSES = 5
out_bname = 'IsoData'
ENVI_DOIT, 'class_doit', fid=fid, pos=pos, dims=dims, $ out_bname=out_bname, out_name=out_name, method=4, $ r_fid=r_fid, $ NUM_CLASSES = NUM_CLASSES, $ ITERATIONS = ITERATIONS, $ in_memory=0, $ CHANGE_THRESH = CHANGE_THRESH, $ ISO_MERGE_DIST = ISO_MERGE_DIST, $ ISO_MERGE_PAIRS = ISO_MERGE_PAIRS, $ ISO_MIN_PIXELS = ISO_MIN_PIXELS, $ ISO_SPLIT_SMULT = ISO_SPLIT_SMULT, $ ISO_SPLIT_STD = ISO_SPLIT_STD, $ MIN_CLASSES = MIN_CLASSES END ;-K-Means 7 7: BEGIN IF ~KEYWORD_SET(NUM_CLASSES) THEN NUM_CLASSES = 5 IF ~KEYWORD_SET(CHANGE_THRESH) THEN CHANGE_THRESH = .5 IF ~KEYWORD_SET(ITERATIONS) THEN ITERATIONS = 1 out_bname = 'K-Means'
thresh=REPLICATE(0.05,num_classes)
ENVI_DOIT, 'class_doit', fid=fid, pos=pos, dims=dims, $ out_bname=out_bname, out_name=out_name, method=7, $ r_fid=r_fid, $ lookup = BYTARR(3,num_classes+1), $ NUM_CLASSES = NUM_CLASSES, $ in_memory=0, CHANGE_THRESH=CHANGE_THRESH,$ ITERATIONS = ITERATIONS END ;-平行六面体 0 0: BEGIN ENVI_RESTORE_ROIS, roifile 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) ; Set the unclassified class to black and use roi colors lookup = BYTARR(3,num_classes+1) lookup[0,1] = roi_colors ; 计算类ROI的基本统计信息 mean = FLTARR(N_ELEMENTS(pos), num_classes) stdv = FLTARR(N_ELEMENTS(pos), num_classes) cov = FLTARR(N_ELEMENTS(pos),N_ELEMENTS(pos),num_classes) FOR j=0, num_classes-1 DO BEGIN ; 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 ; thresh=REPLICATE(0.05,num_classes) out_bname = 'parallelepiped' ENVI_DOIT, 'class_doit', fid=fid, pos=pos, dims=dims, $ out_bname=out_bname, out_name=out_name, method=0, $ mean=mean, stdv=stdv, std_mult=st_mult, $ lookup=lookup, class_names=class_names, $ in_memory=0;, thresh=thresh
END ;-最小距离 1 1: BEGIN ENVI_RESTORE_ROIS, roifile 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) ; Set the unclassified class to black and use roi colors lookup = BYTARR(3,num_classes+1) lookup[0,1] = roi_colors ; 计算类ROI的基本统计信息 ; mean = FLTARR(N_ELEMENTS(pos), num_classes) stdv = FLTARR(N_ELEMENTS(pos), num_classes) cov = FLTARR(N_ELEMENTS(pos),N_ELEMENTS(pos),num_classes) FOR j=0, num_classes-1 DO BEGIN ; 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 ; thresh=REPLICATE(0.05,num_classes) out_bname = 'MinimumDistance' ENVI_DOIT, 'class_doit', fid=fid, pos=pos, dims=dims, $ out_bname=out_bname, out_name=out_name, method=1, $ mean=mean, stdv=stdv, std_mult=st_mult, $ lookup=lookup, class_names=class_names, $ in_memory=0 END ;-最大似然 2 2: BEGIN ENVI_RESTORE_ROIS, roifile 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) ; Set the unclassified class to black and use roi colors lookup = BYTARR(3,num_classes+1) lookup[0,1] = roi_colors ; 计算类ROI的基本统计信息 ; mean = FLTARR(N_ELEMENTS(pos), num_classes) stdv = FLTARR(N_ELEMENTS(pos), num_classes) cov = FLTARR(N_ELEMENTS(pos),N_ELEMENTS(pos),num_classes) FOR j=0, num_classes-1 DO BEGIN ; 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 ; thresh=REPLICATE(0.05,num_classes) out_bname = 'MaximumLikelihood' ENVI_DOIT, 'class_doit', fid=fid, pos=pos, dims=dims, $ out_bname=out_bname, out_name=out_name, method=2, $ mean=mean, stdv=stdv, std_mult=st_mult, $ lookup=lookup, class_names=class_names, $ cov = cov,$ in_memory=0 END ;-马氏距离 5 3: BEGIN ENVI_RESTORE_ROIS, roifile 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) ; Set the unclassified class to black and use roi colors lookup = BYTARR(3,num_classes+1) lookup[0,1] = roi_colors ; 计算类ROI的基本统计信息 ; mean = FLTARR(N_ELEMENTS(pos), num_classes) stdv = FLTARR(N_ELEMENTS(pos), num_classes) cov = FLTARR(N_ELEMENTS(pos),N_ELEMENTS(pos),num_classes) FOR j=0, num_classes-1 DO BEGIN ; 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 ; thresh=REPLICATE(0.05,num_classes) out_bname = 'Mahalanobis' ENVI_GET_ROI_INFORMATION, roi_ids,nPts = nPts ENVI_DOIT, 'class_doit', fid=fid, pos=pos, dims=dims, $ out_bname=out_bname, out_name=out_name, method=5, $ mean=mean, stdv=stdv, std_mult=st_mult, $ lookup=lookup, class_names=class_names, $ cov = cov,NPTS = nPts, $ in_memory=0 END ;-神经元网络 ENVI_NEURAL_NET_DOIT 4: BEGIN IF ~KEYWORD_SET(theta) THEN theta = .9 IF ~KEYWORD_SET(eta) THEN eta = .2 IF ~KEYWORD_SET(alpha) THEN alpha = .9 IF ~KEYWORD_SET(act_type) THEN act_type = 0 IF ~KEYWORD_SET(rms_crit) THEN rms_crit = .1 IF ~KEYWORD_SET(num_layers) THEN num_layers = 3 IF ~KEYWORD_SET(num_sweeps) THEN num_sweeps = 10
ENVI_RESTORE_ROIS, roifile roi_ids = ENVI_GET_ROI_IDS(fid=fid, $ roi_colors=lookup, roi_names=class_names) ; Set the classification variables ; num_classes = N_ELEMENTS(roi_ids) class_names = ['Unclassified', class_names] lookup = REFORM([0,0,0, $ REFORM(lookup,3*num_classes)],3,num_classes+1) ; ; Call the doit ; ENVI_DOIT, 'envi_neural_net_doit', $ fid=fid, pos=pos, dims=dims, $ out_name=out_name, rule_out_name='', $ theta=theta, eta=eta, alpha=alpha, $ num_classes=num_classes, num_sweeps=num_sweeps, $ num_layers=num_layers, act_type=act_type, $ rms_crit=rms_crit, roi_ids=roi_ids, /train, $ class_names=class_names, lookup=lookup END ;-向量机 ENVI_SVM_DOIT 5: BEGIN IF ~KEYWORD_SET(thresh) THEN thresh = .5 IF ~KEYWORD_SET(penalty) THEN penalty=75 IF ~KEYWORD_SET(kernel_type) THEN kernel_type=1 IF ~KEYWORD_SET(kernel_degree) THEN kernel_degree=3 IF ~KEYWORD_SET(kernel_bias) THEN kernel_bias = 2. ENVI_RESTORE_ROIS, roifile roi_ids = ENVI_GET_ROI_IDS(fid=fid) ; Call the svm classification doit routine envi_doit, 'envi_svm_doit', $ fid=fid, pos=pos, dims=dims, $ out_name=out_name, $ roi_ids=roi_ids, thresh=thresh, $ penalty=penalty, kernel_type= kernel_type, $ kernel_degree=kernel_degree, kernel_bias=kernel_bias END ELSE: ENDCASE END
PRO cal_FX,in_file,out_file,scale_level,merge_level,ruleset_filename CATCH, Error_status errorshow = 'Sorry to see the error,'+ $ ' please send the error Information to "dongyq@esrichina-bj.cn"' IF Error_status NE 0 THEN BEGIN tmp = DIALOG_MESSAGE(errorshow+STRING(13b)+$ !ERROR_STATE.MSG,/error,title = '错误提示!') return ENDIF ; 打开文件 ENVI_OPEN_FILE, in_file, r_fid=in_fid
;文件打开出错则退出 IF (in_fid EQ -1) THEN RETURN
;获取文件信息 ENVI_FILE_QUERY, in_fid, dims=dims, nb=nb
IF ~FILE_TEST(ruleset_filename) THEN RETURN IF ~FILE_TEST(FILE_DIRNAME(out_file),/directory) THEN FILE_MKDIR,FILE_DIRNAME(out_file) ; 执行FX. ENVI_DOIT, 'envi_fx_doit', $ pos=LINDGEN(nb), $ dims=dims, $ fid=in_fid, $ scale_level=scale_level, $ merge_level=merge_level, $ vector_filename=out_file, $ conf_threshold=0.10, $ ruleset_filename=ruleset_filename, $ r_fid = out_fid END