基于Matlab的双目视觉三维重建技术

本文介绍了利用MATLAB实现双目视觉立体成像,通过特征点匹配、校正和深度计算,成功重建三维模型的过程。从图像预处理、特征检测、匹配到最终的稠密点云可视化,详细展示了关键步骤和代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先需要用到双目视觉平行系统原理
在这里插入图片描述
之后了解到三维重建原理
在这里插入图片描述
在这里插入图片描述
由两张图象的二维图像哥哥像素点的坐标,推导出咱们三维试图重德三维坐标系统中对应的xyz的坐标数值,并显示在Matlab三维图中。
那么像素点怎么找的呢,具体能找到多少个像素点呢,,鉴于现在自己本科那些薄弱的学识,用到的方法就是基元匹配,
在这里插入图片描述
使用MATLAB软件进行程序的编写与仿真,对左右摄像头采集到的图像进行特征点的匹配,构建图像的三维模型
首先拍摄了一组人物图像,下面是原始图像
在这里插入图片描述
得到校正后的图像和上边的差不多,就不展示了
对校正后的图像进行特征点的匹配,发现噪声过大,标注了150个明显特征点
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
去除掉多余背景特征点之后,得到较为清晰的三维图像
在这里插入图片描述
可以看出,这个人物所占的三维空间是很清楚的被展示出来的。

Matlab代码

%%  
% 双目立体视觉  
% 对比实验  
  
%%  
% 清空工作区  
clc;  
clear;  
close all;  
  
%%  
% 导入图像数据  
I1 = imread('viewleft.png');  
I2 = imread('viewright.png');  
figure  
imshowpair(I1, I2, 'montage');   
title('Original Images');  
% 导入相机参数  
load cameraParams.mat  
  
%%  
% 校正  
I1 = undistortImage(I1, cameraParams);  
I2 = undistortImage(I2, cameraParams);  
figure   
imshowpair(I1, I2, 'montage');  
title('Undistorted Images');  
  
%%  
% 特征点提取  
imagePoints1 = detectMinEigenFeatures(rgb2gray(I1), 'MinQuality', 0.1);  
  
%%  
% 可视化  
figure  
imshow(I1, 'InitialMagnification', 50);  
title('150 Strongest Corners from the First Image');  
hold on  
plot(selectStrongest(imagePoints1, 150));  
  
%%  
% Create the point tracker  
tracker = vision.PointTracker('MaxBidirectionalError', 1, 'NumPyramidLevels', 5);  
imagePoints1 = imagePoints1.Location;  
initialize(tracker, imagePoints1, I1);  
% Track the points  
[imagePoints2, validIdx] = step(tracker, I2);  
matchedPoints1 = imagePoints1(validIdx, :);  
matchedPoints2 = imagePoints2(validIdx, :);  
  
%%  
% 特征点匹配  
figure  
showMatchedFeatures(I1, I2, matchedPoints1, matchedPoints2);  
title('Tracked Features');  
  
%%  
% F矩阵估计  
[fMatrix, epipolarInliers] = estimateFundamentalMatrix(...  
  matchedPoints1, matchedPoints2, 'Method', 'MSAC', 'NumTrials', 10000);  
% 极线  
inlierPoints1 = matchedPoints1(epipolarInliers, :);  
inlierPoints2 = matchedPoints2(epipolarInliers, :);  
% 显示内点  
figure  
showMatchedFeatures(I1, I2, inlierPoints1, inlierPoints2);  
title('Epipolar Inliers');  
  
%%  
% R和T(也可以用RANSAC算法)  
R = [0.9455,-0.0096,0.3253;  
    0.0120,0.9999,-0.0053;  
    -0.3252,0.0090,0.9456];  
t = [98.4069,0.1741,18.9018];  
  
%%  
% 稠密的特征点  
imagePoints1 = detectMinEigenFeatures(rgb2gray(I1), 'MinQuality', 0.001);  
  
%%  
% Create the point tracker  创建一个跟踪点
tracker = vision.PointTracker('MaxBidirectionalError', 1, 'NumPyramidLevels', 5);  
% Initialize the point tracker  
imagePoints1 = imagePoints1.Location;  
initialize(tracker, imagePoints1, I1);  
% Track the points  
[imagePoints2, validIdx] = step(tracker, I2);  
matchedPoints1 = imagePoints1(validIdx, :);  
matchedPoints2 = imagePoints2(validIdx, :);  
  
%%  
% cameraMatrix  
camMatrix1 = cameraMatrix(cameraParams, eye(3), [0 0 0]);  
camMatrix2 = cameraMatrix(cameraParams, R', -t*R');  
  
% 三维点云的计算  
points3D = triangulate(matchedPoints1, matchedPoints2, camMatrix1, camMatrix2);  
  
% 获取颜色信息  
numPixels = size(I1, 1) * size(I1, 2);  
allColors = reshape(I1, [numPixels, 3]);  
colorIdx = sub2ind([size(I1, 1), size(I1, 2)], round(matchedPoints1(:,2)), ...  
    round(matchedPoints1(:, 1)));  
color = allColors(colorIdx, :);  
  
% 创建点云  
ptCloud = pointCloud(points3D, 'Color', color);  
  
%%  
% 可视化  
cameraSize = 0.3;  
figure  
plotCamera('Size', cameraSize, 'Color', 'r', 'Label', '1', 'Opacity', 0);  
hold on  
grid on  
plotCamera('Location', t, 'Orientation', R, 'Size', cameraSize, ...  
    'Color', 'b', 'Label', '2', 'Opacity', 0);  
  
% 点云的可视化  
pcshow(ptCloud, 'VerticalAxis', 'y', 'VerticalAxisDir', 'down', ...  
    'MarkerSize', 45);  
  
% Rotate and zoom the plot  
camorbit(0, -30);  
camzoom(1.5);  
  
% Label the axes  
xlabel('x-axis');  
ylabel('y-axis');  
zlabel('z-axis')  
title('Up to Scale Reconstruction of the Scene');

哈哈,顺便说一句,需要在文件中建立加上两张左右相机拍摄出的两张视差照片,并且矫正好,输入你们的相机的R T的参数,这样一个标准的三维重建图片就做好了
我这次实验用的是
在这里插入图片描述

### 使用Matlab实现双目标定 #### 双目立体校准简介 在计算机视觉应用中,双目标定用于计算两个摄像机之间的相对位置和方向。这有助于重建三维场景并提供精确的距离测量。Matlab提供了`stereoCalibrate`函数来简化这一过程。 #### 数据准备 为了执行双目标定,需要收集一系列棋盘格图案的照片,这些照片应覆盖不同的视角和距离。每一对图像应该由两台摄像机同步拍摄得到。确保有足够的重叠区域以便后续匹配角点[^2]。 #### 执行双目标定 下面是一个完整的流程以及相应的代码片段: ```matlab % 加载左、右相机采集到的图片文件名列表 imageFolderLeft = 'path_to_left_camera_images'; imageFolderRight = 'path_to_right_camera_images'; leftImages = imageDatastore(imageFolderLeft); rightImages = imageDatastore(imageFolderRight); % 设置棋盘尺寸(内部交差点数量) boardSize = [7 9]; % 获取所有图像中的角点坐标 [imagePointsLeft, boardSize, imagesUsed] = detectCheckerboardPoints(leftImages.Files); [imagePointsRight, ~, ~] = detectCheckerboardPoints(rightImages.Files); % 定义世界坐标系下的理想角点位置 (单位:mm 或者像素) squareSize = 25; % 假设方格边长为25mm worldPoints = generateCheckerboardCorners(boardSize, squareSize); % 进行单目相机标定 camParamsLeft = estimateCameraParameters(imagePointsLeft , worldPoints); camParamsRight = estimateCameraParameters(imagePointsRight, worldPoints); % 合并参数进行双目校正 stereoParams = stereoCalibration(camParamsLeft, camParamsRight,... imagePointsLeft(:,:,imagesUsed),... imagePointsRight(:,:,imagesUsed)); save('StereoParams.mat', 'stereoParams'); ``` 这段脚本首先加载来自左右两侧摄像机的数据集,并检测其中包含的标准棋盘模式特征点;接着利用`estimateCameraParameters()`分别估计各自内参矩阵;最后调用`stereoCalibration()`完成整个系统的联合优化求解外参关系[^1]。 #### 结果验证与展示 一旦完成了上述步骤,则可以使用以下命令查看结果: ```matlab load StereoParams.mat; showExtrinsics(stereoParams); reconstruct3DImagePair(imread('imgL.jpg'), imread('imgR.jpg'), stereoParams); ``` 此部分展示了内外参数间的关系图,并尝试基于已知模型重构给定的一对测试图像对应的深度信息。
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值