18

我正在开发一个需要校正从移动相机平台拍摄的图像的应用程序。该平台测量滚动角、俯仰角和偏航角,我想让它看起来像从正上方拍摄的图像,通过对这些信息的某种变换。

换句话说,我想要一个平放在地面上的完美正方形,从远处以某种相机方向拍摄,然后进行变换,以便之后正方形完美对称。

我一直在尝试通过 OpenCV(C++) 和 Matlab 来做到这一点,但我似乎遗漏了一些关于如何做到这一点的基本知识。

在 Matlab 中,我尝试了以下方法:

%% Transform perspective
img = imread('my_favourite_image.jpg');
R = R_z(yaw_angle)*R_y(pitch_angle)*R_x(roll_angle);
tform = projective2d(R);   
outputImage = imwarp(img,tform);
figure(1), imshow(outputImage);

其中 R_z/y/x 是标准旋转矩阵(以度数实现)。

对于一些偏航旋转,一切正常:

R = R_z(10)*R_y(0)*R_x(0);

这给出了结果:

图像绕 Z 图像轴旋转 10 度

如果我尝试将图像围绕 X 轴或 Y 轴旋转相同的量,我会得到如下结果:

R = R_z(10)*R_y(0)*R_x(10);

图像绕 X 图像轴旋转 10 度

但是,如果我旋转 10 度,除以一个巨大的数字,它开始看起来不错。但话又说回来,这是一个没有任何研究价值的结果:

R = R_z(10)*R_y(0)*R_x(10/1000);

图像绕 X 图像轴旋转 10/1000 度

有人可以帮我理解为什么绕 X 轴或 Y 轴旋转会使转换变得疯狂吗?有没有办法解决这个问题而不用除以一些随机数和其他魔术?这是否可以使用某种欧拉参数来解决?任何帮助将不胜感激!

更新:完整设置和测量

为了完整起见,添加了完整的测试代码和初始图像,以及平台欧拉角:

代码:

%% Transform perspective
function [] = main()
    img = imread('some_image.jpg');
    R = R_z(0)*R_y(0)*R_x(10);
    tform = projective2d(R);   
    outputImage = imwarp(img,tform);
    figure(1), imshow(outputImage);
end

%% Matrix for Yaw-rotation about the Z-axis
function [R] = R_z(psi)
    R = [cosd(psi) -sind(psi) 0;
         sind(psi)  cosd(psi) 0;
         0          0         1];
end

%% Matrix for Pitch-rotation about the Y-axis
function [R] = R_y(theta)
    R = [cosd(theta)    0   sind(theta);
         0              1   0          ;
         -sind(theta)   0   cosd(theta)     ];
end

%% Matrix for Roll-rotation about the X-axis
function [R] = R_x(phi)
    R = [1  0           0;
         0  cosd(phi)   -sind(phi);
         0  sind(phi)   cosd(phi)];
end

初始图像:

在此处输入图像描述

BODY坐标系中的相机平台测量:

Roll:     -10
Pitch:    -30
Yaw:      166 (angular deviation from north)

据我了解,偏航角与转换没有直接关系。但是,我可能对此有误。

附加信息:

我想指定使用设置的环境不包含可以可靠地用作参考的线条(海洋照片)(地平线通常不在图片中)。此外,初始图像中的正方形仅用作衡量转换是否正确的指标,在​​真实场景中不会出现。

4

4 回答 4

8

所以,这就是我最终做的事情:我认为除非你实际处理的是 3D 图像,否则矫正照片的透视是一项 2D 操作。考虑到这一点,我将变换矩阵的 z 轴值替换为 0 和 1,并对图像应用 2D 仿射变换。

测量 Roll = -10 和 Pitch = -30 的初始图像(参见初始帖子)的旋转按以下方式完成:

R_rotation = R_y(-60)*R_x(10); 
R_2d       = [   R_rot(1,1)  R_rot(1,2) 0; 
                 R_rot(2,1)  R_rot(2,2) 0;
                 0           0          1    ] 

这意味着将相机平台旋转到虚拟相机方向,其中相机放置在场景上方,指向正下方。请注意上面矩阵中用于滚动和俯仰的值。

此外,如果旋转图像以使其与平台航向对齐,则可能会添加围绕 z 轴的旋转,给出:

R_rotation = R_y(-60)*R_x(10)*R_z(some_heading); 
R_2d       = [   R_rot(1,1)  R_rot(1,2) 0; 
                 R_rot(2,1)  R_rot(2,2) 0;
                 0           0          1    ] 

请注意,这不会更改实际图像 - 它只会旋转它。

因此,围绕 Y 轴和 X 轴旋转的初始图像如下所示:

在此处输入图像描述

如上所示,进行这种转换的完整代码是:

% Load image
img = imread('initial_image.jpg'); 

% Full rotation matrix. Z-axis included, but not used.
R_rot = R_y(-60)*R_x(10)*R_z(0); 

% Strip the values related to the Z-axis from R_rot
R_2d  = [   R_rot(1,1)  R_rot(1,2) 0; 
            R_rot(2,1)  R_rot(2,2) 0;
            0           0          1    ]; 

% Generate transformation matrix, and warp (matlab syntax)
tform = affine2d(R_2d);
outputImage = imwarp(img,tform);

% Display image
figure(1), imshow(outputImage);



%*** Rotation Matrix Functions ***%

%% Matrix for Yaw-rotation about the Z-axis
function [R] = R_z(psi)
    R = [cosd(psi) -sind(psi) 0;
         sind(psi)  cosd(psi) 0;
         0          0         1];
end

%% Matrix for Pitch-rotation about the Y-axis
function [R] = R_y(theta)
    R = [cosd(theta)    0   sind(theta);
         0              1   0          ;
         -sind(theta)   0   cosd(theta)     ];
end

%% Matrix for Roll-rotation about the X-axis
function [R] = R_x(phi)
    R = [1  0           0;
         0  cosd(phi)   -sind(phi);
         0  sind(phi)   cosd(phi)];
end

感谢您的支持,我希望这对某人有帮助!

于 2013-12-09T11:39:10.233 回答
3

我认为您可以通过这种方式进行转换:

1) 让您有四个 3d 点 A(-1,-1,0)、B(1,-1,0)、C(1,1,0) 和 D(-1,1,0)。您可以取任意 4 个非共线点。它们与图像无关。

2)你有变换矩阵,所以你可以通过将点坐标乘以变换矩阵来设置你的相机。您将获得相对于相机位置/方向的 3d 坐标。

3)您需要将您的点投影到屏幕平面。最简单的方法是使用正投影(忽略深度坐标)。在这个阶段,您已经获得了变换点的 2D 投影。

4)一旦你有 2 组 2D 点坐标(步骤 1 中没有第三坐标的集合和步骤 3 中的集合),你可以以标准方式计算单应矩阵。

5) 对图像应用逆同形变换。

于 2013-12-09T10:28:40.430 回答
2

您需要估计单应性。对于现成的 Matlab 解决方案,请参阅http://www.robots.ox.ac.uk/~vgg/hzbook/code/中的函数vgg_H_from_x_lin.m

对于理论,深入研究计算机视觉教科书,例如在http://szeliski.org/Book/http://programmingcomputervision.com/downloads/ProgrammingComputerVision_CCdraft.pdf的第 3 章中免费提供的教科书

于 2013-12-09T11:46:15.777 回答
2

由于我对相机参数的误解,我的回答可能不正确,但我想知道 Yaw/Pitch/Roll 是否与您的对象的位置有关。我使用了通用旋转的公式,我的代码如下(旋转函数R_x,,,是从你那里复制的R_yR_z我没有在这里粘贴它们)

close all
file='http://i.stack.imgur.com/m5e01.jpg'; % original image
I=imread(file);

R_rot = R_x(-10)*R_y(-30)*R_z(166);
R_rot = inv(R_rot);

R_2d  = [   R_rot(1,1)  R_rot(1,2) 0; 
            R_rot(2,1)  R_rot(2,2) 0;
            0           0          1    ]; 


T = maketform('affine',R_2d);

transformedI = imtransform(I,T);
        figure, imshow(I), figure, imshow(transformedI)

结果:

在此处输入图像描述

这表明您仍然需要一些旋转操作才能在您的脑海中获得“正确”的对齐方式(但可能不需要相机脑海中的正确位置)。所以我改为R_rot = inv(R_rot);R_rot = inv(R_rot)*R_x(-5)*R_y(25)*R_z(180);现在它给了我:

在此处输入图像描述

看起来更像你想要的。谢谢。

于 2013-12-09T16:06:56.210 回答