-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmask8m.m
126 lines (101 loc) · 2.62 KB
/
mask8m.m
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function m = mask8m(varargin)
% MASK masking algorithm for 8m resolution data
%
% m = mask(demFile)
% returns the mask stucture (m.x,m.y,m.z) for the demFile using the
% given image parameters.
%
% m = mask(...,maxDigitalNumber,previewPlot) maxDigitalNumber is optional
% for rescaling the orthoimage to the original source image range.
% If it's mot included or is empty, no rescaling will be applied. If
% previewPlot == 'true', a *_maskPreview.tif image will be saved to the
% same directory as the demFile that shows the DEM hillshade with and
% without the mask applied.
%
% m = mask(demFile,meta) returns the mask stucture (m.x,m.y,m.z) for the
% demFile and meta structure, where meta is the output of readSceneMeta.
%
% REQUIRED FUNCTIONS: readGeotiff, DataDensityMap, edgeSlopeMask,
% DataDensityMask
%
% Ian Howat, [email protected]
% 25-Jul-2017 12:49:25
%% Parse inputs
previewFlag = false;
demFile=varargin{1};
% intialize output
m = [];
mtFile= strrep(demFile,'dem.tif','matchtag.tif');
%% read data and make initial arrays
% read DEM data
z = readGeotiff(demFile);
% read matchtag
mt = readGeotiff(mtFile);
% size consistency checks
if any(size(z.z) ~= size(mt.z))
error('size of dem and matchtag rasters dont match')
end
% initialize output
m.x = z.x;
m.y = z.y;
m.z = false(size(z.z));
m.info = z.info;
m.Tinfo = z.Tinfo;
% parse structures
x = z.x;
y = z.y;
z = z.z;
z(z == -9999) = NaN;
mt = mt.z;
% make data density map
P = DataDensityMap(mt,21);
% set P no data
P(isnan(z)) = NaN;
%% edge crop
M = edgeSlopeMask(x,y,z);
M(isnan(z)) = false;
% data existence check
if ~any(M(:)); return; end
clear z
%% Data Density Filter
n= 21;
Pmin=0.90;
Amin=1000;
Amax=1000;
M =DataDensityMask(M,'n',n,'Pmin',Pmin,'Amin',Amin,'Amax',Amax);
% data existence check
if ~any(M(:)); return; end
M = bwareaopen(M,500);
% add to structure
m.z = M;
%
if ~previewFlag; return; end
%% Display output
%
% read DEM data
z = readGeotiff(demFile);
x = z.x;
y = z.y;
z = z.z;
z(z == -9999) = NaN;
z_masked = z;
z_masked(~M) = NaN;
rf = 0.25;
z = imresize(z,rf);
x = imresize(x,rf); % only needed for hillshade
y = imresize(y,rf); % only needed for hillshade
z_masked = imresize(z_masked,rf);
hill = hillshade(z,x,y);
hill_masked = hillshade(z_masked,x,y);
h=figure(1);
set(gcf,'color','w','visible','off')
hpos = get(gcf,'position');
hpos(3) = 2000;
hpos(4) = 1000;
set(gcf,'position',hpos);
subplot(1,2,1)
imagesc(hill,'alphadata',~isnan(z)); colormap gray; axis equal
subplot(1,2,2)
imagesc(hill_masked,'alphadata',~isnan(z_masked)); colormap gray; axis equal
print(h,'-dtiff','-r100',strrep(demFile,'_dem.tif','_maskPreview.tif'));
close(h)