-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.m
511 lines (454 loc) · 20 KB
/
script.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
%% `matlab-trimesh-stereo-reconstruction`
%> ========================================================================
%>
%> @file matlab-trimesh-stereo-reconstruction/script.m
%> @brief Tesellating stereo reconstruction script via point cloud processing.
%> @requires $matlabroot/toolbox/vision
%> @requires surf2stl
%>
%> ========================================================================
%>
%> MATLAB Computer Vision Toolbox script for stereo reconstruction as a
%> tesselating 3D triangular mesh surface in STL format.
%>
%> This script is unique, because it implements userland logic for:
%> - normalising a binocular disparity data map by combining multiple
%> match methods
%> - model-filtering the scene limits of the generated point cloud
%> - post-processing the scene point cloud with an interpolating signal
%> filter
%> - normalising the Cartesian (X-Y-Z) output axes in the resulting STL
%>
%> **File tree:**
%> - `./script.m`: a command window script
%> - `./app.m`: a GUI script
%> - `./live.mlx`: a live script
%> - Image folders - `./data/*`:
%> - `/input`: actual 3D modelling target scene
%> - `/config/left`: left stereo view with checkerboard
%> - `/config/right`: right stereo view with checkerboard
%> - Documentation - `./assets`
%> - Output - `./point-cloud.stl` (checked out)
%>
%> ========================================================================
%% Configuration
%> ========================================================================
%>
%% @subsection Setup
%>
%> **Input:** collect grayscale stereo photos in landscape using the
%> `*.jpg` / `*.jpeg` format (can use any extension from
%> [`imformats`](/help/matlab/ref/imformats.html).
%> - Add checkerboard images from left stereo view to `./config/left`
%> - Add checkerboard images from right stereo view to `./config/right`
%> - Add modelling input images from stereo view to `./input`
%>
%> imformats() % supported images formats in MATLAB
%>
%> **Output:** A 3D representation of the stereo image set in STL format.
%> The default location is `./point-cloud.stl`.
%>
%> **N.B:** For good performance:
%> - resize your images below 720p (maybe between 360p and 480p)
%> - use [GIMP](https://www.gimp.org/) and [BIMP](https://alessandrofrancesconi.it/projects/bimp/) to convert the image color space to grayscale
%>
%% @subsection Image support
%>
%> - Must be the same orientation as the checkerboard to reduce pixel
%> error from reprojection.
%> - For calibration images:
%> - asymmetric (odd-even) checkerboard should be in all views
%> - minimum image count per folder is 4 (for low reprojection error)
%> - naming convention: `./config/<VIEW>/<VIEW>##.jpg`
%> (e.g. `./config/left/left01.jpg`)
%>
%> ```matlab
%> imformats() % supported images formats in MATLAB
%> ```
%>
%> **N.B:** the image file names must be numbered in ascending order.
%> ========================================================================
%% Source code
%> ========================================================================
%> ========================================================================
%% @section Setup
%> ========================================================================
%% @subsection Environment cleanup
close all;
clear;
clc;
%> ========================================================================
%% @subsection Workspace cleanup
filePath = fullfile(pwd, 'data', 'toolbox');
stlPath = 'point-cloud.stl';
if exist(fullfile(filePath, stlPath), 'file')
recycle on;
delete(fullfile(filePath, stlPath));
end
%> ========================================================================
%% @subsection Input loading
inputImages = imageDatastore(fullfile(filePath, 'input'));
I1 = readimage(inputImages, 1);
if size(I1, 3) == 3
I1 = rgb2gray(I1);
end
I2 = readimage(inputImages, 2);
if size(I2, 3) == 3
I2 = rgb2gray(I2);
end
%> ========================================================================
%% @subsection Dependency management
if ~exist('surf2stl', 'file')
if ~matlab.addons.isAddonEnabled('mpm')
error([ ...
'Please install MPM as a MATLAB Addon.\n' ...
'<a href="' ...
'/matlabcentral/fileexchange/54548' ...
'">' ...
'mpm - File Exchange - MATLAB Central' ...
'</a>' ...
])
else
mpm install surf2stl;
end
end
%> ========================================================================
%% @subsection User configuration
%> @var filePath path to current folder.
%> @var stlPath name of point cloud STL.
%> @var imageMinimum min. no of images in calib folder.
%> @var squareWidth Checkerboard square width in mm.
%> @var ptCloudDensity Point density within squareWidth.
%> @var sGolayFiltOrder Savitsky-Golay extrapolation curve order.
%> @var sGolayFiltFrameLen Savitsky-Golay sliding window point count.
%> @var disparityBMBias Algorithm bias to block matching vs semi
%> global matching (in the range -1 to 1).
%> @var disparityMaxRatio Ratio for disparity map ceiling vs maximum
%> disparity (in the range 0 to 1).
squareWidth = 50;
ptCloudDensity = 5;
sGolayFiltOrder = 3;
sGolayFiltFrameLen = 21;
disparityBMBias = -0.5;
disparityMaxRatio = 0.675;
%> ========================================================================
%% @subsection Show loading indicator
%> Script takes $T = ~30s$ to execute.
wb = waitbar(0, 'Loading');
wb.Visible = 'on';
%> ========================================================================
%% @section Camera calibration of stereo images
%> Code based on MATLAB `rectifyStereoImages` code sample [1].
%> ========================================================================
%% @subsection Image loading
%> Using a grayscale color space reduces image data & overhead in
%> calibration phase. [2]
%>
%> @var inputImages Images from `./input` subfolder.
%> @var calibLeftImages Images from `./config/left` subfolder.
%> @var calibRightImages Images from `./config/right` subfolder.
%> @see #image-support
waitbar(0, wb, 'Loading input images.');
calibLeftImages = imageDatastore(fullfile(filePath, 'config', 'left'));
calibRightImages = imageDatastore(fullfile(filePath, 'config', 'right'));
%> ========================================================================
%% @subsection Image validation
%> @exception char mismatch in image count between `./config/left` & `./config/right`.
%> @exception char below 4 images in `./config/left` & `./config/right`.
%> @exception char mismatch in resolution of `./input` images.
S1 = [size(I1, 1), size(I1, 2)];
S2 = [size(I2, 1), size(I2, 2)];
imageAmounts = struct;
imageAmounts.L = size(calibLeftImages.Files, 1);
imageAmounts.R = size(calibRightImages.Files, 1);
errno{1} = 'stereo2trimesh::ERR_MISMATCH_IMG_COUNT';
errno{2} = 'stereo2trimesh::ERR_CALIB_IMG_INSUFFICIENT';
errno{3} = 'stereo2trimesh::ERR_MISMATCH_IMG_DIM';
if imageAmounts.L ~= imageAmounts.R
e = [errno{1} ' (L: ' imageAmounts.L ', R: ' imageAmounts.R];
errordlg(e);
error(e);
elseif imageAmounts.L < 4
e = [errno{2} ' (n=' imageAmounts.L ')'];
errordlg(e);
error(e);
elseif ~isequal(S1, S2)
e = [errno{3} ' (L: ' S1(1) 'x' S1(2) 'px, R: ' S2(1) 'x' S2(2) 'px)'];
errordlg(e);
error(e);
end
%> ========================================================================
%% @subsection Checkerboard detection
waitbar(0.1, wb, 'Detecting checkerboard keypoints.');
[imagePoints, boardSize] = detectCheckerboardPoints( ...
calibLeftImages.Files, ...
calibRightImages.Files ...
);
%> ========================================================================
%% @subsection Calculate checkerboard keypoints
worldPoints = generateCheckerboardPoints(boardSize, squareWidth);
%> ========================================================================
%% @subsection Calibrate stereo camera system
%> @param EstimateSkew Are image axes exactly perpendicular?
%> Default: `true`.
%> @param EstimateTangentialDistortion Factor in whether the camera is
%> horizontal. Default: `true`.
%> @param NumRadialDistortionCoefficients Good for fish-eye lenses.
%> Default: `2`.
%> @param ImageSize Matrix for size of image - `imageSize`.
%> @todo Adjust `estimateCameraParameters` parameters for experimental
%> stage.
waitbar(0.2, wb, "Estimating camera parameters.");
[stereoParams, ~, estimationErrors] = estimateCameraParameters( ...
imagePoints, worldPoints, ...
'EstimateSkew', true, ...
'EstimateTangentialDistortion', false ...
);
%> ========================================================================
%% @subsection Display camera extrinisics
%> Reprojection is the process of "reprojecting" original image from a
%> camera image. Most camera images have distortion (e.g. "fisheye" lens
%> effect).
%>
%> @figure Checkerboard boundary points for calibration experiment.
waitbar(0.3, wb, "Showing camera extrinisics.");
figure;
showExtrinsics(stereoParams, "CameraCentric");
view([-45 45]);
%> ========================================================================
%% @subsection Graph camera reprojection errors
%> @figure Reprojection errors for calibration experiment.
waitbar(0.4, wb, "Showing reprojection errors.");
figure;
showReprojectionErrors(stereoParams);
displayErrors(estimationErrors, stereoParams);
%> ========================================================================
%% @subsection Stereo rectification with "valid" output view
%> The "valid" option is most suitable for computing disparity.
%> These images have negative polar distortion and appear concave
%> (the top has a U-curve). It limits the rectified image data from
%> to a regular 2D rectangle. [3]
%>
%> @param OutputView Crop the image to a rectangle, fitting inside the
%> overlapping, curved 3D anaglyph. Default: `valid`.
waitbar(0.5, wb, "Showing stereo rectification.");
[F1, F2] = rectifyStereoImages(I1, I2, stereoParams, 'OutputView', 'valid');
pixelDensityMm = mrdivide( ...
mean([ ...
stereoParams.CameraParameters1.FocalLength, ...
stereoParams.CameraParameters2.FocalLength ...
], 2), ...
mean([ ...
stereoParams.CameraParameters1.IntrinsicMatrix(1, 1), ...
stereoParams.CameraParameters2.IntrinsicMatrix(1, 1) ...
], 2) ...
);
approxImageHeight = 2 * mean([size(F1, 1), size(F2, 1)], 2) / pixelDensityMm;
approxImageWidth = 2 * sqrt(2) * mean([size(F1, 2), size(F2, 2)], 2) / pixelDensityMm;
%> ========================================================================
%% @subsection Display an "valid" output anaglyph image
%> Display an anaglyph image for "valid" output view.
%> @figure Stereo anaglyph image of input scene.
waitbar(0.6, wb, "Showing stereo anaglyph.");
figure;
hold on;
labels{1} = plot(nan, nan, 'color', 'red');
labels{2} = plot(nan, nan, 'color', 'black');
labels{3} = plot(nan, nan, 'color', 'cyan');
legend([labels{:}], {'left', '', 'right'});
imshow(stereoAnaglyph(F1, F2));
axis tight;
title 'Rectified Image';
clearvars labels;
%> ========================================================================
%% @section Disparity computation from stereo images.
%> Code based on MATLAB `disparitySGM` code sample. [4]
%> ========================================================================
%% @subsection Compute disparity map from stereo images
%> Generate a disparity (Cartesian *z*-depth) colormap of the scene. We
%> take a biased average of the disparity map produced by semi-global and
%> block matching algorithms. This ensures reduced "hole" (neutral)
%> amplitudes in the disparity data.
%>
%> @todo Adjust `disparityBMBias` as appropriate for the image input.
%> @todo Adjust the range maximum to $c\times2^4,c\in\mathbb{N}$ to
%> remove outliers or camera noise.
waitbar(0.7, wb, "Computing disparity map.");
disparityMapBM = disparityBM(F1, F2, "DisparityRange", [0, 64]);
disparityMapSGM = disparitySGM(F1, F2, "DisparityRange", [0, 64]);
disparityMapBM(isnan(disparityMapBM)) = 0;
disparityMapSGM(isnan(disparityMapSGM)) = 0;
disparityBMQuotient = (1 + disparityBMBias) / 2;
disparitySGMQuotient = (1 - disparityBMBias) / 2;
disparityMap = disparityBMQuotient * disparityMapBM + disparitySGMQuotient * disparityMapSGM;
disparityMap(disparityMap==0) = NaN;
%> ========================================================================
%% @subsection Remove "spike" transients
%> Limit disparity values to ~90% of the maximum to remove maximal AKA
%> "spike" transients.
disparityMapCeil = disparityMaxRatio * max(max(disparityMap));
disparityMap(disparityMap>=disparityMapCeil) = NaN;
%> ========================================================================
%% @section Display disparity map
%> @figure Disparity map as `parula` colormap image.
waitbar(0.8, wb, "Showing parula colormap.");
figure;
imshow(disparityMap, [0, 64]);
title 'Disparity Map';
axis tight;
colormap parula;
colorbar southoutside;
%> ========================================================================
%% @section Point cloud generation using depth data
%> ========================================================================
%% @subsection Reconstruct organised point cache matrix
%> Produces Cartesian scatter point cloud data in m - standard STL
%> dimensions.
waitbar(0.9, wb, "Generating point cloud.");
rawPoints3D = reconstructScene(disparityMap, stereoParams);
rawPoints3D(isinf(rawPoints3D)) = NaN;
rawPoints3D = double(rawPoints3D) ./ 1000;
%> ========================================================================
%% @subsection Initialise axial, co-ordinate point cloud cache
pointsCache = struct;
axesKeys = ["X", "Y", "Z"];
for m = 1:3
k = char(axesKeys(m));
p = rawPoints3D(:, :, m);
pointsCache.(k) = p;
end
clearvars p k;
%> ========================================================================
%% @subsection Compute checkerboard centroid as struct
%> Compute checkerboard position as a Cartesian coordinate in the point
%> cloud. It's the mean of the co-ordinate set closest to the origin in
%> the *z*-axis.
%
%> @todo See if I need to change `min` in some way (assumes convex).
checkerboardCentroid = struct;
checkerboardCentroid.Z = min(min(pointsCache.Z));
checkerboardIndex = sort(find(checkerboardCentroid.Z == pointsCache.Z));
checkerboardCentroid.X = 0;
checkerboardCentroid.Y = 0;
%> ========================================================================
%% @subsection Restrict point cloud to image scene dimensions
%> Limits:
%> - point cloud width *x* = scene image width
%> - point cloud height *y* = sqrt(0.5) × scene image height
%> - point cloud depth *z* = ?0.5 × scene image (height + width)
waitbar(0.9, wb, "Filter-processing point cloud co-ordinates.");
limits = struct;
cacheAxes = char(fieldnames(pointsCache));
for m = 1:3
switch m
case 1
bound = approxImageWidth;
case 2
bound = sqrt(0.5) * approxImageHeight;
otherwise
bound = mean([approxImageHeight, approxImageWidth], 2) / 2;
end
k = cacheAxes(m);
c = checkerboardCentroid.(k);
l = bound/1000;
lim = [c - l, c + l];
limits.(k) = lim;
p = pointsCache.(k);
p(p < lim(1) | p > lim(2)) = NaN;
pointsCache.(k) = p;
end
clearvars k lim p;
%> ========================================================================
%% @subsection Filter point cloud for invalid values
%> Remove invalid (NaN) values inside point cloud.
%> - Values that are `+Inf` / `-Inf` / `NaN`.
%> - Points that fall outside range of point cloud.
nanPoints = ( 0 ...
| isnan(pointsCache.X) ...
| isnan(pointsCache.Y) ...
| isnan(pointsCache.Z) ...
);
for m = 1:3
k = cacheAxes(m);
p = pointsCache.(k);
p(nanPoints) = checkerboardCentroid.(k);
pointsCache.(k) = p;
end
clearvars k p;
%> ========================================================================
%% @section Surface mesh conversion
%> ========================================================================
%% @subsection Surface mesh denoising and interpolation
% Generate a organised point cloud as a struct with 1 axis per field.
%> Code adapted from StackOverflow. [5]
%>
%> 1. The `scatteredInterpolant` factory creates interpolant. [6]
%> 2. MATLAB maps `meshgrid` regular matrix of x-y points.
%> 3. Savitzky-Golay filter used to denoise points in Z axis.
%>
%> @see https://commons.wikimedia.org/wiki/File:Lissage_sg3_anim.gif
waitbar(0.9, wb, "Interpolating point cloud as surface mesh.");
gs = (1 / ptCloudDensity) * (squareWidth / 1000);
I = scatteredInterpolant(pointsCache.X(:), pointsCache.Y(:), pointsCache.Z(:), "natural");
gridPoints = struct;
intX = min(pointsCache.X(:)):gs:max(pointsCache.X(:));
intY = min(pointsCache.Y(:)):gs:max(pointsCache.Y(:));
[gridPoints.X, gridPoints.Y] = meshgrid(intX, intY);
gridPoints.Z = I(gridPoints.X, gridPoints.Y);
intZ1 = sgolayfilt(gridPoints.Z.', sGolayFiltOrder, sGolayFiltFrameLen);
intZ2 = sgolayfilt(gridPoints.Z, sGolayFiltOrder, sGolayFiltFrameLen);
gridPoints.Z = (intZ1.' + intZ2)/2;
%> ========================================================================
%% @subsection Restore correct Cartesian axes in coordinate system
%> Apply geometric transforms to gridded point clouds:
%> - $-1$ scalar transformation of *y*-axis (vertical axis of image plane)
%> - $-1$ scalar transformation of *z*-axis (depth axis of image plane)
gridPoints = struct('X', gridPoints.X, 'Y', gridPoints.Z, 'Z', -1 .* gridPoints.Y);
%> ========================================================================
%% @subsection Create point cloud scatter matrix
points3D = double.empty();
for m = 1:3
points3D(:, :, m) = gridPoints.(cacheAxes(m));
end
clearvars cacheAxes;
%> ========================================================================
%% @subsection Mesh triangulation view of point cloud
%> @figure 3D connected surface mesh plot of the point cloud.
%> @note We reverse the geometric transforms.
waitbar(0.925, wb, "Show surface mesh plot.");
figure;
mesh(-1 .* gridPoints.X, gridPoints.Z, -1 .* gridPoints.Y);
title 'Mesh Triangulation';
xlabel 'x (horizontal displacement in m)';
ylabel 'y (vertical displacement in m)';
zlabel 'z (scene depth in m)';
set(gcf, "Color", "w");
set(gca, "XColor", "k");
set(gca, "YColor", "k");
set(gca, "ZColor", "k");
set(gca, "LineWidth", 1);
axis equal;
view([180 -90]);
colormap gray;
colorbar southoutside;
rotate3d on;
%> ========================================================================
%% @section STL file generation from point cloud
%> Using `surf2stl` for high stability & speed (low interpolation).
waitbar(0.95, wb, "Writing STL output..");
stlPath = char(fullfile(pwd, stlPath));
surf2stl(stlPath, gridPoints.X, gridPoints.Y, gridPoints.Z);
%> ========================================================================
%% @section Teardown
waitbar(1, wb, "Done!");
winopen(stlPath);
close(wb);
%> ========================================================================
%% References
%> @section references
%> 1. /help/matlab/ref/rgb2gray.html
%> 2. /help/vision/examples/depth-estimation-from-stereo-video.html
%> 3. /help/vision/ref/rectifystereoimages.html
%> 4. /help/vision/ref/disparitysgm.html
%> 5. https://stackoverflow.com/a/39576639
%> 6. /help/matlab/ref/scatteredinterpolant.html