且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 OpenCV's findHomography 透视矩阵转换为 iOS'CATransform3D

更新时间:2023-01-20 16:08:32

免责声明

我从未尝试过,所以请谨慎对待.

I have never tried this so take it with a grain of salt.

CATransform3D 是一个 4x4 矩阵,它对 3 维齐次向量 (4x1) 进行运算以生成另一个相同类型的向量.我假设在渲染时,由 4x1 向量描述的对象将每个元素除以第 4 个元素,而第 3 个元素仅用于确定哪些对象出现在哪些对象之上.假设这是正确的...

CATRansform3D is a 4x4 matrix which operates on a 3 dimensional homogeneous vector (4x1) to produce another vector of the same type. I am assuming that when rendered, objects described by a 4x1 vector have each element divided by the 4th element and the 3rd element is used only to determine which objects appear on top of which. Assuming this is correct...

推理

findHomography 返回的 3x3 矩阵对二维齐次向量进行运算.这个过程可以分为4个步骤

The 3x3 matrix returned by findHomography operates on a 2 dimensional homogeneous vector. That process can be thought of in 4 steps

  1. 单应性的第一列乘以x
  2. 单应性的第二列乘以y
  3. 单应性的第三列乘以1
  4. 得到的第 1 个和第 2 个向量元素除以第 3 个

您需要在 4x4 向量中复制此过程,其中我假设结果向量中的第三个元素对您的目的毫无意义.

You need this process to be replicated in a 4x4 vector in which I am assuming the 3rd element in the resulting vector is meaningless for your purposes.

解决方案

像这样构造你的矩阵(H 是你的单应矩阵)

Construct your matrix like this (H is your homography matrix)

[H(0,0), H(0,1), 0, H(0,2),
 H(1,0), H(1,1), 0, H(1,2),
      0,      0, 1,      0
 H(2,0), H(2,1), 0, H(2,2)]

这显然满足 1,2 和 3.4 是满足的,因为齐次元素总是最后一个.这就是为什么同质行"如果你不得不被撞到一条线上.第 3 行的 1 是让向量的 z 分量不受干扰地通过.

This clearly satisfies 1,2 and 3. 4 is satisfied because the homogeneous element is always the last one. That is why the "homogeneous row" if you will had to get bumped down one line. The 1 on the 3rd row is to let the z component of the vector pass through unmolested.

以上所有内容均以行主要表示法(如 openCV)完成,以尽量避免混淆.您可以查看 Tommy 的回答以了解转换为主要列的外观(您基本上只是将其转置).但是请注意,目前汤米和我不同意如何构建矩阵.

All of the above is done in row major notation (like openCV) to try to keep things from being confusing. You can look at Tommy's answer to see how the conversion to column major looks (you basically just transpose it). Note however that at the moment Tommy and I disagree about how to construct the matrix.