-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpCloudICP_ConstV.m
278 lines (207 loc) · 7.74 KB
/
pCloudICP_ConstV.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
clearvars;
close all;
message = 'ACQUIRE? YES=1, NO=0 ';
prompt = input(message);
clear message;
if prompt == 1
% Acquire
clear LabSceneData;
% Specify numFrames for acquisition length.
numFrames = 200;
framesPerTrigger = 50;
[LabSceneData,~,~, trackingCoords,isTracked] = kinectPtCloudAcquire(...
numFrames, framesPerTrigger);
else
clear prompt;
% Load data otherwise
dataFile = fullfile(pwd,'Yaw_test');
load(dataFile);
clear dataFile;
end
% Initialisation Block
% Sequence Crop
initFrame = 1;
finalFrame = 2000;
if finalFrame > length(LabSceneData)
finalFrame = length(LabSceneData)
end
% Enable frame-by-frame point cloud visualiser for debugging
showPtCloudComparison = false;
% Initialise point cloud reference objects (denoise)
ptCloudCurrent = pcdenoise(LabSceneData{initFrame});
% Downsample to improve speed and accuracy
gridSize = 0.1;
fixed = pcdownsample(ptCloudCurrent, 'gridAverage', gridSize);
moving = fixed; %initialise
% Initialise Variables
tform = affine3d(eye(4));
ptCloudAligned = ptCloudCurrent;
ptCloudScene = ptCloudCurrent;
mergeSize = 0.015;
droppedFrameCount = 0;
L2dist = zeros(1,finalFrame);
dT(finalFrame) = 0;
dV(finalFrame) = 0;
% Initialise the transformation object that accumulates the transformation.
accumTform = tform;
% Initialise Viewer. Y-axis vertical as specified by Kinect coordinate
% system
figure
hAxes = pcshow(ptCloudScene, 'VerticalAxis','Y', 'VerticalAxisDir', 'Down');
title('Updated world scene')
% Set the axes property for faster rendering
hAxes.CameraViewAngleMode = 'auto';
hScatter = hAxes.Children;
% Initialise camCloud as a pointCloud axes object purely for visualisation.
% camTrack used to store coordinates.
camCloud = zeros(31,3);
camCloud(2:11,1) = 0.01:0.01:0.1;
camCloud(12:21,2) = 0.01:0.01:0.1;
camCloud(22:31,3) = 0.01:0.01:0.1;
camTrack = zeros(finalFrame,7);
camCloud = pointCloud(camCloud);
camColor = zeros(31,3);
camColor(2:11,1) = 255;
camColor(12:21,2) = 255;
camColor(22:31,3) = 255;
camCloud.Color = uint8(camColor);
clear camColor;
% Main Processing Loop
for i = initFrame+1:finalFrame
disp('Processing Frame: ');
disp(num2str(i));
% Line up next frame
ptCloudCurrent = LabSceneData{1,i};
% Use previous moving point cloud as reference.
fixed = moving;
moving = pcdownsample(LabSceneData{1,i}, 'gridAverage', gridSize);
% Debugging code to visualise frame-by-frame alignments
if (showPtCloudComparison)
figure; %#ok<UNRCH>
pcshowpair(pctransformNonRigid(ptCloudCurrent,tform),ptCloudScene,'VerticalAxis','Y','VerticalAxisDir','Down'...
,'MarkerSize',16)
title(strcat('Difference between scene and frame',num2str(i)))
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
end
% Store input transformation from last frame
initialTform = tform;
% Apply ICP registration. Currently assuming constant velocity model
[tform,~,rmse] = pcregrigidModified(moving, fixed, 'Metric','pointToPlane',...
'InlierRatio',0.7,'Extrapolate', true,'Verbose',false...
,'InitialTransform',tform);
% Calculate absolute running velocity and acceleration between frames
T1(:,1) = initialTform.T(4, 1:3)';
T2(:,1) = tform.T(4, 1:3)';
dT(i) = sqrt(sum((T1(:,1)-T2(:,1)).^2));
dV(i) = sqrt((dT(i)-dT(i-1))).^2;
% Velocity filter. Re-estimate tform without initial
if (abs(dV(i)))>0.05
[tform,~,rmse] = pcregrigidModified(moving, fixed, 'Metric','pointToPlane',...
'InlierRatio',0.7,'Extrapolate', true,'Verbose',false);
end
% RMSE Filter
if (rmse > 0.05)
droppedFrameCount = droppedFrameCount+1;
[tform,~,rmse] = pcregrigidModified(moving, fixed, 'Metric','pointToPlane',...
'InlierRatio',0.7,'Extrapolate', true,'Verbose',false);
end
% Store Euclidean dist of current transform
L2dist(i)=rmse;
% Transform the current point cloud to the reference coordinate system
% defined by the first point cloud.
accumTform = affine3d(tform.T * accumTform.T);
% Non-Rigid Transform Filter
% if ~isRigidTransform(accumTform)
% droppedFrameCount = droppedFrameCount+1;
% continue
% end
% Perform forward transformation
ptCloudAligned = pctransformNonRigid(ptCloudCurrent, accumTform);
% Transform camera position to current reference scene.
camPlot = pctransformNonRigid(camCloud,accumTform);
camTrack(i,1) = camPlot.Location(1,1);
camTrack(i,2) = camPlot.Location(1,2);
camTrack(i,3) = camPlot.Location(1,3);
% Extract quaternion rotation data
% Done use quaternion classdef by:
% Mark Tincknell, MIT LL, 29 July 2011, revised 25 June 2015
q = quaternion.rotationmatrix(tform.T(1:3,1:3));
camTrack(i,4) = q.e(4,1);
camTrack(i,5) = q.e(3,1);
camTrack(i,6) = q.e(2,1);
camTrack(i,7) = q.e(1,1);
% Update the world scene.
ptCloudScene = pcmerge(ptCloudScene, ptCloudAligned, mergeSize);
% Update Camera Pose
ptCloudScene = pcmerge(ptCloudScene, camPlot, mergeSize);
% Visualize the world scene.
hScatter.XData = ptCloudScene.Location(:,1);
hScatter.YData = ptCloudScene.Location(:,2);
hScatter.ZData = ptCloudScene.Location(:,3);
hScatter.CData = ptCloudScene.Color;
drawnow('update')
end
% Tracking and Kalman Filter Block
% Initialise for potentially 6 tracked targets
detectedLocation = zeros(6,3);
for i = find(isTracked, 1):finalFrame
for j =1:6
if trackingCoords(i,:,j);
detectedLocation(j,:) = trackingCoords(i,:,j);
% Configure filter using detected location
kalmanFilter = configureKalmanFilter('ConstantAcceleration',...
detectedLocation(j,:), [1 1 1]*1e5, [25, 10, 10], 25);
% Initialise prediction
predict(kalmanFilter);
% Correct covariance
projectedLocation(i,:,j) = correct(kalmanFilter, detectedLocation(1,:));
% Now predict 10 steps ahead
for k=i+1:1+10
projectedLocation(k,:,j) = predict(kalmanFilter);
end
% Clear old location value
detectedLocation = zeros(6,3);
end
end
end
% Visualisation Block
% %transform the data parallel to viewing axes, specify angle
% angle = -6*pi/180;
% A = [1,0,0,0;...
% 0, cos(angle),-sin(angle), 0; ...
% 0, sin(angle), cos(angle), 0; ...
% 0 0 0 1];
% ptCloudScene = pctransform(ptCloudScene, affine3d(A));
pcshow(ptCloudScene, 'VerticalAxis','Y', 'VerticalAxisDir', 'Down', ...
'Parent', hAxes)
title('Updated world scene')
xlabel('X (m)')
ylabel('Y (m)')
zlabel('Z (m)')
% Visualise trajectory alone.
camCloud = pointCloud(camTrack(:,1:3));
% View Result
figure
pcshow(camCloud, 'VerticalAxis','Y', 'VerticalAxisDir', 'Down', ...
'MarkerSize',50)
title('Updated world scene')
xlabel('X (m)')
ylabel('Y (m)')
zlabel('Z (m)')
% Plot running RMSE and display number of dropped frames
L2dist = L2dist(L2dist~=0);
distFig = figure;
plot(L2dist); title(strcat('RMS Error Euclidean Distance between ICP frames. Avg('...
,num2str(mean(L2dist)),')'));
xlabel('Frame Number'); ylabel('RMSE Distance.');
disp(strcat('Done. ' ,num2str(droppedFrameCount),' frames dropped!'));
beep
% % Uncomment to evaluate testbench performance data
% % Save estimated trajectory file
% trajEstimate = horzcat(timestamps, camTrack(1:finalFrame,:));
% trajEstimate = trajEstimate';
% fileID = fopen('estimated_trajectory_ConstV_360_hemi.txt','w');
% fprintf(fileID,'%5.5f %5.5f %5.5f %5.5f %5.5f %5.5f %5.5f %5.5f\n',trajEstimate);
% fclose(fileID);