利用ENVI的ROI统计影像信息

首先在ENVI当中建立ROI,然后保存成XML格式

利用下面代码的

raster = e.OpenRaster(file)和

raster.ExportRois,infilepath+'incidence_angle_sample.csv' , rois, 'CSV'

就可以实现按照ROI统计的影像信息的CSV

打开生成的CSV后得到

利用ENVI的ROI统计影像信息_第1张图片

 RO1 #1 类别 npts(像素个数): 320

 RO1 #2 类别 npts(像素个数): 320

B1、B2、B3为影像的波段1、2和3的像素值

最后用READ_CSV打开该文件

hh=roicsv.FIELD7; 读取B1
hv=roicsv.FIELD8;读取B2
angle=roicsv.FIELD9;

pro data_select_by_roi
  COMPILE_OPT IDL2
  ; Start the application
  e = ENVI(/HEADLESS)
  
  ys=2016;
  ye=2021;
  ms=1;
  me=4;

  for yr=ys,ye do begin
    for mon=ms,me do begin
      infilepath='H:\Lead\data\sample\'+STRING(yr, FORMAT='(I04)')+'\'+STRING(mon, FORMAT='(I01)')+'\'
      cd,infilepath
      tifdata = file_search('S1*.tif',count = num);infilepath,
      if num eq 0 then begin
        print,infilepath,' has no tif file.'
        continue
      endif
      
      ; Open an input raster
      file = FILEPATH(tifdata[0], ROOT_DIR=infilepath);
      raster = e.OpenRaster(file)
       
      ; Open an ROI file
;      roiname=infilepath+'incidence_angle_sample.xml'
      file = FILEPATH('incidence_angle_sample.xml', ROOT_DIR=infilepath);, SUBDIRECTORY = ['data']
      rois = e.OpenRoi(file)
      if file_test(infilepath+'incidence_angle_sample.csv') eq 0 then begin
        raster.ExportRois,infilepath+'incidence_angle_sample.csv' , rois, 'CSV'
        print,'文件新建'
      endif
      
      roicsv = READ_CSV( infilepath+'incidence_angle_sample.csv' ,N_TABLE_HEADER=13,TYPES='Float')
      
      filenum=N_elements(roicsv.FIELD1);
      x=roicsv.FIELD1;
      y=roicsv.FIELD2;
      hh=roicsv.FIELD7;
      hv=roicsv.FIELD8;
      angle=roicsv.FIELD9;


      flag=0
      for i=0,filenum-1 do begin
        ;搜集数据
        if i eq 0 or ((i mod 16) eq 0) then begin
          temp1=[hh[i]]
          temp2=[hv[i]]
          temp3=[angle[i]]
        endif else begin
          temp1=[temp1,hh[i]]
          temp2=[temp2,hv[i]]
          temp3=[temp3,angle[i]]
        endelse
        ;判断是否需要提交结果
        if i ne 0 and  (N_elements(temp1) eq 16) then begin ;判断是否与前一个不为同ROI  abs(x[i-1]-x[i]) gt 5 and abs(y[i-1]-y[i])
          if flag eq 0 then begin ;判断是否第一次建立结果数组
            result1=mean(temp1)
            result2=mean(temp2)
            result3=mean(temp3)
            flag=1
          endif else begin
            result1=[result1,mean(temp1)]
            result2=[result2,mean(temp2)]
            result3=[result3,mean(temp3)]          
          endelse      
        endif   

      endfor
      if yr gt 2017 or (yr eq 2017 and mon ge 3) then begin
        final_ice=[[result1[0:19]],[result2[0:19]],[result3[0:19]]]
        final_ice=Transpose(final_ice)
        final_ow=[[result1[20:-1]],[result2[20:-1]],[result3[20:-1]]]
        final_ow=Transpose(final_ow)
      endif else begin
        
        final_ow=[[result1[0:19]],[result2[0:19]],[result3[0:19]]]
        final_ow=Transpose(final_ow)
        final_ice=[[result1[20:-1]],[result2[20:-1]],[result3[20:-1]]]
        final_ice=Transpose(final_ice)
        
      endelse
      
      WRITE_CSV,infilepath+'HH_HV_incidence_angle_statistics_ow.csv',final_ow
      WRITE_CSV,infilepath+'HH_HV_incidence_angle_statistics_ice.csv',final_ice
      print,'hello',infilepath+'HH_HV_incidence_angle_statistics.csv'
    endfor
  endfor
    

end

你可能感兴趣的:(ENVI,idl,ENVI)