视觉SLAM方面的旋转矩阵和平移计算的Matlab例程较少,单独写了一个测试一下。测试环境为Matlab2025b。代码部分如下:
%% 1. 合成场景
clear; close all;
% ------------- 相机内参 --------------------------
fx = 800; fy = 800; cx = 320; cy = 240;
K = cameraIntrinsics([fx fy],[cx cy],[640 480]); % 640x480 图像
% ------------- 3‑D 点 ---------------------------------------------------------
N = 200;
Xw = [rand(N,1)*10-5, ... % X ∈[-5,5]
rand(N,1)*10-5, ... % Y ∈[-5,5]
rand(N,1)*10+5]; % Z ∈[5,15]
% ------------- 相机 1:原点 -----------------------------------------------
Tcw1 = rigidtform3d; % 旋转为单位矩阵,平移为零
R1 = Tcw1.Rotation;
t1 = Tcw1.Translation; % 1×3 行向量
% ------------- 相机 2:旋转 + 平移 -----------------------------------------
theta = deg2rad(10);
R2 = axang2rotm([0 0 1 theta]); % 绕 Z 轴 15°
% t2 = [0.8 0.3 1.6]; % 1×3 行向量
t2=rand(1,3)*3;
% ------------- 投影函数 ----------------------------------------------
proj = @(R,t,point3d) ProjectPoints_(point3d, R, t, fx, fy, cx, cy);
% ------------- 生成像素坐标(无噪声) -----------------------------------
pointPixe1 = proj(R1,t1,Xw);
pointPixe2 = proj(R2,t2,Xw);
% ------------- 加入噪声 ---------------------------------------------
sigmaPix = 1; % 1 px Gaussian noise
matchPoint1n = pointPixe1 + sigmaPix*randn(size(pointPixe1));
matchPoint2n = pointPixe2 + sigmaPix*randn(size(pointPixe2));
%% 2. 特征匹配(这里直接使用全部点对)
%% 3. 估计本质矩阵
[E,inliersIdx] = estimateEssentialMatrix(matchPoint1n, matchPoint2n, K);
pointPixe1_in = matchPoint1n(inliersIdx,:);
pointPixe2_in = matchPoint2n(inliersIdx,:);
%% 4. 相对姿态恢复
% [relativePose,~] = estrelpose(E,K,pts1_in,pts2_in);
% using SVD to get the rotation R and translation t
[U,Sig,V]=svd(E);
% Ensure that the subsequently obtained R still belongs to SO(3)
if det(U)<0
U=-U;
end
if det(V)<0
V=-V;
end
W=[0 -1 0;1 0 0;0 0 1];
Ra=U*W*V';
Rb=U*W'*V';
Ta=U(:,3);
Tb=-Ta;
cameraProjection1=K.K*[eye(3) zeros(3,1)];
% get the four projection matrix on position 2
cameraProj2=zeros(3,4,4);
cameraProj2(:,:,1)=[Ra Ta];
cameraProj2(:,:,2)=[Ra Tb];
cameraProj2(:,:,3)=[Rb Ta];
cameraProj2(:,:,4)=[Rb Tb];
point3D_tri4=zeros(3,4);
for s=1:4
point3D_tri4(:,s) = triangulate(pointPixe1_in(1,:), pointPixe2_in(1,:), cameraProjection1, K.K*cameraProj2(:,:,s));
if point3D_tri4(3,s)>0
ss=cameraProj2(:,:,s)*[point3D_tri4(:,s);1];
if ss(3)>0
break;
end
end
end
cameraProjection2=K.K*cameraProj2(:,:,s);
%% 5. 三角化 --------------------------------------------------------
point3D_tri = triangulate(pointPixe1_in, pointPixe2_in, cameraProjection1, cameraProjection2);
% 计算误差
XwInliers=Xw(inliersIdx,:);
scale=point3D_tri(1,3)/XwInliers(1,3);
point3D_tri=point3D_tri/scale;
err_tri = sqrt(mean(sum((point3D_tri - XwInliers).^2,2)));
%% -------------------------------------------------------------------------
%% 6. 结果可视化 --------------------------------------------------------
%% -------------------------------------------------------------------------
figure('Name','Triangulation Demo','NumberTitle','off');
scatter3(Xw(:,1),Xw(:,2),Xw(:,3),'bx','LineWidth',1.5); hold on;
scatter3(point3D_tri(:,1),point3D_tri(:,2),point3D_tri(:,3),'ro','LineWidth',1.5);
legend('Ground Truth','Reconstructed','Location','best');
grid on; axis equal; xlabel('X'); ylabel('Y'); zlabel('Z');
title(sprintf('RMSE = %.4f m',err_tri));
%% -------------------------------------------------------------------------
%% 7. 自定义投影函数(仅在 Toolbox 不可用时使用) --------------------
%% -------------------------------------------------------------------------
function uv = ProjectPoints_(worldPoints, R, t, fx, fy, cx, cy)
% 3‑D 点投影到像素坐标
% 输入:
% worldPoints : Nx3 矩阵
% R : 3×3 旋转矩阵
% t : 1×3 平移向量
% fx,fy : 焦距
% cx,cy : 主点坐标
%
% 输出:
% uv : Nx2 像素坐标
% 相机坐标
% Xc = (worldPoints*R + t); % Nx3
Xc = (R*worldPoints' + t')'; % Nx3
% 投影(透视投影)
u = (fx * Xc(:,1) + cx * Xc(:,3)) ./ Xc(:,3);
v = (fy * Xc(:,2) + cy * Xc(:,3)) ./ Xc(:,3);
uv = [u, v];
end
部分代码通过模型生成。
当加入的噪声为0时,效果图如下:

当加入噪声时,效果如下:

Mathworks的 File Exchange 也有更新。https://ww2.mathworks.cn/matlabcentral/fileexchange/185246-monocular-visual-slam-rotation-matrix-and-translation
318




被折叠的 条评论
为什么被折叠?



