用envi二次开发的方式,提取TM影像中的水。如图1所示。
图1 TM影像(提取前)
提取的影像中水的方法有很多种,包括单波段阈值法和多波段组合法,这里选用多波段组合法。
公式为:(float(b2)+float(b3)) gt (float(b4)+float(b5))。即第二波段加第三波段大于第四波段加第五波段。
先用把公式代入到envi中,实现No problem以后,继续。
接着需要用IDL中的波段计算的函数,math_doit。
在IDL中的代码如下所示(修改之前):
1 PRO math_doit 2 3 compile_opt IDL2 4 catch,error_status 5 if error_status NE 0 then begin 6 void=dialog_message(!error_state.msg,title='发生错误',/error) 7 catch,/cancel 8 return 9 endif 10 11 ; First restore all the base save files. 12 envi, /restore_base_save_files 13 14 ; Initialize ENVI and send all errors 15 ; and warnings to the file batch.txt 16 envi_batch_init, log_file='batch.txt' 17 inputfilename='D:\Program Files\Exelis\ENVI50\classic\data\can_tmr.img' 18 19 ; Open the input file 20 envi_open_file,inputfilename , r_fid=fid 21 if (fid eq -1) then begin 22 envi_batch_exit 23 return 24 endif 25 26 ; Set the keywords. We will perform the 27 ; band math on all samples in the file. 28 envi_file_query, fid, dims=dims 29 t_fid = [fid,fid] 30 pos = [1,2,3,4] 31 exp='(float(b2)+float(b3)) gt (float(b4)+float(b5))' 32 out_name='d:\Water.img' 34 35 ; Perform the band math processing 36 envi_doit, 'math_doit', $ 37 fid=t_fid, pos=pos, dims=dims, $ 38 exp=exp, out_name=out_name, $ 39 r_fid=r_fid 40 END
结果发现,编译完成以后没有错误,运行也没有错误,但是就是没有计算结果,很让人头疼。
纠结了半天,后来才发现,原来是fid的惹的祸。
envi帮助中,有关于math_doit函数的解释:
math_doit,用于影像中的波段计算。
使用语法为,ENVI_DOIT, 'MATH_DOIT', DIMS=array, EXP=string, FID=array, /IN_MEMORY, OUT_BNAME=string array, OUT_NAME=string, POS=array, R_FID=variable
1、DIMS
指dimensions,包含有五个长整型来定义空间子集。
DIMS[0]:只要在有感兴趣区的时候才有用,否则,默认为-1。
DIMS[1]:开始像元的行数,第一个x像元为0。
DIMS[2]:最后的像元的行数。
DIMS[3]:开始像元的列数,第一个y像元为0。
DIMS[4]:最后的像元的列数。
如果影像文件没有空间子集,也要写DIMS,写法如下:
envi_file_query,fid,dims=dims
2、EXP
指expression,即波段计算的表达式。如:
EXP='b1+b2'
EXP='byte((float(b1)+float(b2)+float(b3))/3.0)'
3、FID
指定长整型数组所代表的影像的ID,每一个代表着EXP中的一个波段。
4、IN_MEMORY
指定输出文件是否输出在内存中,如果不输出在内存中,则必须指定OUT_NAME。
5、OUT_BANME
指定输出波段名字。
6、OUT_NAME
指定输出影像的名字,如果设置为IN_MEMORY,则不需要设置。
7、POS
指定波段位置的数组,表明要进行计算的波段数量。
长整型数组,从0至少到1,0即为波段1,1为波段2,以此类推。
如果在一个影像中使用,则可以这样写:
POS=[0,1,2,3]
envi_doit,'envi_stats_doit',dims=dims,fid=fid,pos=pos,$
comp_flag=3,dmin=dmin,dmax=dmax,mean=mean,stdv=stdv,hist=hist
但是,如果计算多个影像中的不用波段,如计算test1影像的波段3,test2影像的波段2,test影像的波段6,test4影像的波段4,则应该这么写:
fid_array=[fid1,fid2,fid3,fid4]
pos=[2,1,5,3]
envi_doit,'cf_doit',dims=dims,fid=fid_array
out_name='test_composite_file'
8、R_FID
指‘returned FID’,生成的新影像的FID,如果为-1,则生成失败。
(详细解释参见ENVI Classic Help——>math_doit)
所以,原来是代码中第29行写错了,应改为t_fid=[fid,fid,fid,fid],四个波段应该是四个fid,这样就OK了。
运行结果如图2所示。
图2 TM影像(提取后)
另外,如果运行以后没有得到结果,检查一下运行后R_FID的值,如果为-1,则检查输出文件的名称是否正确,文件名中不能包括 / \ : * " ? < > | 这九种符号。