Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 计算旋转二维图像的平移值和旋转角度_Image_Matlab_Image Processing_Matrix_Computer Vision - Fatal编程技术网

Image 计算旋转二维图像的平移值和旋转角度

Image 计算旋转二维图像的平移值和旋转角度,image,matlab,image-processing,matrix,computer-vision,Image,Matlab,Image Processing,Matrix,Computer Vision,我有两个图像,其中一个是原始图像,第二个是变换图像 我必须找出使用3x3变换矩阵旋转了多少度变换后的图像。另外,我还需要知道翻译离原文有多远 这两幅图像都是灰度的,保存在矩阵变量中。它们的尺寸相同[350 500] 我找到了一些像这样的课堂讲稿 课堂讲稿说,我应该使用以下旋转矩阵公式: 对于平移矩阵,公式如下所示: 一切都很好。但有两个问题: 我无法想象如何使用MATLAB实现这些公式 公式的形状是为了找到x',y'值,但我已经得到了x,x',y值。我需要找到旋转角度(θ)和tx和ty 我想

我有两个图像,其中一个是原始图像,第二个是变换图像

我必须找出使用3x3变换矩阵旋转了多少度变换后的图像。另外,我还需要知道翻译离原文有多远

这两幅图像都是灰度的,保存在矩阵变量中。它们的尺寸相同
[350 500]

我找到了一些像这样的课堂讲稿

课堂讲稿说,我应该使用以下旋转矩阵公式:

对于平移矩阵,公式如下所示:

一切都很好。但有两个问题:

  • 我无法想象如何使用MATLAB实现这些公式
  • 公式的形状是为了找到
    x'
    y'
    值,但我已经得到了
    x
    x'
    y
    值。我需要找到旋转角度(θ)和
    t
    x
    t
    y
  • 我想知道矩阵中
    x
    x'
    y
    y'
    的等价性
  • 我有以下代码:

    rotationMatrix = [   cos(theta) sin(theta) 0 ; ...
                        -sin(theta) cos(theta) 0 ; ...
                                 0          0  1];
    
    
    translationMatrix = [  1  0  tx; ...
                           0  1  ty; ...
                           0  0   1];
    
    但是正如你所看到的,tx,ty,theta变量在使用之前没有定义。如何计算θ、
    t
    x
    t
    y

    PS:禁止使用图像处理工具箱功能。

    这本质上是一个问题。您所做的是在一个图像中给定坐标,在另一个图像中给定相应坐标,您尝试恢复用于将点从一个图像扭曲到另一个图像的组合平移和旋转矩阵

    通过将两个矩阵相乘,基本上可以将旋转和平移合并为一个矩阵。乘法就是简单地将两个操作组合在一起。你会得到:

    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
    的比例是考虑单应缩放和消失点。让我们对这个方程进行矩阵向量乘法。我们可以忽略第三个元素,因为它对我们没有用处(目前):

    现在让我们来看看第三个元素。我们知道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'
    
    现在,对于每一对唯一的点,有两个方程。我们现在可以做的是建立一个过度确定的方程组。取每一对,用它们建立两个方程。然后将其转换为矩阵形式,即:

    Ah=b

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

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

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

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

    如果您需要一些MATLAB代码,让我们假设您的点存储在
    sourcePoints
    targetPoints
    中<代码>源点来自第一幅图像,
    目标点
    用于第二幅图像。显然,两幅图像之间应该有相同数量的点。假设
    源点
    目标点
    都存储为
    M x 2
    矩阵。第一列包含
    x
    坐标,第二列包含
    y
    坐标

    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)';
    
    完成后,将有一个组合的旋转和平移矩阵。如果需要
    x
    y
    翻译,只需在
    hmatrix
    中选择第3列第1行和第2行即可。然而,我们也可以使用
    h
    本身的向量,因此h13将是元素3,h23将是元素数6。如果需要旋转角度,只需对第1行、第2行和第1列、第2列使用适当的逆三角函数即可。对于
    h
    向量,这将是元素1、2、4和5。这将有点不一致,取决于您选择的元素,因为这是通过最小二乘法解决的。获得一个好的整体角度的一种方法可能是找到所有4个元素的角度,然后进行某种平均。不管怎样,这都是一个好主意
    h = A \ b;
    
    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)';