计算在2D中将四边形变换为另一个四边形的矩阵

C g*_*ics 5 matlab robotics computer-vision numerical-methods projective-geometry

在下图中,目标是计算单应矩阵H,其将点a1 a2 a3 a4变换为它们的对应点b1 b2 b3 b4.那是:

[b1 b2 b3 b4] = H * [a1 a2 a3 a4]
Run Code Online (Sandbox Code Playgroud)

您建议采用什么方式成为计算H(3x3)的最佳方法.a1 ... b4是2D中的点,它们在齐次坐标系中表示(即[a1_x a1_y 1]',...). 编辑:对于这些类型的问题我们使用SVD,所以我想看看如何在Matlab中简单地完成.

编辑:

以下是我最初尝试使用Maltlab中的svd(H = Q/P)来解决它的方法.Cosider给出了给定示例的以下代码

px=[0 1 1 0];  % a square
py=[1 1 0 0];

qx=[18 18 80 80];    % a random quadrangle
qy=[-20 20 60 -60];
if (DEBUG)
  fill(px,py,'r');
  fill(qx,qy,'r');
end

Q=[qx;qy;ones(size(qx))];
P=[px;py;ones(size(px))];
H=Q/P;
H*P-Q
answer:
   -0.0000         0         0         0         0
  -20.0000   20.0000  -20.0000   20.0000    0.0000
   -0.0000         0         0         0   -0.0000
Run Code Online (Sandbox Code Playgroud)

我期待答案是一个空矩阵但它不是!...这就是我在StackOverflow中提出这个问题的原因.现在,我们都知道这是一个投射变换,而不是欧几里德.然而,很好地知道在一般护理中是否可以仅使用4个点来计算这样的矩阵.

矩阵计算

Amr*_*mro 1

使用您发布的数据:

P = [px(:) py(:)];
Q = [qx(:) qy(:)];
Run Code Online (Sandbox Code Playgroud)

计算变换:

H = Q/P;
Run Code Online (Sandbox Code Playgroud)

应用变换:

Q2 = H*P;
Run Code Online (Sandbox Code Playgroud)

比较结果:

err = Q2-Q
Run Code Online (Sandbox Code Playgroud)

输出:

err =
   7.1054e-15   7.1054e-15
  -3.5527e-15  -3.5527e-15
  -1.4211e-14  -2.1316e-14
   1.4211e-14   1.4211e-14
Run Code Online (Sandbox Code Playgroud)

对于所有意图和目的来说都是零..


编辑:

因此,正如您在评论中指出的,上述方法不会计算 3x3 单应矩阵。它只是使用与所提供的点一样多的方程来求解方程组:

H * A = B   -->   H = B*inv(A)   -->   H = B/A (mrdivide)
Run Code Online (Sandbox Code Playgroud)

另外,MATLAB的图像处理工具箱中有CP2TFORM函数。以下是应用于所示图像的示例:

%# read illustration image
img = imread('https://i.stack.imgur.com/ZvaZK.png');
img = imcomplement(im2bw(img));

%# split into two equal-sized images
imgs{1} = img(:,fix(1:end/2));
imgs{2} = img(:,fix(end/2:end-1));

%# extract the four corner points A and B from both images
C = cell(1,2);
for i=1:2
    %# some processing
    I = imfill(imgs{i}, 'holes');
    I = bwareaopen(imclearborder(I),200);
    I = imfilter(im2double(I), fspecial('gaussian'));

    %# find 4 corners
    C{i} = corner(I, 4);

    %# sort corners in a consistent way (counter-clockwise)
    idx = convhull(C{i}(:,1), C{i}(:,2));
    C{i} = C{i}(idx(1:end-1),:);
end

%# show the two images with the detected corners
figure
for i=1:2
    subplot(1,2,i), imshow(imgs{i})
    line(C{i}(:,1), C{i}(:,2), 'Color','r', 'Marker','*', 'LineStyle','none')
    text(C{i}(:,1), C{i}(:,2), num2str((1:4)'), 'Color','r', ...
        'FontSize',18, 'Horiz','left', 'Vert','bottom')
end
Run Code Online (Sandbox Code Playgroud)

检测到角点后,现在我们可以获得空间变换:

%# two sets of points
[A,B] = deal(C{:});

%# infer projective transformation using CP2TFORM
T = cp2tform(A, B, 'projective');

%# 3x3 Homography matrix
H = T.tdata.T;
Hinv = T.tdata.Tinv;

%# align points in A into B
X = tformfwd(T, A(:,1), A(:,2));

%# show result of transformation
line(X([1:end 1],1), X([1:end 1],2), 'Color','g', 'LineWidth',2)
Run Code Online (Sandbox Code Playgroud)

结果:

>> H = T.tdata.T
H =
      0.74311    -0.055998    0.0062438
      0.44989      -1.0567   -0.0035331
      -293.31       62.704      -1.1742

>> Hinv = T.tdata.Tinv
Hinv =
       -1.924     -0.42859   -0.0089411
      -2.0585      -1.2615   -0.0071501
       370.68       39.695            1
Run Code Online (Sandbox Code Playgroud)

截屏

我们可以自己验证计算:

%# points must be in Homogenous coordinates (x,y,w)
>> Z = [A ones(size(A,1),1)] * H;
>> Z = bsxfun(@rdivide, Z, Z(:,end))   %# divide by w
Z =
          152           57            1
          219          191            1
           62          240            1
           92          109            1
Run Code Online (Sandbox Code Playgroud)

映射到 B 中的点:

%# maximum error
>> max(max( abs(Z(:,1:2)-B) ))
ans =
   8.5265e-14
Run Code Online (Sandbox Code Playgroud)