1. 程式人生 > >8.結構光:雙目視覺(基於視差)

8.結構光:雙目視覺(基於視差)

Matlab基於視差進行三維重建的程式碼如下:

%%
% 清理空間
clc;
clear;
close all;

%%
% 匯入立體標定引數
load stereoParams.mat
% 立體引數的視覺化
figure;
showExtrinsics(stereoParams);

%% 
% 匯入資料
frameLeft = imread('pattern_cam1_im1.png');
frameRight = imread('pattern_cam2_im1.png');

[frameLeftRect, frameRightRect] = ...
    rectifyStereoImages(frameLeft, frameRight, stereoParams);

figure;
imshow(stereoAnaglyph(frameLeftRect, frameRightRect));
title('Rectified Frames');

%%
% 視差計算
frameLeftGray  = rgb2gray(frameLeftRect);
frameRightGray = rgb2gray(frameRightRect);

disparityMap = disparity(frameLeftGray, frameRightGray);
figure;
imshow(disparityMap, [0, 64]);
title('Disparity Map');
colormap jet
colorbar

%%
% 三維重建
points3D = reconstructScene(disparityMap, stereoParams);

% Convert to meters and create a pointCloud object
points3D = points3D ./ 1000;
ptCloud = pointCloud(points3D, 'Color', frameLeftRect);

% Create a streaming point cloud viewer
player3D = pcplayer([-3, 3], [-3, 3], [0, 8], 'VerticalAxis', 'y', ...
    'VerticalAxisDir', 'down');

% Visualize the point cloud
view(player3D, ptCloud);