IDL开发——密度分割
一、实现功能
输入tif格式文件,按照步长为0.2等分进行分割。
二、IDL代码
1.主窗体
在主窗体中写明各base成分,标注event。
wButton4=WIDGET_BUTTON(wtemp2,value='密度分割',event_pro='Density')
2.event 事件
pro Density,ev
COMPILE_OPT IDL2
ENVI,/RESTORE_BASE_SAVE_FILES
ENVI_Batch_init
filename=ENVI_PICKFILE(title='请输入待分割的文件',filter='*.tif')
ENVI_OPEN_FILE,filename,r_fid=fid,/no_realize
ENVI_FILE_QUERY,fid,ns = numSample,nl = numLine,nb = numBand,$
data_type=dataType,dims = dims
mapInfo = ENVI_GET_MAP_INFO(FID=fid)
imageData = FLTARR(numSample,numLine,numBand)
imageData[*,*,0] = ENVI_GET_DATA(fid = fid,dims = dims,pos = 0)
;*********************************确定密度分割的阈值***********************************************************
;w=where(NDVI ge 0,count)
ht=histogram(imageData[*,*,0],nbins=255,locations=locations)
ht_acc=total(ht,/cumulative)/n_elements(imageData[*,*,0])
w1=where(ht_acc gt 0.20)
num1=locations[w1[0]]
w2=where(ht_acc gt 0.40)
num2=locations[w2[0]]
w3=where(ht_acc gt 0.60)
num3=locations[w3[0]]
w4=where(ht_acc gt 0.80)
num4=locations[w4[0]]
minv=min(imageData[*,*, 0])
maxv=max(imageData[*,*,0])
print,minv,maxv
print,'num1:',num1,'num2:',num2,'num3:',num3,'num4:',num4
dst=[[minv,num1,209,255,115],$
[num1,num2,170,255,0],$
[num2,num3,152,230,0],$
[num3,num4,56,168,0],$
[num4,maxv,38,115,0]]
;**************************************************密度分割*********************************************************
sz2=size(dst)
n_ds=sz2[2]
sz=size(imageData[*,*,0])
ns=sz[1]&nl=sz[2]
result_r=bytarr(ns,nl)
result_g=bytarr(ns,nl)
result_b=bytarr(ns,nl)
;密度分割
for i=0,n_ds-1 do begin
w=where(imageData[*,*,0] gt dst[0,i] and imageData[*,*,0] le dst[1,i],count)
if count gt 0 then begin
result_r[w]=dst[2,i]
result_g[w]=dst[3,i]
result_b[w]=dst[4,i]
endif
endfor
resultmap=bytarr(3,ns,nl)
resultmap[0,*,*]=result_r
resultmap[1,*,*]=result_g
resultmap[2,*,*]=result_b
sz3=size(resultmap)
ns3=sz3[2] & nl3=sz3[3]
W1=window(window_title='密度分割',dimension=[numSample,numLine*1.25])
i1=image(resultmap,dimension=[ns3,nl3],margin=0,window_title='Level',position=[0,0.2,1,1],/order)
;tickname=[dst[0,0],transpose(dst[1,*])]
;tickname=string(tickname,format='(f6.2)')
tickname=[string(minv),string(num1),string(num2),string(num3),string(num4),string(maxv)]
tab_dst=dst[2:4,*]
c1=colorbar(position=[0.05,0.1,0.95,0.15],title='density_slice',$
rgb_table=tab_dst,tickname=tickname,taper=0,/border)
DELVAR,imageData
end