matlab - 使用 Roll-Pitch-Yaw 角度变换图像(图像校正)

标签 matlab opencv image-processing image-rotation euler-angles

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

换句话说,我想要一个完美的正方形平躺在地上,从远处用一些相机方向拍摄,然后进行转换,以便正方形之后完全对称。

我一直在尝试通过 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);

结果是:

Image rotated 10 degrees about the Z-image axis

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

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

Image rotated 10 degrees about the X-image axis

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

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

Image rotated 10/1000 degrees about the X-image axis

有人能帮我理解为什么围绕 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

初始图像:

enter image description here

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

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

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

附加信息:

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

最佳答案

所以,这就是我最终所做的:我认为除非您实际处理的是 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 轴旋转的初始图像如下所示:

enter image description here

如上所示,执行此转换的完整代码是:

% 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

感谢大家的支持,希望对大家有所帮助!

关于matlab - 使用 Roll-Pitch-Yaw 角度变换图像(图像校正),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20445147/

相关文章:

matlab - 多次应用MATLAB的idwt2

opencv - 在 Ubuntu 11.10 上安装 OpenCV 2.3.1 时出错

c# - 使用 C# 合并 2 个图像

mysql - Matlab 和 MySQL 找不到合适的驱动程序

c++ - 灰度到红-绿-蓝 (MATLAB Jet) 色标

c - OpenCV:缓冲 IplImage* 以在 pthreads 之间共享

c# - 如何从 PNG 图像中删除空白并重新保存它们

visual-studio-2010 - 在 OpenCV 中使用 calcHist() 函数的参数列表错误

arrays - 用随机 int 数字填充数组

c++ - 返回指针数组(CvSeq 指针)