Friday, November 20, 2015

Matlab: Masking the Imagery

Matlab操作非常简单,mask文件的背景也是目标影像的背景,以mask影像的背景索引为目标影像的索引赋值为背景值,结果保存在新文件。
待掩膜的影像数据应与Mask.tif具有一致的行列及坐标、空间分辨率信息,如Fig. 1。可以在代码中配置输出影像数据的背景数值,BackValue=-99;。
Fig. 1
 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
% Created by LI Xu
% Version 1.0
% 30 September, 2015

% Description:
% Masking the imageries.

% 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

clear;
clc;


% Mask
maskpath='mask.tif';
[mask, geo]=geotiffread(maskpath);
info=geotiffinfo(maskpath);
index_invalid=find(mask==0);

% Sourse Directory
SouDir='./new';
% Destination Directory
DesDir='./snow';

% All files to be masked
files=dir([SouDir, '/*.tif']);
% BackGround Value
BackValue=-99;

for ii=1:length(files)
    filename=files(ii).name;
    inpath=[SouDir, '/', filename];
    
    image=imread(inpath);
    image(index_invalid)=BackValue;
    
    % Output file path
    otpath=[DesDir, '/', filename];
    geotiffwrite(otpath, image, geo, 'GeoKeyDirectoryTag', info.GeoTIFFTags.GeoKeyDirectoryTag);
    disp([num2str(ii), ':', otpath]);
    
end

disp('********************************');

No comments:

Post a Comment