-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaddStrip2Mosaic.m
290 lines (213 loc) · 7.65 KB
/
addStrip2Mosaic.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
function [N,returnFlag] = addStrip2Mosaic(metaFile,m,dy0,N,dtrans,rmse,varargin)
%addStrip2Meta merge strip into mosaic file
% N = addStrip2Mosaic(metaFile,m,dy0,N,dtrans) adds the strip data
% specified in the *_meta.txt file into the mat file handle m. N is the
% mosaic data/no data byte mask. dtrans are [dz;dx;dy] registration
% offsets to be applied to the strip before merging (where z1=z0+dz). If
% dtrans=[0;0;0], no registration will be applied. If dtrans=[], the
% strip will be corgistered to existing data in the mosaic if exists, or
% inserted without registration if not.
%
% N = addStrip2Mosaic(...,'option',value) where options are:
% 'mergeMethod' can be one of:
% 'feather' Apply a linear weighting between the edges of
% overlapping strips. DEFAULT
%
% 'warp' Force the edge of the new data to align with the edge
% of the mosaic and throw away new data that overlaps.
%
% 'underprint' Only add new data where there is no existing data
%
% 'mask', mask where mask is an nx2 cell array of mask vertices
% coordinates with columns x and y, and each row a polygon
%
% 'maxrmse', value specifies a maximum rmse, over which the results will
% be rejected and nan's will be returned
%
% Subfunctions: readStripInTile, edgeFeather, edgeWarp, coregistedems,
% interpolate2grid
%
% Ian Howat, Ohio State University
% version 1.0; 28-Jul-2016 09:50:32
% version 1.1; 06-Dec-2016 14:29:02
% - changed to NaNs in dtrans triggers coregistration
% Set Defaults
mergeMethod='feather';
mask=cell(1,2);
maxrmse=inf;
minNewPixels=100;
minOverlapPixels=100;
returnFlag = false;
% parse input args
for i=1:2:length(varargin)
switch lower(varargin{i})
case 'mergemethod'
mergeMethod=varargin{i+1};
if ~strcmp(mergeMethod,'feather') && ~strcmp(mergeMethod,'warp') && ~strcmp(mergeMethod,'underprint')
error('unrecognized merge method string')
end
case 'mask'
mask=varargin{i+1};
if ~iscell(mask) || size(mask,2) ~= 2
error('input variable "mask" must be a cell array with two columns (x,y)')
end
case 'maxrmse'
maxrmse=varargin{i+1};
case 'minnewpixels'
minNewPixels=varargin{i+1};
case 'minoverlappixels'
minOverlapPixels=varargin{i+1};
otherwise
error('Unknown input arguments')
end
end
% read this strip data
[x,y,z,mt,or,c,r] = readStripInTile(metaFile,m.x,m.y,'mask',mask);
% check for competely masked file
if isempty(x)
m.dtrans=[m.dtrans,[NaN;NaN;NaN]];
m.rmse=[m.rmse,NaN];
returnFlag=true;
return;
end
% convert mosaic subset area to index
c=find(c);
r=find(r);
if length(c)*length(r) < minOverlapPixels
fprintf('%d pixel overlap is too small, skipping\n',sum(c)*sum(r));
return;
end
%% Check for new coverage
%subset count grid
Nsub=N(r,c);
%quit if too few new pixels added
newPixels=sum(Nsub(:)==0 & ~isnan(z(:)));
if newPixels < minNewPixels
disp('redundant coverage, skipping');
m.dtrans=[m.dtrans,[NaN;NaN;NaN]];
m.rmse=[m.rmse,NaN];
returnFlag=true;
return;
end
A= Nsub ~= 0 & ~isnan(z); % overlapping coverage mask
%% Registration
% if no dtrans given, but there's no overlap to coregister, then set 0
if ~any(A(:)) && isempty(dtrans); dtrans=[0;0;0]; end
% if dtrans is empty and there's overlap, then apply coregistration
if isempty(dtrans) || any(isnan(dtrans))
if sum(A(:)) < minOverlapPixels
fprintf('%d pixel overlap is too small, skipping\n',sum(A(:)));
return;
end
% crop to new coverage
co= find(sum(A) ~= 0,1,'first'):find(sum(A) ~= 0,1,'last');
ro= find(sum(A,2) ~= 0,1,'first'):find(sum(A,2) ~= 0,1,'last');
% co-register strip to mosaic subset
[~,dtrans,rmse] = coregisterdems(...
m.x(1,c(co)),m.y(r(ro),1),m.z(r(ro),c(co)),...
x(co),y(ro),z(ro,co),m.mt(r(ro),c(co)),mt(ro,co));
% coregisterdems returns dtrans values as positive offsets, whereas
% the gcp registration are negative, so need to reverse the sign:
dtrans=-dtrans;
% check for coregistration failure
if isnan(rmse) || rmse > maxrmse
disp('coregistration failure, skipping');
m.dtrans=[m.dtrans,[NaN;NaN;NaN]];
m.rmse=[m.rmse,NaN];
return;
end;
end
% apply dtrans and interpolate back to grid (but only dtrans is not zeros)
if any(dtrans ~= 0)
x = x + dtrans(2);
y = y + dtrans(3);
z = z + dtrans(1);
% interpolate the first dem to the same grid
[z,mt,or] = interpolate2grid(x,y,z,mt,or,m.x(1,c),m.y(r,1));
end
%% Data Merge
% if no overlap, set to underprint
if ~any(A(:)); mergeMethod = 'underprint'; end
% make date grid
dy=~isnan(z).*dy0;
switch mergeMethod
case 'underprint'
n = find(Nsub == 0 & ~isnan(z));
zsub = m.z(r,c);
zsub(n)=z(n);
m.z(r,c)=zsub;
clear z zsub
orsub=m.or(r,c);
orsub(n)=or(n);
m.or(r,c) = orsub;
clear or orsub
dysub=m.dy(r,c);
dysub(n)=dy0;
m.dy(r,c) = dysub;
clear dy dysub
case 'feather'
dx = m.x(1,2)-m.x(1,1);
buff=10*dx+1;
tic;
W = edgeFeather(Nsub~=0,~isnan(z),'buffer',buff);
toc;
% make weighted elevation grid
zsub=m.z(r,c);
A= zsub.*W + z.*(1-W);
n = isnan(zsub) & ~isnan(z); A(n)= z(n);
n = ~isnan(zsub) & isnan(z); A(n)= zsub(n);
% put strip subset back into full array
m.z(r,c) = A;
clear A z
% make weighted ortho grid
or = single(or);
or(or ==0) = NaN;
orsub=single(m.or(r,c));
orsub(orsub==0) = NaN;
A= orsub.*W + or.*(1-W);
A( isnan(orsub) & ~isnan(or))= or( isnan(orsub) & ~isnan(or));
A(~isnan(orsub) & isnan(or))= orsub(~isnan(orsub) & isnan(or));
A(isnan(A)) = 0; % convert back to uint16
A = uint16(A);
m.or(r,c) = A;
clear A or
% make weighted dy grid
dy = single(dy);
dy(dy==0) = NaN;
dysub = single(m.dy(r,c));
dysub(dysub==0) = NaN;
A= dysub.*W + dy.*(1-W);
A( isnan(dysub) & ~isnan(dy))= dy ( isnan(dysub) & ~isnan(dy));
A(~isnan(dysub) & isnan(dy))= dysub(~isnan(dysub) & isnan(dy));
A(isnan(A)) = 0; % convert back to uint16
A = uint16(A);
m.dy(r,c) = A;
clear A dy
clear W
case 'warp'
% merge z by warping edge of z to match zsub
[m.z(r,c),edgeWarpflag] = edgeWarp(m.z(r,c),z);
if edgeWarpflag
disp('redundant coverage, skipping');
m.dtrans=[m.dtrans,[NaN;NaN;NaN]];
m.rmse=[m.rmse,NaN];
returnFlag=true;
return;
end
orsub=m.or(r,c);
orsub(orsub == 0)=or(orsub == 0);
m.or(r,c) = orsub;
clear orsub
dysub=m.dy(r,c);
dysub(dysub == 0)=dy(dysub == 0);
m.dy(r,c) = dysub;
clear dysub
end
% for the matchtag, alwats just straight combination
m.mt(r,c) = m.mt(r,c) | mt;
clear mt
% put coregistration data into file
m.dtrans=[m.dtrans,dtrans(:)];
m.rmse=[m.rmse,rmse];
N(r,c)=~isnan(m.z(r,c));
returnFlag=true;