计算旋转二维图像的平移值和旋转角度 [英] Calculating translation value and rotation angle of a rotated 2D image

查看:137
本文介绍了计算旋转二维图像的平移值和旋转角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张图片,其中一张是原始图片,第二张是转换后的图片.

我必须找出使用 3x3 转换矩阵旋转了多少度转换后的图像.另外,我需要找出从原点翻译的距离.

两幅图像都经过灰度处理并保存在矩阵变量中.它们的大小相同 [350 500].

我找到了一些讲义,比如问题.您正在做的是给定一幅图像中的坐标和另一幅图​​像中的相应坐标,您正在尝试恢复用于将点从一个图像扭曲到另一个图像的组合平移和旋转矩阵.

通过将两个矩阵相乘,您基本上可以将旋转和平移组合到一个矩阵中.乘法只是将两个运算组合在一起.你会得到:

H = [cos(theta) -sin(theta) tx][sin(theta) cos(theta) ty][ 0 0 1]

这背后的想法是通过每对点之间的最小二乘法最小化误差来找到参数.

基本上,你要找的是以下关系:

xi_after = H*xi_before

H 是将坐标从一个图像映射到另一个所需的组合旋转和平移矩阵.H 也是一个 3 x 3 矩阵,并且知道右下方的条目(第 3 行,第 3 列)是 1,这使事情变得更容易.此外,假设您的点在增强坐标系中,我们本质上希望找到从第一幅图像 (x_i, y_i) 到另一个 (x_i', y_i'):

[p_i*x_i'] [h11 h12 h13] [x_i][p_i*y_i'] = [h21 h22 h23] * [y_i][ p_i ] [h31 h32 1 ] [ 1 ]

p_i 的标度是为了说明单应标度和消失点.让我们执行这个方程的矩阵向量乘法.我们可以忽略第三个元素,因为它对我们没有用(目前):

p_i*x_i' = h11*x_i + h12*y_i + h13p_i*y_i' = h21*x_i + h22*y_i + h23

现在让我们看看第三个元素.我们知道p_i = h31*x_i + h32*y_i + 1.因此,将p_i代入每个方程,重新排列求解x_i'y_i',我们得到:

x_i' = h11*x_i + h12*y_i + h13 - h31*x_i*x_i' - h32*y_i*x_i'y_i' = h21*x_i + h22*y_i + h23 - h31*x_i*y_i' - h32*y_i*y_i'

您现在拥有的是每个独特点对的两个方程.我们现在能做的是建立一个超定方程组.取出每一对并从中建立两个方程.然后你将把它变成矩阵形式,即:

<块引用>

啊 = b

A 将是使用第一张图像的坐标从每组方程构建的系数矩阵,b 将是每对点第二个图像和 h 将是您正在求解的参数.最终,您最终要解决这个以矩阵形式重新表述的线性方程组:

您将求解可以通过最小二乘法执行的向量 h.在 MATLAB 中,您可以通过以下方式执行此操作:

h = A;

给你的一个旁注:如果图像之间的移动真的只是一个旋转和平移,那么在我们求解参数后,h31 和 h32 都将为零.但是,我总是喜欢彻底,所以无论如何我都会解决h31和h32.

注意:此方法仅适用于至少 4 独特点对.因为有 8 个参数需要求解,并且每个点有 2 个方程,A 必须至少有 8 的秩才能使系统保持一致(如果你想加入一些线性循环中的代数术语).如果您的分数低于 4 分,您将无法解决此问题.

如果您需要一些 MATLAB 代码,假设您的点存储在 sourcePointstargetPoints 中.sourcePoints 来自第一张图片,targetPoints 来自第二张图片.显然,两个图像之间应该有相同数量的点.假设 sourcePointstargetPoints 都存储为 M x 2 矩阵.第一列包含您的 x 坐标,而第二列包含您的 y 坐标.

numPoints = size(sourcePoints, 1);%//将数据转换为两倍以确保sourcePoints = double(sourcePoints);targetPoints = double(targetPoints);%//提取相关数据xSource = sourcePoints(:,1);ySource = sourcePoints(:,2);xTarget = targetPoints(:,1);yTarget = targetPoints(:,2);%//创建辅助向量vec0 = zeros(numPoints, 1);vec1 = ones(numPoints, 1);xSourcexTarget = -xSource.*xTarget;ySourcexTarget = -ySource.*xTarget;xSourceyTarget = -xSource.*yTarget;ySourceyTarget = -ySource.*yTarget;%//建立矩阵A = [xSource ySource vec1 vec0 vec0 vec0 xSourcexTarget ySourcexTarget;...vec0 vec0 vec0 xSource ySource vec1 xSourceyTarget ySourceyTarget];%//构建 RHS 向量b = [x目标;y目标];%//用最小二乘法求解单应性h=A;%//重塑为 3 x 3 矩阵(可选)%//必须在执行 reshape 时转置%//列主要格式h(9) = 1;%//在我们重塑之前添加 h33 为 1hmatrix = reshape(h, 3, 3)';

完成后,您将拥有一个组合的旋转和平移矩阵.如果您需要 xy 翻译,只需在 hmatrix 中选择第 3 列、第 1 行和第 2 行.但是,我们也可以使用 h 本身的向量,因此 h13 将是元素 3,而 h23 将是元素编号 6.如果您想要旋转角度,只需采用适当的反三角函数函数到第 1、2 行和第 1、2 列.对于 h 向量,这将是元素 1、2、4 和 5.根据您选择的元素,会有一些不一致这是通过最小二乘法解决的.获得良好整体角度的一种方法可能是找到所有 4 个元素的角度,然后进行某种平均.无论哪种方式,这是一个很好的起点.

参考文献

我不久前通过 Leow Wee Kheng 的计算机视觉课程了解了单应性.我告诉你的是基于他的幻灯片:http://www.comp.nus.edu.sg/~cs4243/lecture/camera.pdf.如果您想知道我从哪里提取这些材料,请查看幻灯片 30-32.但是,我自己写的 MATLAB 代码 :)

I have two images which one of them is the Original image and the second one is Transformed image.

I have to find out how many degrees Transformed image was rotated using 3x3 transformation matrix. Plus, I need to find how far translated from origin.

Both images are grayscaled and held in matrix variables. Their sizes are same [350 500].

I have found a few lecture notes like this.

Lecture notes say that I should use the following matrix formula for rotation:

For translation matrix the formula is given:

Everything is good. But there are two problems:

  1. I could not imagine how to implement the formulas using MATLAB.
  2. The formulas are shaped to find x',y' values but I already have got x,x',y,y' values. I need to find rotation angle (theta) and tx and ty.
  3. I want to know the equivailence of x, x', y, y' in the the matrix.

I have got the following code:

rotationMatrix = [   cos(theta) sin(theta) 0 ; ...
                    -sin(theta) cos(theta) 0 ; ...
                             0          0  1];


translationMatrix = [  1  0  tx; ...
                       0  1  ty; ...
                       0  0   1];

But as you can see, tx, ty, theta variables are not defined before used. How can I calculate theta, tx and ty?

PS: It is forbidden to use Image Processing Toolbox functions.

解决方案

This is essentially a homography recovery problem. What you are doing is given co-ordinates in one image and the corresponding co-ordinates in the other image, you are trying to recover the combined translation and rotation matrix that was used to warp the points from the one image to the other.

You can essentially combine the rotation and translation into a single matrix by multiplying the two matrices together. Multiplying is simply compositing the two operations together. You would this get:

H = [cos(theta) -sin(theta)  tx]
    [sin(theta) cos(theta)   ty]
    [    0           0        1]

The idea behind this is to find the parameters by minimizing the error through least squares between each pair of points.

Basically, what you want to find is the following relationship:

xi_after = H*xi_before

H is the combined rotation and translation matrix required to map the co-ordinates from the one image to the other. H is also a 3 x 3 matrix, and knowing that the lower right entry (row 3, column 3) is 1, it makes things easier. Also, assuming that your points are in the augmented co-ordinate system, we essentially want to find this relationship for each pair of co-ordinates from the first image (x_i, y_i) to the other (x_i', y_i'):

[p_i*x_i']   [h11 h12 h13]   [x_i]
[p_i*y_i'] = [h21 h22 h23] * [y_i]
[  p_i   ]   [h31 h32  1 ]   [ 1 ]

The scale of p_i is to account for homography scaling and vanishing points. Let's perform a matrix-vector multiplication of this equation. We can ignore the 3rd element as it isn't useful to us (for now):

p_i*x_i' = h11*x_i + h12*y_i + h13
p_i*y_i' = h21*x_i + h22*y_i + h23

Now let's take a look at the 3rd element. We know that p_i = h31*x_i + h32*y_i + 1. As such, substituting p_i into each of the equations, and rearranging to solve for x_i' and y_i', we thus get:

x_i' = h11*x_i + h12*y_i + h13 - h31*x_i*x_i' - h32*y_i*x_i'
y_i' = h21*x_i + h22*y_i + h23 - h31*x_i*y_i' - h32*y_i*y_i'

What you have here now are two equations for each unique pair of points. What we can do now is build an over-determined system of equations. Take each pair and build two equations out of them. You will then put it into matrix form, i.e.:

Ah = b

A would be a matrix of coefficients that were built from each set of equations using the co-ordinates from the first image, b would be each pair of points for the second image and h would be the parameters you are solving for. Ultimately, you are finally solving this linear system of equations reformulated in matrix form:

You would solve for the vector h which can be performed through least squares. In MATLAB, you can do this via:

h = A  b;

A sidenote for you: If the movement between images is truly just a rotation and translation, then h31 and h32 will both be zero after we solve for the parameters. However, I always like to be thorough and so I will solve for h31 and h32 anyway.

NB: This method will only work if you have at least 4 unique pairs of points. Because there are 8 parameters to solve for, and there are 2 equations per point, A must have at least a rank of 8 in order for the system to be consistent (if you want to throw in some linear algebra terminology in the loop). You will not be able to solve this problem if you have less than 4 points.

If you want some MATLAB code, let's assume that your points are stored in sourcePoints and targetPoints. sourcePoints are from the first image and targetPoints are for the second image. Obviously, there should be the same number of points between both images. It is assumed that both sourcePoints and targetPoints are stored as M x 2 matrices. The first columns contain your x co-ordinates while the second columns contain your y co-ordinates.

numPoints = size(sourcePoints, 1);

%// Cast data to double to be sure
sourcePoints = double(sourcePoints);
targetPoints = double(targetPoints);

%//Extract relevant data
xSource = sourcePoints(:,1);
ySource = sourcePoints(:,2);
xTarget = targetPoints(:,1);
yTarget = targetPoints(:,2);

%//Create helper vectors
vec0 = zeros(numPoints, 1);
vec1 = ones(numPoints, 1);

xSourcexTarget = -xSource.*xTarget;
ySourcexTarget = -ySource.*xTarget;
xSourceyTarget = -xSource.*yTarget;
ySourceyTarget = -ySource.*yTarget;

%//Build matrix
A = [xSource ySource vec1 vec0 vec0 vec0 xSourcexTarget ySourcexTarget; ...
    vec0 vec0 vec0 xSource ySource vec1 xSourceyTarget ySourceyTarget];

%//Build RHS vector
b = [xTarget; yTarget];

%//Solve homography by least squares
h = A  b;

%// Reshape to a 3 x 3 matrix (optional)
%// Must transpose as reshape is performed
%// in column major format
h(9) = 1; %// Add in that h33 is 1 before we reshape
hmatrix = reshape(h, 3, 3)';

Once you are finished, you have a combined rotation and translation matrix. If you want the x and y translations, simply pick off column 3, rows 1 and 2 in hmatrix. However, we can also work with the vector of h itself, and so h13 would be element 3, and h23 would be element number 6. If you want the angle of rotation, simply take the appropriate inverse trigonometric function to rows 1, 2 and columns 1, 2. For the h vector, this would be elements 1, 2, 4 and 5. There will be a bit of inconsistency depending on which elements you choose as this was solved by least squares. One way to get a good overall angle would perhaps be to find the angles of all 4 elements then do some sort of average. Either way, this is a good starting point.

References

I learned about homography a while ago through Leow Wee Kheng's Computer Vision course. What I have told you is based on his slides: http://www.comp.nus.edu.sg/~cs4243/lecture/camera.pdf. Take a look at slides 30-32 if you want to know where I pulled this material from. However, the MATLAB code I wrote myself :)

这篇关于计算旋转二维图像的平移值和旋转角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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