您的位置:首页 > 编程语言 > MATLAB

matlab-双目标定及场景重建

2014-04-25 22:55 489 查看

http://www.mathworks.cn/cn/help/vision/ug/stereo-vision.html

介绍matlab相机标定的网站:http://www.vision.caltech.edu/bouguetj/calib_doc/,里面有matlab标定工具箱的下载链接。
标定板下载:http://www.vision.caltech.edu/bouguetj/calib_doc/htmls/own_calib.html

注意事项:

1、matlab2014a只能对单个相机标定
MATLAB相机标定工具箱 MATLAB二维相机标定的解决方案 calibration

http://www.ilovematlab.cn/thread-267670-1-1.html

(出处: MATLAB中文论坛 )

2、matlab工具箱生成的单个相机参数放在cameraParams下,
而双目的标定的输出是stereoParams。

1、双目立体相机标定:estimateCameraParameters(imagePoints,worldPoints)

目的:

标定每个相机的内参,以及第二个相机相对于第一个相机的平移和旋转。
标定的参数可以用来校正立体像对,使极线平行。

步骤:
从不同角度拍摄标定板,不要使用压缩存储的格式如PNG,可以用JPEG。
通常用不对称的棋盘作为标定板,放在你想测量物体的距离。

Specify Calibration Images
Try to Detect Checkerboards(可以用imtool或imrect来查看需检测的区域,把干扰区域像素设为0)

im(3:700, 1247:end, :) = 0;


Detect Checkerboards in the Modified Images这一步matlab2014提供了detectCheckboardPoints(images1,images2)函数,直接检测棋盘的角点。
Calibrate the Stereo Camera System:标定的结果输出在stereoParams结构体中,里面包含了所有相机标定的内外参数,如旋转矩阵R、平移向量T等。2014直接提供了标定函数estimateCameraParameters(imagePoints,worldPoints),很强大!

2、立体像对校正:rectifyStereoImages(I1,I2,stereoParams)

把2维图像中寻找对应点,转换为1维。

2014版直接提供了校正的函数:rectifyStereoImages(I1,I2,stereoParams)


% Read in the stereo pair of images.
I1 = imread('sceneReconstructionLeft.jpg');
I2 = imread('sceneReconstructionRight.jpg');

% Rectify the images.
[J1, J2] = rectifyStereoImages(I1, I2, stereoParams);

% Display the images before rectification.
figure; imshow(cat(3, I1(:,:,1), I2(:,:,2:3)), 'InitialMagnification', 50);
title('Before Rectification');

% Display the images after rectification.
figure; imshow(cat(3, J1(:,:,1), J2(:,:,2:3)), 'InitialMagnification', 50);
title('After Rectification');



3、计算视差:disparity函数

视差——对应点之间的距离。

这个disparity视差函数有讲究,包含SAD块匹配和半局匹配semi-global block matching,
%半全局匹配(默认)还考虑了相邻块之间的视差



disparityMap = disparity(rgb2gray(J1), rgb2gray(J2));
figure; imshow(disparityMap, [0, 64], 'InitialMagnification', 50);
colormap('jet');
colorbar;
title('Disparity Map');


4、重建三维点云:reconstructScene(disparityMap,stereoParams)

用第3步生成的视差图和第1步生成的相机标定参数重建三维点云。

pointCloud = reconstructScene(disparityMap, stereoParams);

% Convert from millimeters to meters.
pointCloud = pointCloud / 1000;

5、三维场景可视化

每一个颜色通道需要分别画三维点图,循环RGB三色通道。可以通过rgb2ind把彩色图像转换为灰度图。

% Reduce the number of colors in the image to 128.
[reducedColorImage, reducedColorMap] = rgb2ind(J1, 128);

% Plot the 3D points of each color.
hFig = figure; hold on;
set(hFig, 'Position', [1 1 840   630]);
hAxes = gca;

X = pointCloud(:, :, 1);
Y = pointCloud(:, :, 2);
Z = pointCloud(:, :, 3);

for i = 1:size(reducedColorMap, 1)
% Find the pixels of the current color.
x = X(reducedColorImage == i-1);
y = Y(reducedColorImage == i-1);
z = Z(reducedColorImage == i-1);

if isempty(x)
continue;
end

% Eliminate invalid values.
idx = isfinite(x);
x = x(idx);
y = y(idx);
z = z(idx);

% Plot points between 3 and 7 meters away from the camera.
maxZ = 7;
minZ = 3;
x = x(z > minZ & z < maxZ);
y = y(z > minZ & z < maxZ);
z = z(z > minZ & z < maxZ);

plot3(hAxes, x, y, z, '.', 'MarkerEdgeColor', reducedColorMap(i, :));
hold on;
end

% Set up the view.
grid on;
cameratoolbar show;
axis vis3d
axis equal;
set(hAxes,'YDir','reverse', 'ZDir', 'reverse');
camorbit(-20, 25, 'camera', [0 1 0]);


-----------------------------------------------------------------------------------------------------------------------------------------------
20140609
1、打开E:\matlab\3D reconstruction\TOOLBOX_calib里的calib_gui.m,运行,
打开E:\matlab\3D reconstruction\TOOLBOX_calib\calib_example里的文件,
校正左图,将校正后文件存为Calib_Results_left.mat;再校正右图,将校正后文件存为Calib_Results_right.mat。
打开E:\matlab\3D reconstruction\TOOLBOX_calib里的stereo_gui.m进行双目标定。
2、E:\matlab\1programEllipse3D\My3FindEllipses20140502.m提取左图各椭圆中心
My3FindEllipses20140502_rightPic.m提取右图各椭圆中心

3、E:\matlab\1programEllipse3D\MyEllipses3Dzuobiao.m恢复三维坐标
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: