如何旋转和缩放单应性矩阵

5

我需要帮助,

我从服务器接收到一个单应矩阵,因此我想将这个单应矩阵规范化到我的应用程序的坐标系中。当我尝试在坐标系中表示一个对象时,服务器应用程序生成下列4个点:

received [96.629539, 217.31934; 97.289948, 167.21941; 145.69249, 168.28044; 145.69638, 219.84604]

而我的应用程序生成了下列4个点:

local [126.0098, 55.600437; 262.39163, 53.98035; 259.41382, 195.34763; 121.48138, 184.95235]

如果你在图形中表示这些点,R(received),P(local)

enter image description here

看起来生成的正方形被旋转和缩放了,因此我想知道是否有任何方法可以将这个旋转和缩放应用于服务器单应矩阵,以便能够拥有与我的应用程序单应矩阵相同的单应矩阵。

谢谢,如果您需要更多信息,请问我。


非常感谢快速的答复,最终我使用了其他的近似方法,就是从服务器获取点并使用findhomography来获取逆单应矩阵。

homography=findHomography(srcPoints, dstPoints, match_mask, RANSAC, 10);

谢谢!!!


我不明白你想做什么。 - Thomash
听起来像是透视变换?你是在将一个四边形映射到另一个吗? - PeskyGnat
我想对单应性矩阵进行修改,将其转换为我的单位/坐标系统。 - Gustavo
2个回答

4

我想我弄清楚了。下面是您的两个单应性更准确的绘图。其中蓝色是“接收到”的单应性,红色是“本地”的单应性。

enter image description here

您可以使用OpenCV函数getAffineTransform来计算3个点对的仿射变换(我不得不重新组织您的点对,因为它们的顺序错误)。我在numpy中执行了以下操作:

r = array([[97.289948, 167.21941], [96.629539, 217.31934], [145.69638, 219.84604]], np.float32)
l = array([[126.0098, 55.600437], [121.48138, 184.95235], [259.41382, 195.34763]], np.float32)
A = cv2.getAffineTransform(r, l)

这给我们带来了以下的仿射关系:

array([[  2.81385763e+00,  -5.32961421e-02,  -1.38838108e+02],
       [  7.88519054e-02,   2.58291747e+00,  -3.83984986e+02]])

我将这应用到 r 上,以查看是否可以获得 l ,以确保它像这样工作:

# split affine warp into rotation, scale, and/or shear + translation matrix
T = mat(A[:, 2]).T
matrix([[-138.83810801],
        [-383.98498637]])

A = mat(A[:, 0:2])
matrix([[ 2.81385763, -0.05329614],
        [ 0.07885191,  2.58291747]])

# apply warp to r to get l
r = mat(r).T
A*r + T
# gives
matrix([[ 126.00980377,  121.48137665,  259.41381836],
        [  55.60043716,  184.9523468 ,  195.34762573]])
# which equals
l = mat(l).T
matrix([[ 126.00980377,  121.48137665,  259.41381836],
        [  55.60043716,  184.9523468 ,  195.34762573]], dtype=float32)

值得注意的是,您可以使用OpenCV函数getPerspectiveTransform生成透视变换,就像Markus Jarderot所展示的那样。
希望这有所帮助!

2
如果您将点和变换插入到Maple中,您可以很快得到结果。
> with(LinearAlgebra);

> # The server coordinates
  pa := [[96.629539, 217.31934], [97.289948, 167.21941], [145.69249, 168.28044],
         [145.69638, 219.84604]]:

> # The local coordiantes
  pb := [[126.0098, 55.600437], [262.39163, 53.98035], [259.41382, 195.34763],
         [121.48138, 184.95235]]:

> # The placeholder variables for the transformation (last one is '1', because it
  # is scale-invariant)
  T := [seq]([seq](`if`(i = 3 and j = 3, 1, t[i, j]), j = 1 .. 3), i = 1 .. 3):
  V := convert(map(op, T)[1 .. -2], set):

> # Transformation function (Matrix multiplication + divide with 3rd coordinate)
  trans := (p, T) -> [
      (T[1, 1]*p[1]+T[1, 2]*p[2]+T[1, 3])/(T[3, 1]*p[1]+T[3, 2]*p[2]+T[3, 3]),
      (T[2, 1]*p[1]+T[2, 2]*p[2]+T[2, 3])/(T[3, 1]*p[1]+T[3, 2]*p[2]+T[3, 3])
  ]:

> # Transform pa, and construct the equation system
  pat := map(trans, pa, T):
  eqs := {op}(zip((p1, p2) -> op(zip(`=`, p1, p2)), pat, pb)):

> # Solve for the transform variables
  sol := solve(eqs, V):

> # Populate the transform
  eval(T, sol);

输出:

[[  .1076044020,   -3.957029830,    1074.517140  ],
 [ 4.795375318,      .3064507355,   -430.7044862 ],
 [ 0.3875626264e-3, 0.3441632491e-2,   1         ]]

要使用此功能,请将其与服务器点相乘,如T * <x,y,1>


void ServerToLocal(double serverX, double serverY, double *localX, double *localY)
{
    double w;
    w = 0.3875626264e-3 * serverX + 0.3441632491e-2 * serverY + 1.0;
    *localX = (.1076044020 * serverX - 3.957029830 * serverY + 1074.517140) / w;
    *localY = (4.795375318 * serverX + .3064507355 * serverY - 430.7044862) / w;
}

可以阅读另一种方法,链接在这里

只要有一个合理的线性代数库,这个方法可以用 C 语言编写。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接