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);

法二:

close all;
if(~exist('stereoParams_8_15'))
    load('C:\Users\1\Desktop\双目视觉\stereoParams_8_15.mat');
end

img1=imread('left.png');
img2=imread('right.png');
img1 = imresize(img1,[489,734]);
img2 = imresize(img2,[489,734]);
figure,imshow(img1);

[J1,J2]=rectifyStereoImages(img1,img2,stereoParams_8_15,'OutputView','valid');

disparityRange = [0 256];
 

disparityMap = imread('shicha.png');
disparityMap = imresize(disparityMap,[489, 734],'nearest'); % [489 734] [479 720]
disparityMap = disparityMap(10:488,14:733);

disparityMap = single(disparityMap);

figure 
imshow(disparityMap,[0,100]),colormap('jet'),colorbar; % ,'InitialMagnification',50

% return world coordinate from disparity map
points3D = reconstructScene(disparityMap, stereoParams_8_15);
 
% Convert to meters and create a pointCloud object
points3D = points3D ./ 10;

ptCloud = pointCloud(points3D, 'Color', J1);
 

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



% object=disparityMap(470,336,:);
% objectcoord=points3D(470,336,:);
% object1=disparityMap(477,604,:);
% objectcoord1=points3D(477,604,:);
% object2=disparityMap(615,308,:);
% objectcoord2=points3D(615,308,:);
% object3=disparityMap(624,609,:);
% objectcoord3=points3D(624,609,:);
 
% Visualize the point cloud
view(player3D, ptCloud),rotate3d on;

你可能感兴趣的:(matlab利用双目图像视差进行三维重建)