如何根据已知的内在和外在参数在 Matlab 中进行透视校正? [英] How to do perspective correction in Matlab from known Intrinsic and Extrinsic parameters?

查看:11
本文介绍了如何根据已知的内在和外在参数在 Matlab 中进行透视校正?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Matlab for camera calibration using Jean- Yves Bouget's Camera Calibration Toolbox. I have all the camera parameters from the calibration procedure. When I use a new image not in the calibration set, I can get its transformation equation e.g. Xc=R*X+T, where X is the 3D point of the calibration rig (planar) in the world frame, and Xc its coordinates in the camera frame. In other words, I have everything (both extrinsic and intrinsic parameters).

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

What I want to do is to perform perspective correction on this image i.e. I want it to remove any perspective and see the calibration rig undistorted (its a checkerboard).

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

Matlab's new Computer Vision toolbox has an object that performs a perspective transformation on an image, given a 3X3 matrix H. The problem is, I can't compute this matrix from the known intrinsic and extrinsic parameters!

推荐答案

对于所有几个月后仍然对此感兴趣的人,我已经设法使用 Kovesi 的代码(http://www.csse.uwa.edu.au/~pk/research/matlabfns),尤其是单应性 2d.m 函数.但是,您将需要装备四个角的像素值.如果相机是稳定固定的,那么您将需要这样做一次.请参阅下面的示例代码:

To all who are still interested in this after so many months, i've managed to get the correct homography matrix using Kovesi's code (http://www.csse.uwa.edu.au/~pk/research/matlabfns), and especially the homography2d.m function. You will need however the pixel values of the four corners of the rig. If the camera is steady fixed, then you will need to do this once. See example code below:

%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(您可能会在上面的代码中看到).此外,我已经设法仅从参数计算单应矩阵,但结果略有偏差(可能是校准工具箱中的舍入误差).最好的方法是上面的.

That should do the trick. Note that Matlab defines the x axis in an image ans the rows index and y as the columns. Thus one must swap x-y in the equations (as you'll probably see in the code above). Furthermore, i had managed to compute the homography matrix from the parameters solely, but the result was slightly off (maybe roundoff errors in the calibration toolbox). The best way to do this is the above.

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

If you want to use just the camera parameters (that is, don't use Kovesi's code), then the Homography matrix is H=KK*Rmat*inv_KK. In this case the code is,

% 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 中进行透视校正?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆