-
Notifications
You must be signed in to change notification settings - Fork 19
/
run_pixel_offset.m
467 lines (424 loc) · 17.5 KB
/
run_pixel_offset.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
%%_________________________________________________________________________
%% DIC using FFT-approach and optional pre- and post-processing
%% GIT VERSION
%% MAIN
%%_________________________________________________________________________
%{
for 8bit, equal dim, single- and three-, and four-band images, with
GIS-ready geotiff preview
*Pre-processing*: Wallis filter, Co-Registration
*Post-processing*: RMSE threshold, mean, median, spatial vector filter
___________________________________________________________________________
STRUCTURE:
|PARAMETERS - modify
|CODE - don't modify
___________________________________________________________________________
V. Bickel & A. Manconi 21.5.2020
valentin.bickel@erdw.ethz.ch / andrea.manconi@erdw.ethz.ch
ETH Zurich / MPS Goettingen
---------------------------------------------------------------------------
MIT License
Copyright (c) 2018 Valentin Tertius Bickel & Andrea Manconi
---------------------------------------------------------------------------
Please cite this routine as:
Bickel, V.T.; Manconi, A.; Amann, F.
"Quantitative assessment of Digital Image Correlation methods to detect
and monitor surface displacements of large slope instabilities."
Remote Sens. 2018, 10(6), 865.
%}
%%_________________________________________________________________________
%%
warning off;clc
clear
clc
%%_________________________________________________________________________
%% INPUTS
tic
coreg = 0; % 0 = full image, 1 = define patch
geotiff = 0; % choose if you want to output a geotiff preview, no = 0, yes = 1;
way = 1; % define way to output quickview getiff, default 8bit output = 1 , alternative 16bit output = 2. NOTE: choice changes unit of preview!!!
epsg = 21781; % EPSG code, specify in case you would like to output geotiff preview
yourtext = 'dic_run_00'; % specify the name of the output
pix = 0.25; % Pixel size or GSD (in meters), only required if geotiff = 0
cd Input
if geotiff == 1
% Primary image
[primary, R] = geotiffread('YOURIMAGEHERE.tif'); % read
% Secondary image
[secondary, ~] = geotiffread('YOURIMAGEHERE.tif'); % read
pix = R.CellExtentInWorldY; % GSD in meters/pixel taken from the geotiff
latcoord = R.YWorldLimits(1):R.CellExtentInWorldY:R.YWorldLimits(2);
loncoord = R.XWorldLimits(1):R.CellExtentInWorldX:R.XWorldLimits(2);
else
%Primary image
[primary] = imread('YOURIMAGEHERE.tif'); % read
%Secondary image
[secondary] = imread('YOURIMAGEHERE.tif'); % read
end
cd ..
%%_________________________________________________________________________
%% Pre-processing: Wallis filter
cd DIC
disp '<<< Running! >>>'
wallis = 0; % wallis = 0, wallis filtering off, wallis = 1, wallis filtering on
win = 20; % block size = EVEN NUMBERS ONLY
tarm = 150; % target mean
tars = 150; % target standard deviation
b=1; % brightness enforcing constant
c=0.9995; % contrast enforcing constant
if wallis == 1
disp '<<< Pre-processing: Wallis filter >>>'
tic
[primary,secondary] = wallis_filter(primary,secondary,win,tarm,tars,b,c);
toc
end
clear wallis tarm tars b c win
%%_________________________________________________________________________
%% Co-registration parameters
sp = 1; % Image split for Co-registration
co_os = 1; % Image oversampling for Co-registration
%% DIC parameters
% Offset type
wi = 128; % Window size [pix]
os = 1; % Oversampling factor
% Filter type
filter = 1; % 1 = threshold filter
% 2 = arithmetic mean filter
% 3 = vector filter
% 4 = median filter
% #1
thr = 1; % masking threshold (0-1, 1=no mask)
% #2
mfws = [6 6]; % mean filter window size for arithmetic mean filter
cut = 12; % window size/cut = cut off filter for a_mean filter, OPTIONAL
% #3
magcap = wi; % tolerance-diff, values which are greater as this value are cut [pixel]
xcap = wi/4; % tolerance-diff, values which are greater as this value are cut [pixel]
ycap = wi/4; % tolerance-diff, values which are greater as this value are cut [pixel]
% #4
med = [5 5]; % dimensions of median filter window [pixel]
% Colorscale -------------------------------------------
% min and max values for the X displacement colorscale, in meters
scalax=[-5 5];
% min and max values for the Y displacement colorscale, in meters
scalay=[-5 5];
% Additional definitions --------------------------------------------------
coppia = 't1-t0';
% Additional PO analysis parameters
skip_x = wi/2; % half size of the window = Nyquist is happy!
skip_y = wi/2;
skip = skip_x;
%%_________________________________________________________________________
%% Co-Registration
if coreg == 1
imagesc(primary(:,:,1)); colormap gray; title("NOTE: if bbox size is too small, coreg might fail")
set(gcf,'position',get(0,'ScreenSize'));
rect = getrect(gca);
close
else
rect = 1;
end
disp '<<< Co-registration >>>'
tic
[T0,T1] = co_registration(primary,secondary,sp,co_os,coreg,rect);
toc
clear sp co_os coreg
%%_________________________________________________________________________
%% DIC
disp '<<< Running FFT >>>'
tic
[RR] = pixoff(T0,T1,skip_x,skip_y,wi,os,coppia);
clear os
% txt ASCII output
ii = (min(RR(:,1)):skip:max(RR(:,1)));
jj = (min(RR(:,2)):skip:max(RR(:,2)));
DX = RR(:,4).*pix; % left right displacement (E W) + pixel to meter
DY = RR(:,3).*pix; % up down displacement (N S) + pixel to meter
D2D = sqrt(DX.^2+DY.^2); % displacement resultant (magnitude)
RMSE = RR(:,5); % RMSE taken from the FFT correlation
% orientation correction, because Matlab is weird
DX_mat = flipud(rot90(reshape(DX, [size(jj,2),size(ii,2)]))); % reshape to matrix and correct
DY_mat = flipud(rot90(reshape(DY, [size(jj,2),size(ii,2)])));
D2D_mat = flipud(rot90(reshape(D2D, [size(jj,2),size(ii,2)])));
RMSE_mat = flipud(rot90(reshape(RMSE, [size(jj,2),size(ii,2)])));
DX = reshape(flipud(DX_mat)',size(D2D)); % reshape to vector
DY = reshape(flipud(DY_mat)',size(D2D));
D2D = reshape(flipud(D2D_mat)',size(D2D));
RMSE = reshape(flipud(RMSE_mat)',size(D2D));
if geotiff == 1
res = [loncoord(RR(:,2))',latcoord(RR(:,1))',DX,DY,D2D,RMSE];
else
res = [RR(:,2),RR(:,1),DX,DY,D2D,RMSE];
end
cd ..
addpath(genpath('DIC'))
cd Output
saveascii(res,[yourtext,'.txt'],2, ','); % LONcoord | LATcoord | xoff(m) | yoff(m) | horizontal resultant (m) | RMSE
cd ..
t = RR;
nx = round(size(T0,2)/5); ny = round(size(T0,1)/10);
clear ii jj DX DY RMSE D2D DX_mat DY_mat D2D_mat RMSE_mat res
ii=1:numel(min(t(:,1)):skip_x:max(t(:,1))); jj=1:numel(min(t(:,2)):skip_x:max(t(:,2)));
[I,J]=meshgrid(ii,jj);
toc
%%_________________________________________________________________________
%% Filtering
disp '<<< Post-processing: Filtering >>>'
tic
%_#1_______________________________________________________________
% Threshold correlation filter
if filter == 1
filter_selection = 1;
pp = t(:,end)<thr; % gives a 1 true or 0 not true
end
%__________________________________________________________________
%_#2_______________________________________________________________
% Arithmetic mean filter
if filter == 2
filter_selection = 2;
[tpostfiltx,tpostfilty] = mean_filt(T0,t,mfws,wi);
end
%__________________________________________________________________
%_#3_______________________________________________________________
% Vector filter
if filter == 3
filter_selection = 3;
[tot_check] = vec_filt(t,T0,wi,ycap,xcap,magcap);
end
%__________________________________________________________________
%_4________________________________________________________________
% Median filter
if filter == 4
filter_selection = 2;
[tpostfiltx,tpostfilty] = med_filt(T0,t,med,wi);
end
%__________________________________________________________________
toc
%%_________________________________________________________________________
%% Forwarding Geotiff information
disp '<<< Post-processing: geotiff preview >>>'
tic
if geotiff == 1
[a,b] = size(J);
if filter_selection == 1
pp(pp == 0) = 1; % vector correction for reshape
outfigure = reshape((sqrt(t(pp,3).^2+t(pp,4).^2)*pix),a,b);
outfigure=outfigure';
outfigure(:,end)=outfigure(:,a-1);
end
if filter_selection == 2
outfigure = reshape((sqrt(tpostfiltx.^2+tpostfilty.^2)*pix),a,b);
outfigure=outfigure';
outfigure(:,end)=outfigure(:,a-1);
end
if filter_selection == 3
outfigure = reshape((tot_check(:,5)*pix),a,b);
outfigure=outfigure';
outfigure(:,end)=outfigure(:,a-1);
end
load mycmap_s.mat % custom colormap, choose if custom or Matlab map is used for the preview
%mycmap = colormap(jet); % Matlab colormap
%close
Q2 = imresize(outfigure,size(primary(:,:,1)),'near');
cd Output
if way == 1
geotiffwrite([yourtext,'.tif'], uint8(Q2), mycmap,R,'CoordRefSysCode',epsg);
end
if way == 2
geotiffwrite([yourtext,'.tif'], uint16(Q2*1000),R,'CoordRefSysCode',epsg); % factor 1000 to maintain small signals in the 16bit output, adapt as required
% NOTE: the original output is in m/pix, i.e., applying a factor of e.g. *1000 will result in a unit of mm/pix for the preview output !!!
end
cd ..
end
toc
%%_________________________________________________________________________
%% Plotting
% Multi-band reduction of input images, if required
[x, y, z] = size(primary);
if z > 3
orig_m_int(:,:,1) = primary(:,:,1);
orig_m_int(:,:,2) = primary(:,:,2);
orig_m_int(:,:,3) = primary(:,:,3);
orig_m = orig_m_int;
orig_s_int(:,:,1) = secondary(:,:,1);
orig_s_int(:,:,2) = secondary(:,:,2);
orig_s_int(:,:,3) = secondary(:,:,3);
orig_s = orig_s_int;
else
orig_m = primary;
orig_s = secondary;
end
clear primary secondary orig_m_int orig_s_int
% Plot 2D offset and displacement vectors
if filter_selection == 1 % RMSE threshold filter __________________
figure(1)
h = subplot(2,2,1);
imshow(orig_m)
caxis([-100 180])
colormap(h,gray);
title('primary image')
box on; axis on; grid on; grid minor
ax = gca;
ax.GridAlpha=0.8;
xticks([nx 2*nx 3*nx 4*nx 5*nx])
yticks([ny 2*ny 3*ny 4*ny 5*ny 6*ny 7*ny 8*ny 9*ny 10*ny])
i = subplot(2,2,2);
imshow(orig_s)
caxis([-100 180])
colormap(i,gray);
title('secondary image')
box on; axis on; grid on; grid minor
ax = gca;
ax.GridAlpha=0.8;
xticks([nx 2*nx 3*nx 4*nx 5*nx])
yticks([ny 2*ny 3*ny 4*ny 5*ny 6*ny 7*ny 8*ny 9*ny 10*ny])
g = subplot(2,2,3);
twoD=sqrt(t(pp,3).^2+t(pp,4).^2);
scatter(t(pp,2),t(pp,1),30,(twoD.*pix),'filled','s','MarkerFaceAlpha',.95,'MarkerEdgeAlpha',.95)
colormap(g,jet);
title('2D Offset magnitude')
caxis(scalay)
xlim([0 size(T0,2)])
ylim([0 size(T0,1)])
box on; axis on; grid on; grid minor
ax = gca;
ax.GridAlpha=0.8;
xticks([nx 2*nx 3*nx 4*nx 5*nx])
yticks([ny 2*ny 3*ny 4*ny 5*ny 6*ny 7*ny 8*ny 9*ny 10*ny])
pbaspect([size(T0,2) size(T0,1) 1])
set(gca,'YDir','reverse')
gc=colorbar('EastOutside');
xlabel(gc,'pixel units')
m = subplot(2,2,4);
quiver(t(:,2),t(:,1),(t(:,4)*pix),(t(:,3)*pix),5,'b')
colormap(m,jet);
title('Displacement vectors')
xlim([0 size(T0,2)])
ylim([0 size(T0,1)])
box on; axis on; grid on; grid minor
ax = gca;
ax.GridAlpha=0.8;
xticks([nx 2*nx 3*nx 4*nx 5*nx])
yticks([ny 2*ny 3*ny 4*ny 5*ny 6*ny 7*ny 8*ny 9*ny 10*ny])
pbaspect([size(T0,2) size(T0,1) 1])
set(gca,'YDir','reverse')
linkaxes([h,g,i,m], 'xy')
end
if filter_selection == 2 % Mean & Median filter ___________________
figure(1)
h = subplot(2,2,1);
imshow(orig_m)
caxis([-100 180])
colormap(h,gray);
title('primary image')
box on; axis on; grid on; grid minor
ax = gca;
ax.GridAlpha=0.8;
xticks([nx 2*nx 3*nx 4*nx 5*nx])
yticks([ny 2*ny 3*ny 4*ny 5*ny 6*ny 7*ny 8*ny 9*ny 10*ny])
i = subplot(2,2,2);
imshow(orig_s)
caxis([-100 180])
colormap(i,gray);
title('secondary image')
box on; axis on; grid on; grid minor
ax = gca;
ax.GridAlpha=0.8;
xticks([nx 2*nx 3*nx 4*nx 5*nx])
yticks([ny 2*ny 3*ny 4*ny 5*ny 6*ny 7*ny 8*ny 9*ny 10*ny])
g = subplot(2,2,3);
twoD=sqrt(tpostfiltx.^2+tpostfilty.^2);
scatter(t(:,2),t(:,1),30,(twoD*pix),'filled','s','MarkerFaceAlpha',.95,'MarkerEdgeAlpha',.95)
colormap(g,jet);
title('2D Offset magnitude')
caxis(scalay)
xlim([0 size(T0,2)])
ylim([0 size(T0,1)])
box on; axis on; grid on; grid minor
ax = gca;
ax.GridAlpha=0.8;
xticks([nx 2*nx 3*nx 4*nx 5*nx])
yticks([ny 2*ny 3*ny 4*ny 5*ny 6*ny 7*ny 8*ny 9*ny 10*ny])
pbaspect([size(T0,2) size(T0,1) 1])
set(gca,'YDir','reverse')
gc=colorbar('EastOutside');
xlabel(gc,'pixel units')
m = subplot(2,2,4);
quiver(t(:,2),t(:,1),(tpostfiltx(:,1)*pix),(tpostfilty(:,1)*pix),5,'b')
colormap(m,jet);
title('Displacement vectors')
xlim([0 size(T0,2)])
ylim([0 size(T0,1)])
box on; axis on; grid on; grid minor
ax = gca;
ax.GridAlpha=0.8;
xticks([nx 2*nx 3*nx 4*nx 5*nx])
yticks([ny 2*ny 3*ny 4*ny 5*ny 6*ny 7*ny 8*ny 9*ny 10*ny])
pbaspect([size(T0,2) size(T0,1) 1])
set(gca,'YDir','reverse')
linkaxes([h,g,i,m], 'xy')
end
if filter_selection == 3 % Vector filter __________________________
figure(1)
h = subplot(2,2,1);
imshow(orig_m)
caxis([-100 180])
colormap(h,gray);
title('primary image')
box on; axis on; grid on; grid minor
ax = gca;
ax.GridAlpha=0.8;
xticks([nx 2*nx 3*nx 4*nx 5*nx])
yticks([ny 2*ny 3*ny 4*ny 5*ny 6*ny 7*ny 8*ny 9*ny 10*ny])
i = subplot(2,2,2);
imshow(orig_s)
caxis([-100 180])
colormap(i,gray);
title('secondary image')
box on; axis on; grid on; grid minor
ax = gca;
ax.GridAlpha=0.8;
xticks([nx 2*nx 3*nx 4*nx 5*nx])
yticks([ny 2*ny 3*ny 4*ny 5*ny 6*ny 7*ny 8*ny 9*ny 10*ny])
g = subplot(2,2,3);
scatter(t(:,2),t(:,1),30,(tot_check(:,5)*pix),'filled','s','MarkerFaceAlpha',.95,'MarkerEdgeAlpha',.95)
colormap(g,jet);
title('2D Offset magnitude')
caxis(scalay)
xlim([0 size(T0,2)])
ylim([0 size(T0,1)])
box on; axis on; grid on; grid minor
ax = gca;
ax.GridAlpha=0.8;
xticks([nx 2*nx 3*nx 4*nx 5*nx])
yticks([ny 2*ny 3*ny 4*ny 5*ny 6*ny 7*ny 8*ny 9*ny 10*ny])
pbaspect([size(T0,2) size(T0,1) 1])
set(gca,'YDir','reverse')
gc=colorbar('EastOutside');
xlabel(gc,'pixel units')
m = subplot(2,2,4);
quiver(t(:,2),t(:,1),(tot_check(:,4)*pix),(tot_check(:,3)*pix),5,'b')
colormap(m,jet);
title('Displacement vectors')
xlim([0 size(T0,2)])
ylim([0 size(T0,1)])
box on; axis on; grid on; grid minor
ax = gca;
ax.GridAlpha=0.8;
xticks([nx 2*nx 3*nx 4*nx 5*nx])
yticks([ny 2*ny 3*ny 4*ny 5*ny 6*ny 7*ny 8*ny 9*ny 10*ny])
pbaspect([size(T0,2) size(T0,1) 1])
set(gca,'YDir','reverse')
linkaxes([h,g,i,m], 'xy')
end
%% END OF SCRIPT
%{
MIT License
Copyright (c) 2018 Valentin Tertius Bickel & Andrea Manconi
---------------------------------------------------------------------------
Please cite this routine as:
Bickel, V.T.; Manconi, A.; Amann, F.
"Quantitative assessment of Digital Image Correlation methods to detect
and monitor surface displacements of large slope instabilities."
Remote Sens. 2018, 10(6), 865.
%}