-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfcd_preview.m
More file actions
139 lines (122 loc) · 3.89 KB
/
fcd_preview.m
File metadata and controls
139 lines (122 loc) · 3.89 KB
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
function fcd_preview( vid )
%FCD_PREVIEW Live preview of displacement field extracted from a video
%stream of a checkerboard pattern that is being distorted
%
% SYNOPSIS: fcd_preview( vid )
%
% INPUT vid: Video input object
%
% Keyboard controls during preview:
% 'Spacebar' Pauze/resume preview
% 'r' Reset reference
% 'p' Increase CLim by 50%
% 'l' Decrease CLim by 50%
%
% See also:
% FCD_DISPFIELD
% VIDEOINPUT
%
% Copyright (c) 2017 Sander Wildeman
% Distributed under the MIT License, see LICENSE file
figTitle = 'WMPreview';
hFig = figure('Name', figTitle);
hFig.KeyPressFcn = @keypress_callback;
% take a snapshot to use as reference
Iref = double(getsnapshot(vid));
[rows,cols] = size(Iref);
figTitle = [figTitle ' - ' num2str(cols) 'x' num2str(rows)];
% obtain carrier signal used for demodulation
[kr, ku] = findorthcarrierpks(Iref, 4*pi/min(size(Iref)), Inf);
krad = sqrt(sum((kr-ku).^2))/2;
fIref = fft2(Iref);
cr = getcarrier(fIref, kr, krad);
cu = getcarrier(fIref, ku, krad);
% init filtering window for fft preview
kxvec = fftshift(kvec(cols));
kyvec = fftshift(kvec(rows));
wr = hann(rows,'periodic');
wc = hann(cols,'periodic');
win2d = wr(:)*wc(:)';
% init raw footage preview
axRaw = axes('Position', [0, 0, 1/3, 1]);
colormap(axRaw, gray(256))
iRaw = image(Iref); % set reference image as place holder
axis image
axis off
% fft preview
axFFT = axes('Position', [1/3, 0, 1/3, 1]);
colormap(axFFT, flip(gray(256)))
fftIm = fftshift(abs(fft2((Iref-mean(Iref(:))).*win2d)));
iFFT = image(kxvec, kyvec, fftIm);
iFFT.CDataMapping = 'scaled';
axFFT.CLim = [0,max(fftIm(:))/50];
hold on
crh = cr.plot('color','y','marker','none');
cuh = cu.plot('color','g','marker','none');
hold off
axis image
axis off
% profile preview
axH = axes('Position', [2/3, 0, 1/3, 1]);
colormap(axH, parula(256))
iH = image(zeros(rows, cols));
iH.CDataMapping = 'scaled';
axH.CLim = [0,pi/sqrt(sum(kr.^2))];
axis image
axis off
% start preview
timerh = tic;
tprev = 0;
prev_running = true;
setappdata(iRaw,'UpdatePreviewWindowFcn',@preview_callback);
preview(vid, iRaw)
function preview_callback(~, event, hImage)
Idef = double(event.Data);
% update previews
% raw
hImage.CData = Idef;
% fft
absFFT = fftshift( abs( fft2( (Idef-mean(Idef(:))).*win2d ) ) );
iFFT.CData = absFFT;
axFFT.CLim = [0,max(absFFT(:))/50];
% profile
[u,v] = fcd_dispfield(fft2(Idef), cr, cu, false);
nrmU = sqrt(u.^2+v.^2);
iH.CData = nrmU;
% display frame rate in title
tcur = toc(timerh);
hFig.Name = [figTitle ' - ' num2str(1/(tcur-tprev),'%.1f') ' FPS'];
tprev = tcur;
end
function keypress_callback(~, event)
switch event.Key
case 'space'
if prev_running
stoppreview(vid)
else
preview(vid,iRaw)
end
prev_running = ~prev_running;
case 'r'
stoppreview(vid)
Iref = iRaw.CData;
[kr, ku] = findorthcarrierpks(Iref, 4*pi/min(size(Iref)), Inf);
krad = sqrt(sum((kr-ku).^2))/2;
fIref = fft2(Iref);
cr = getcarrier(fIref, kr, krad);
cu = getcarrier(fIref, ku, krad);
delete(crh); delete(cuh);
axes(axFFT);
hold on;
crh = cr.plot('color','y','marker','none');
cuh = cu.plot('color','g','marker','none');
hold off;
disp('Iref is reset')
preview(vid,iRaw);
case 'p'
axH.CLim = axH.CLim/1.5;
case 'l'
axH.CLim = axH.CLim*1.5;
end
end
end