-
Notifications
You must be signed in to change notification settings - Fork 110
/
inpaintnans.m
executable file
·262 lines (228 loc) · 6.84 KB
/
inpaintnans.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
function DEM = inpaintnans(DEM,varargin)
%INPAINTNANS interpolate missing values in a grid (GRIDobj)
%
% Syntax
%
% DEMf = inpaintnans(DEM,type)
% DEMf = inpaintnans(DEM,type,k)
% DEMf = inpaintnans(DEM,type,k,conn)
% DEMf = inpaintnans(DEM,DEM2)
% DEMf = inpaintnans(DEM,DEM2,method)
%
% Description
%
% inpaintnans fills gaps in a grid (GRIDobj) generated by measurement
% errors or missing values. The user may choose between different
% techniques to fill the gaps. Note that the algorithm fills only
% pixels not connected to the DEM grid boundaries.
%
% inpaintnans(DEM,type) or inpaintnans(DEM,type,k) fills missing values
% in the DEM based on the values surrounding regions with missing values.
%
% inpaintnans(DEM,DEM2) or inpaintnans(DEM,DEM2,method) fills missing
% values using interpolation from another grid. An example is that
% missing values in a SRTM DEM could be filled with values derived from
% an ASTER GDEM.
%
% Input
%
% DEM digital elevation model with missing values
% indicated by nans (GRIDobj)
% type fill algorithm
% 'laplace' (default): laplace interpolation
% as implemented in roifill
% 'fill': elevate all values in each connected
% region of missing values to the minimum
% value of the surrounding pixels (same as
% the function nibble in ArcGIS Spatial Analyst)
% 'nearest': nearest neighbor interpolation
% using bwdist
% 'neighbors': this option does not close all nan-regions. It
% adds a one-pixel wide boundary to the valid values in
% the DEM and derives values for these pixels by a
% distance-weighted average from the valid neighbor
% pixels. This approach does not support the third input
% argument k.
% k if supplied, only connected components with
% less or equal number of k pixels are filled. Others
% remain nan. Set to inf if all pixels enclosed by non-nan
% pixels should be filled.
% conn Connectivity, specified as scalar 4 or 8
% DEM2 if the second input argument is a GRIDobj, inpaintnans will
% interpolate from DEM2 to locations of missing values in
% DEM.
% method interpolation method if second input argument is a GRIDobj.
% {'linear'},'nearest','spline','pchip', or 'cubic'.
%
% Output
%
% DEM processed digital elevation model (GRIDobj)
%
% Example
%
% DEM = GRIDobj('srtm_bigtujunga30m_utm11.tif');
% DEM.Z(300:400,300:400) = nan;
% subplot(1,2,1)
% imageschs(DEM,[],'colorbar',false)
% DEMn = inpaintnans(DEM);
% subplot(1,2,2);
% imageschs(DEMn,[],'colorbar',false)
%
%
% See also: ROIFILL, FILLSINKS, BWDIST
%
% Author: Wolfgang Schwanghart (w.schwanghart[at]geo.uni-potsdam.de)
% Date: 3. September, 2018
if nargin == 1
DEM.Z = deminpaint(DEM.Z,varargin{:});
elseif ischar(varargin{1})
if strcmpi(varargin{1},'neighbors')
DEM = interpneighborpixels(DEM);
return
end
DEM.Z = deminpaint(DEM.Z,varargin{:});
elseif isa(varargin{1},'GRIDobj')
if nargin == 2
method = 'linear';
else
method = varargin{2};
method = validatestring(method,...
{'linear','nearest','spline','pchip','cubic','tt'},'GRIDobj/inpaintnans','method',3);
end
switch method
case 'tt'
DEM = ttinpaint(DEM,varargin{1});
otherwise
INAN = isnan(DEM);
IX = find(INAN.Z);
[x,y] = ind2coord(DEM,IX);
znew = interp(varargin{1},x,y,method);
DEM.Z(IX) = znew;
end
end
end
function dem = deminpaint(dem,type,k,conn)
if nargin == 1
type = 'laplace';
k = inf;
conn = 8;
elseif nargin == 2
k = inf;
conn = 8;
elseif nargin == 3
conn = 8;
end
% error checking
% clean boundary
I = isnan(dem);
% imclearborder
% I = imclearborder(I,conn);
marker = I;
marker(2:end-1,2:end-1) = false;
I = ~imreconstruct(marker,I) & I;
clear marker
if ~isinf(k)
I = xor(bwareaopen(I,k+1,conn),I);
end
%
if numel(dem) < 10000^2 || ~strcmpi(type,'laplace')
% interpolation
switch lower(type)
case 'nearest'
% nearest neighbor interpolation
[~,L] = bwdist(~I);
dem = dem(L);
case 'laplace'
% -- use roifill (Code before 2015a)
% dem = roifill(dem,imdilate(I,ones(3)));
% -- use regionfill (Code after and including 2015a)
dem = regionfill(dem,I);
case 'fill'
% fill to lowest surrounding neighbor
marker = inf(size(dem),class(dem));
markerpixels = imdilate(I,ones(3)) & ~I;
marker(markerpixels) = dem(markerpixels);
mask = dem;
mask(I | isnan(dem)) = -inf;
marker = -marker;
mask = -mask;
demrec = imreconstruct(marker,mask,conn);
dem(I) = -demrec(I);
otherwise
error('type unknown')
end
else
CC = bwconncomp(I,conn);
STATS = regionprops(CC,'SubarrayIdx','Image');
for r = 1:numel(STATS)
rows = STATS(r).SubarrayIdx{1};
rows = [min(rows)-1 max(rows)+1];
cols = STATS(r).SubarrayIdx{2};
cols = [min(cols)-1 max(cols)+1];
demtemp = dem(rows(1):rows(2),cols(1):cols(2));
inatemp = padarray(STATS(r).Image,[1 1],false);
% -- Code before 2015a
% demtemp = roifill(demtemp,imdilate(inatemp,ones(3)));
% -- Code after and including 2015a
demtemp = regionfill(demtemp,inatemp);
dem(rows(1):rows(2),cols(1):cols(2)) = demtemp;
end
end
end
function DEM = ttinpaint(DEM,DEM2)
INAN = isnan(DEM);
INAN.Z = imclearborder(INAN.Z);
B = dilate(INAN,ones(5)) & ~INAN;
IX = find(B);
[x,y] = ind2coord(B,IX);
z = interp(DEM2,x,y);
DIFF = DEM;
DIFF.Z(IX) = z-DIFF.Z(IX);
DIFF = inpaintnans(DIFF,'laplace');
DEM = inpaintnans(DEM,DEM2);
DEM.Z(INAN.Z) = DEM.Z(INAN.Z)+DIFF.Z(INAN.Z);
end
function DEM = interpneighborpixels(DEM)
%INTERPNEIGHBORPIXELS Interpolate pixels from their neighbor pixels
%
% Syntax
%
% DEMi = interpneighborpixels(DEM)
%
% Description
%
% INTERPNEIGHBORPIXELS uses distance weighted averages to calculate
% missing values (nans) for pixels that border pixels with valid
% values.
%
% Input arguments
%
% DEM GRIDobj
%
% Output arguments
%
% DEMi GRIDobj with
%
I = isnan(DEM.Z);
sq2 = sqrt(2);
w = 1./[sq2 1 sq2; ...
1 0 1; ...
sq2 1 sq2];
w = w(:);
Z = nlfilter(DEM.Z,[3 3],@fun);
DEM.Z = Z;
function b = fun(a)
a = a(:);
if ~isnan(a(5))
b = a(5);
return
end
I = isnan(a);
if all(I)
b = nan;
return
end
I = ~I;
b = sum(a(I).*(w(I)./sum(w(I))));
end
end