Thursday, November 19, 2015

ENVI+IDL: Resampling

问题:已知一个影像,对另一影像像元分辨率按照已知影像进行重采样?
方法:ENVI+IDL。
练习数据包括两张GEOTIFF文件,具有相同的空间参考,STANDARD.tif是已知影像,像元分辨率:1000*1000METERS;reprojected.tif是被重采样影像,像元分辨率:799.232051*799.232051METERS。
(原来,我尝试Python+GDAL进行重投影和重采样,但没发现Python+GDAL重采样的代码,所以就分别做处理了。)
reprojected.tif与resampled.tif前后转换对比,如图 1(似乎没什么不同?)。
图 1
注意:土地利用数据的重采样方法应选择最邻近法,它基于离散数据在插值过程中不会产生新的数值,适用于类似土地利用分类数据的插值操作(NEAREST—Performs a nearest neighbor assignment, is the fastest of the interpolation methods. It is used primarily for discrete data, such as a land-use classification, since it will not change the values of the cells. The maximum spatial error will be one-half the cell size.)。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
;;Created by LI Xu
;;Version 1.0
;;16 october, 2014


pro Resample_Doit_Batch
  print, 'Time Begin:'+' '+systime()
  begintime=SYSTIME(1)

  ;;Standard Image
  stdimgpath='E:\Tools\rad\pro\mask.tif'
  ;;Source Directory
  SouDir='E:\Tools\rad\Rad'
  ;;Destination Directory
  DesDir='E:\Tools\rad\NEW'
  ;;Interpolation method
  ;;Interp
  ;;0: Nearest neighbor
  ;;1: Bilinear
  ;;2: Cubic convolution
  ;;3: Pixel aggregate
  intermed=3
  
  ;;Retrieve all files
  files=file_search(SouDir, '*.tif', count=file_counts, /test_regular)
  for ii=0, file_counts-1 do begin
    file=files(ii)
    filename=strsplit(file, '\', count=str_num ,/EXTRACT)
    filename=filename(str_num-1)
    otfile=DesDir+'\'+filename
    ;print, otfile
    Resample_Doit, stdimgpath, file, intermed, otfile
  endfor
  
  print, 'End!'
  print, 'Time End:'+' '+systime()
  endtime=systime(1)
  timespan=endtime-begintime
  print, 'Time Span:'+' '+string(timespan)+' s'

end

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
;;Created by LI Xu
;;Version 1.0
;;16 October, 2014

;;Resampling x/y resolution size
;;Reference:
;;http://www.cnblogs.com/myyouthlife/archive/2012/07/01/2571785.html

;;Modified by LI Xu
;;Version 1.1
;;October 19, 2015
;;Add the variable for interpolating method


pro Resample_Doit, standardtif, resampltif, intemed, ottif
  
  ;;standardtif='D:\ForLIU\MOD_NDVI.tif'
  ;;resampltif='D:\ForLIU\LI_reprojected.tif'
  ;;ottif='D:\ForLIU\LI_resampled.tif'
  
  ;;*********************************
  compile_opt IDL2
  envi, /restore_base_save_files
  
  ;;Open the tifs
  ;;MODELPIXELSCALETAG DOUBLE[3] XYZ
  stdimg=read_tiff(standardtif, GEOTIFF=GEOVAR)
  tarXsize=GEOVAR.MODELPIXELSCALETAG(0)
  tarYsize=GEOVAR.MODELPIXELSCALETAG(1)
  envi_open_file, resampltif, r_fid=fid_resampled
  resampltif=read_tiff(resampltif, GEOTIFF=GEOVAR)
  nowXsize=GEOVAR.MODELPIXELSCALETAG(0)
  nowYsize=GEOVAR.MODELPIXELSCALETAG(1)
  envi_file_query, fid_resampled, dims=dims, nb=nb
  ;;DIMS[0]: A pointer to an open ROI; use only in cases where ROIs define the spatial subset. Otherwise, set to -1L.
  ;;DIMS[1]: The starting sample number. The first x pixel is 0.
  ;;DIMS[2]: The ending sample number
  ;;DIMS[3]: The starting line number. The first y pixel is 0.
  ;;DIMS[4]: The ending line number
  pos = lindgen(nb)
  
  Xsize=tarXsize/nowXsize
  Ysize=tarYsize/nowYsize
  envi_doit, 'resize_doit', $
    fid=fid_resampled, pos=pos, dims=dims, $
    interp=intemed, rfact=[Xsize, Ysize], $
    out_name=ottif, r_fid=r_fid
  ;;Interp  
  ;;0: Nearest neighbor
  ;;1: Bilinear
  ;;2: Cubic convolution
  ;;3: Pixel aggregate

end

References

No comments:

Post a Comment