您的位置:首页 > 其它

IDL中关于波段计算的问题

2013-04-04 21:52 316 查看
用envi二次开发的方式,提取TM影像中的水。如图1所示。



图1 TM影像(提取前)

提取的影像中水的方法有很多种,包括单波段阈值法和多波段组合法,这里选用多波段组合法。

公式为:(float(b2)+float(b3)) gt (float(b4)+float(b5))。即第二波段加第三波段大于第四波段加第五波段。

先用把公式代入到envi中,实现No problem以后,继续。

接着需要用IDL中的波段计算的函数,math_doit。

在IDL中的代码如下所示(修改之前):

PRO math_doit

compile_opt IDL2
catch,error_status
if error_status NE 0 then begin
void=dialog_message(!error_state.msg,title='发生错误',/error)
catch,/cancel
return
endif

; First restore all the 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'
inputfilename='D:\Program Files\Exelis\ENVI50\classic\data\can_tmr.img'

; Open the input file
envi_open_file,inputfilename , r_fid=fid
if (fid eq -1) then begin
envi_batch_exit
return
endif

; Set the keywords. We will perform the
; band math on all samples in the file.
envi_file_query, fid, dims=dims
t_fid = [fid,fid]
pos = [1,2,3,4]
exp='(float(b2)+float(b3)) gt (float(b4)+float(b5))'
out_name='d:\Water.img'

; Perform the band math processing
envi_doit, 'math_doit', $
fid=t_fid, pos=pos, dims=dims, $
exp=exp, out_name=out_name, $
r_fid=r_fid
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,则检查输出文件的名称是否正确,文件名中不能包括 / \ : * " ? < > | 这九种符号。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: