matlab - 如何根据已知的内在和外在参数在 Matlab 中进行透视校正?

标签 matlab computer-vision matlab-cvst extrinsic-parameters

我正在使用 Matlab 使用 Jean- Yves Bouget's Camera Calibration Toolbox 进行相机校准.我有所有的相机 来自校准程序的参数。当我不使用新图像时 在校准集中,我可以得到它的变换方程,例如 Xc=R*X+T,其中 X 是校准装置(平面)的 3D 点 世界坐标系,Xc 是它在相机坐标系中的坐标。其他 换句话说,我拥有一切(外部参数和内部参数)。

我想做的是对这张图片进行透视校正 即我希望它移除任何透视图并查看校准装置 未失真(它是一个棋盘)。

Matlab 的新计算机视觉工具箱有一个对象,可以对一个图像执行透视变换 图像,给定一个 3X3 矩阵 H。问题是,我无法计算这个 来自已知内在和外在参数的矩阵!

最佳答案

对于这么多个月后仍然对此感兴趣的所有人,我已经设法使用 Kovesi 的代码(http://www.csse.uwa.edu.au/~pk/research/matlabfns),尤其是 homography2d.m 函数获得了正确的单应矩阵。但是,您将需要钻机四个角的像素值。如果相机稳定固定,则需要执行一次。请参阅下面的示例代码:

%get corner pixel coords from base image
p1=[33;150;1];
p2=[316;136;1];
p3=[274;22;1];
p4=[63;34;1];
por=[p1 p2 p3 p4];
por=[0 1 0;1 0 0;0 0 1]*por;    %swap x-y <--------------------

%calculate target image coordinates in world frame
% rig is 9x7 (X,Y) with 27.5mm box edges
XXw=[[0;0;0] [0;27.5*9;0] [27.5*7;27.5*9;0] [27.5*7;0;0]];
Rtarget=[0 1 0;1 0 0;0 0 -1]; %Rotation matrix of target camera (vertical pose)
XXc=Rtarget*XXw+Tc_ext*ones(1,4); %go from world frame to camera frame
xn=XXc./[XXc(3,:);XXc(3,:);XXc(3,:)]; %calculate normalized coords
xpp=KK*xn;  %calculate target pixel coords

% get homography matrix from original to target image
HH=homography2d(por,xpp);
%do perspective transformation to validate homography
pnew=HH*por./[HH(3,:)*por;HH(3,:)*por;HH(3,:)*por]; 

这应该可以解决问题。请注意,Matlab 将图像中的 x 轴定义为行索引,将 y 定义为列。因此,必须交换方程式中的 x-y(您可能会在上面的代码中看到)。此外,我设法仅从参数计算单应矩阵,但结果略有偏差(可能是校准工具箱中的舍入误差)。执行此操作的最佳方法是上述方法。

如果只想使用相机参数(即不要使用 Kovesi 的代码),则 Homography 矩阵为 H=KK*Rmat*inv_KK。在这种情况下,代码是,

% corner coords in pixels
p1=[33;150;1];
p2=[316;136;1];
p3=[274;22;1];
p4=[63;34;1];
pmat=[p1 p2 p3 p4];
pmat=[0 1 0;1 0 0;0 0 1]*pmat; %swap x-y

R=[0 1 0;1 0 0;0 0 1];  %rotation matrix of final camera pose
Rmat=Rc_ext'*R;  %rotation from original pose to final pose
H=KK*Rmat*inv_KK; %homography matrix
pnew=H*pmat./[H(3,:)*pmat;H(3,:)*pmat;H(3,:)*pmat]; %do perspective transformation

H2=[0 1 0;-1 0 0;0 0 1]*H;  %swap x-y in the homography matrix to apply in image

关于matlab - 如何根据已知的内在和外在参数在 Matlab 中进行透视校正?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23572237/

相关文章:

matlab - 在 MATLAB 中使用 princomp 进行 PCA(用于人脸识别)

tensorflow - 训练模型以实现 DLib 的面部标志,例如手部特征点及其标志

matlab - 验证相机校准仍然有效

algorithm - 图像中数字的识别(Matlab)

matlab - 保存 Matlab 图而不绘制它,然后在 VISIBLE 状态下打开它

c - 如何使用 MinGW 为 MATLAB 2011 配置 mex

image - 突出显示具有不同颜色图像的区域及其周围区域

matlab - 在 matlab 中读取视频 - 音频输出端口不相关

MATLAB 清除当前图

algorithm - OpenCV矩形绘制函数