首页 文章

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

提问于
浏览
13

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

我想要做的是对此图像执行透视校正,即我希望它删除任何透视图并看到校准装置未失真(它是棋盘格) .

Matlab的新计算机视觉工具箱有一个对象,在给定3X3矩阵H的情况下对图像执行透视变换 . 问题是,我不能从已知的内在和外在参数计算这个矩阵!

2 回答

  • 4

    对于那些在这么多个月后仍对此感兴趣的人,我的代码(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
    
  • 1

    方法1:在摄像机校准工具箱中,您应该注意到工作区中棋盘的每个图像都有一个H矩阵 . 我不熟悉计算机视觉工具箱,但也许这是你的功能需要的矩阵 . 似乎H的计算如下:

    KK = [fc(1) fc(1)*alpha_c cc(1);0 fc(2) cc(2); 0 0 1];
    H = KK * [R(:,1) R(:,2) Tc]; % where R is your extrinsic rotation matrix and Tc the translation matrix
    H = H / H(3,3);
    

    方法2:如果计算机视觉工具箱功能不适合您,那么为了找到图像的预期投影我已经使用了interp2函数,如下所示:

    [X, Y] = meshgrid(0:size(I,2)-1, 0:size(I,1)-1);
    im_coord = [X(:), Y(:), ones(prod(size(I_1)))]';
    % Insert projection here for X and Y to XI and YI
    ZI = interp2(X,Y,Z,XI,YI);
    

    我不久前在项目中使用了预期投影,我相信你需要使用齐次坐标 . 我想我发现this维基百科的文章非常有帮助 .

相关问题