双目视觉-Matlab实现

本次实验使用两个130万像素的海康威视工业相机,计划使用Matlab生成三维点云,但是由于本实验的视差值太大(512),而Matlab最大值只能取128,所以到视差图后面的效果就比较不理想了,后面就又转到python-opencv那边去了,不过前面几步效果不错,特此记录下。

实验前需要准备好的文件:标定好的摄像机内外参数,左右视图个一张。

  

显示函数代码imshowLine.m:

function imshowLine(I, a, b)
%UNTITLED 此处显示有关此函数的摘要
%   此处显示详细说明
    [H, ~] = size(I);
    bb = floor(b/2);
    out = I;
    for i = 1:H
        if(mod(i, a)==0)
            out(i-bb:i+bb,:)=255;
        end
    end
    imshow(out)
end

主函数stereoVision.m:

% 读取彩色图像
img_L = imread("L06.bmp");
img_R = imread("R06.bmp");
load('stereoParams.mat');
% 矫正左右图像,利用导入的立体相机参数
[L_Rect, R_Rect] = rectifyStereoImages(img_L, img_R, stereoParams2);

%存储矫正图像
% imwrite(L_Rect, 'L_Rect.bmp');
% imwrite(R_Rect, 'R_Rect.bmp');
imwrite(L_Rect, 'L_Rect.png');
imwrite(R_Rect, 'R_Rect.png');

%显示并画线检测是否对齐
figure;imshowLine(uint8([L_Rect R_Rect]),50, 2);        
figure;imshow(L_Rect);

% %利用矫正后的左右图生成立体图像
Anaglyph = stereoAnaglyph(L_Rect, R_Rect);
%figure;imshow(Anaglyph);

% 使用imtool确定视差范围值(行坐标之差max=990-490=500)这里取512,但是matlab只能取128怎么办???
%figure;imtool(Anaglyph);
disparityRange=[0, 128];

%利用矫正后的灰度图生成视差图
% Minimum value of uniqueness = 20
disparityMap_t20 = disparitySGM(L_Rect, R_Rect, 'DisparityRange', disparityRange,'UniquenessThreshold',20);
%figure;imshow(disparityMap_t20, disparityRange);title('disp t20');colormap jet; colorbar;

% Minimum value of uniqueness = 15
disparityMap_t15 = disparitySGM(L_Rect, R_Rect, 'DisparityRange', disparityRange, 'UniquenessThreshold', 15);
figure; imshow(disparityMap_t15, disparityRange); title('disp t15'); colormap jet; colorbar;

% Minimum value of uniqueness = 10
disparityMap_t10 = disparitySGM(L_Rect, R_Rect, 'DisparityRange', disparityRange, 'UniquenessThreshold', 10);
%figure; imshow(disparityMap_t10, disparityRange); title('disp t10'); colormap jet; colorbar;

% Minimum value of uniqueness = 5
disparityMap_t5 = disparitySGM(L_Rect, L_Rect, 'DisparityRange', disparityRange, 'UniquenessThreshold', 5);
%figure; imshow(disparityMap_t5, disparityRange); title('disp t5'); colormap jet; colorbar;

% Minimum value of uniqueness = 0
disparityMap_t0 = disparitySGM(L_Rect, L_Rect, 'DisparityRange', disparityRange, 'UniquenessThreshold', 0);
%figure; imshow(disparityMap_t0, disparityRange); title('disp t0'); colormap jet; colorbar;

%利用视差图重建3d图
points3D = reconstructScene(disparityMap_t15, stereoParams2);

%将距离单位由 mm->m
points3D = points3D ./ 1000;

%存储3D图的点云数据
ptClound = pointCloud(points3D, 'color',img_L);

%使用pcplayer观察点云图
player3D = pcplayer([-1, 1], [-1, 1], [0, 2], 'VerticalAxis','Y','VerticalAxisDir','Up');
view(player3D,ptClound);

校正结果:

立体图像:

 

视差图:

深度图:

 

三维点云:

 

posted @ 2022-12-16 09:13  路人加  阅读(480)  评论(2编辑  收藏  举报