问题描述
我想采用从 OpenCV 返回的透视变换矩阵 findHomography
函数并将其(在 C++ 或 Objective-C 中)转换为 iOS 的 CATransform3D
.我希望它们在 Core Graphics 端准确再现扭曲"效果方面尽可能接近.示例代码将不胜感激!
I'd like to take the perspective transform matrix returned from OpenCV's findHomography
function and convert it (either in C++ or Objective-C) to iOS' CATransform3D
. I'd like them to be as close as possible in terms of accurately reproducing the "warp" effect on the Core Graphics side. Example code would really be appreciated!
来自 iOS 的 CATransform3D.h:
From iOS' CATransform3D.h:
/* Homogeneous three-dimensional transforms. */
struct CATransform3D
{
CGFloat m11, m12, m13, m14;
CGFloat m21, m22, m23, m24;
CGFloat m31, m32, m33, m34;
CGFloat m41, m42, m43, m44;
};
类似问题:
将 opencv 仿射矩阵转换为 CGAffineTransform
推荐答案
免责声明
我从未尝试过,所以请谨慎对待.
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
- 单应性的第一列乘以x
- 单应性的第二列乘以y
- 单应性的第三列乘以1
- 得到的第 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.
这篇关于将 OpenCV's findHomography 透视矩阵转换为 iOS'CATransform3D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!