Monday, June 6, 2016

IDL: MVC

Introduction

MVC(Maximum-value composite)方法计算得出多个矩阵之间对应位置的最大值,空间数据处理中比较常用,尤其是NDVI。这里介绍两个版本的IDL语言MVC函数,其区别在于计算过程的内存占用情况,若输入N个文件,第一版内存占用为N,第二版则仅有1/N,后者相比前者对内存要求较小,适用于单个文件较大的情况。

第一版

 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
;;Created by LI Xu
;;20 November, 2013
;;Version 1.0

;;Discrption
;;MVC, the maximum-value composite method
;;infullname: string array, consist of N files with full path
;;strSaveDir: string, the dirctory of result
;;strflag: string, flag to month or year , etc..


;+
; :Description:
;    Describe the procedure.
;
; :Params:
;    infullname
;    strSaveDir
;    strflag
;
;
;
; :Author: LiXu
;-
pro MVC, infullname, strSavepath

  
  ;;Read one by one
  ok=Query_Tiff(infullname[0], info)
  Cols=info.dimensions[0]
  Rows=info.dimensions[1]
  N_Dim=size(infullname, /n_elements)
  Pics=DblArr(N_Dim, Cols, Rows)
  for ii=0, N_Dim-1 do Begin
    Pics[ii, *, *]=Read_Tiff(infullname[ii], GEOTIFF=GEOVAR)   
  endfor
  
  ;;Run > 
  Result=Pics[0, *, *]
  ;;Loop >
  for jj=1, N_Dim-1 do Begin
    Result=Result>Pics[jj, *, *]   
  endfor
  
  ;;Write a result tiff
  Write_Tiff, strSavePath, Result, GEOTIFF=GEOVAR, /float
  print, strSavePath+' done!'
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
;; Created by LI Xu
;; Version 1.0
;; June 6, 2016

;; Description:
;; MVC Method

;; If you have any question about this code,
;; please do not hesitate to contact me via E-mail:
;; jeremy456@163.com

;; Blog:
;; http://blog.sciencenet.cn/u/lixujeremy
;; http://lixuworld.blogspot.com/

pro MVC_TinyMomory, fullnames, stroutpath
  ;; Create a blank Matrix 
  ok=Query_Tiff(fullnames[0], info)
  Cols=info.dimensions[0]
  Rows=info.dimensions[1]
  N_Dim=size(fullnames, /n_elements)
  OutMat=DblArr(Cols, Rows)  
  for ii=0, N_Dim-1 do Begin
    image=Read_Tiff(fullnames[ii], GEOTIFF=GEOVAR)  
    OutMat=OutMat>image  
  endfor
  
  ;;Write a result tiff
  Write_Tiff, stroutpath, OutMat, GEOTIFF=GEOVAR, /float
  print, stroutpath+' done!'


end

Matlab版

 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
% Created by LI Xu
% Version 1.0
% June 21, 2016

% Description:
% MVC Matlab Version

% If you have any question about this code,
% please do not hesitate to contact me via E-mail: 
% jeremy456@163.com

% Blog:
% http://blog.sciencenet.cn/u/lixujeremy
% http://lixuworld.blogspot.com/

% Note;
% fullpathes: full file pathes in cell
% otpath:   output file path               

function MVC4Image(fullpathes, otpath)
    % First Image
    filepath=fullpathes{1};
    [output, geo]=geotiffread(filepath);
    info=geotiffinfo(filepath);
    [row, col]=size(output);
    
    output=output(:);
    for ii=2:length(fullpathes)
        filepath=fullpathes{ii};
        image=imread(filepath);
        image=image(:);        
        output=[output, image];
        output=max(output, [], 2);   
    end
        
    output=reshape(output, row, col);
    geotiffwrite(otpath, output, geo, 'GeoKeyDirectoryTag', info.GeoTIFFTags.GeoKeyDirectoryTag);
       
end

References

No comments:

Post a Comment