C++ 从单应矩阵计算缩放、旋转和平移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25658443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating scale, rotation and translation from Homography matrix
提问by Lakshya Kejriwal
I am trying to calculate scale, rotation and translation between two consecutive frames of a video. So basically I matched keypoints and then used opencv function findHomography()
to calculate the homography matrix.homography = findHomography(feature1 , feature2 , CV_RANSAC); //feature1 and feature2 are matched keypoints
我正在尝试计算视频的两个连续帧之间的缩放、旋转和平移。所以基本上我匹配了关键点,然后使用 opencv 函数findHomography()
来计算单应矩阵。homography = findHomography(feature1 , feature2 , CV_RANSAC); //feature1 and feature2 are matched keypoints
My question is: How can I use this matrix to calculate scale, rotation and translation?.
Can anyone provide me the code or explanation as to how to do it?
我的问题是:如何使用这个矩阵来计算缩放、旋转和平移?。
任何人都可以向我提供有关如何操作的代码或解释吗?
采纳答案by Vineet
if you can use opencv 3.0, this decomposition method is available http://docs.opencv.org/3.0-beta/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#decomposehomographymat
如果您可以使用 opencv 3.0,则可以使用这种分解方法 http://docs.opencv.org/3.0-beta/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#decomposehomographymat
回答by Emiswelt
For estimating a tree-dimensional transform and rotation induced by a homography, there exist multiple approaches. One of themprovides closed formulas for decomposing the homography, but they are very complex. Also, the solutions are never unique.
为了估计由单应性引起的树维变换和旋转,存在多种方法。其中之一提供了用于分解单应性的封闭公式,但它们非常复杂。此外,解决方案从来都不是唯一的。
Luckily, OpenCV 3 already implements this decomposition (decomposeHomographyMat). Given an homography and a correctly scaled intrinsics matrix, the function provides a set of four possible rotations and translations.
幸运的是,OpenCV 3 已经实现了这种分解(decomposeHomographyMat)。给定单应性和正确缩放的内在矩阵,该函数提供一组四种可能的旋转和平移。
回答by Emiswelt
Given Homography matrix H
:
给定单应矩阵H
:
|H_00, H_01, H_02|
H = |H_10, H_11, H_12|
|H_20, H_21, H_22|
Assumptions:
假设:
H_20 = H_21 = 0
and normalized to H_22 = 1
to obtain 8 DOF.
H_20 = H_21 = 0
并归一化以H_22 = 1
获得 8 DOF。
The translation along x and y axes are directly calculated from H
:
沿 x 和 y 轴的平移直接计算为H
:
tx = H_02
ty = H_12
The 2x2
sub matrix on the top left corner is decomposed to calculate shear, scaling and rotation. An easy and quick decomposition method is explained here.
2x2
左上角的子矩阵被分解以计算剪切、缩放和旋转。这里解释了一种简单快速的分解方法。
Note: this method assumes invertible matrix.
注意:此方法假设矩阵可逆。
回答by Vlad
The right answer is to use homographyas it is defined dst = H ? src
and explore what it does to small segments around a particular point.
正确的答案是使用定义的单应性dst = H ? src
并探索它对特定点周围的小段的作用。
Translation
翻译
Given a single point, for translation do
给定一点,翻译做
T = dst - (H ? src)
T = dst - (H ? src)
Rotation
回转
Given two points p1and p2
给定两个点 p 1和 p 2
p1= H ? p1
p2= H ? p2
p 1= H ? p 1
p 2= H ? p 2
Now just calculate the angle between vectors p1p2and p1' p2'.
现在只需计算向量 p 1p 2和 p 1' p 2'之间的角度。
Scale
规模
You can use the same trick but now just compare the lengths: |p1p2| and |p1' p2'|.
您可以使用相同的技巧,但现在只需比较长度:|p 1p 2| 和 |p 1' p 2'|。
To be fair, use another segment orthogonal to the first and average the result. You will see that there is noconstant scale factor or translation one. They will depend on the src
location.
公平地说,使用另一个与第一个正交的段并平均结果。您将看到没有恒定的比例因子或平移因子。它们将取决于src
位置。
回答by Vinmean
The question seems to be about 2D parameters. Homography matrix captures perspective distortion. If the application does not create much perspective distortion, one can approximate a real world transformation using affine transformation matrix (that uses only scale, rotation, translation and no shearing/flipping). The following link will give an idea about decomposing an affine transformation into different parameters.
问题似乎与 2D 参数有关。单应矩阵捕获透视失真。如果应用程序不会产生太多的透视失真,则可以使用仿射变换矩阵(仅使用缩放、旋转、平移且不使用剪切/翻转)来近似真实世界的变换。以下链接将提供有关将仿射变换分解为不同参数的想法。
https://math.stackexchange.com/questions/612006/decomposing-an-affine-transformation
https://math.stackexchange.com/questions/612006/decomposing-an-affine-transformation
回答by deight
Since i had to struggle for a couple of days to create my homography transformation function I'm going to put it here for the benefit of everyone.
由于我不得不花费几天时间来创建我的单应变换函数,因此我将把它放在这里,以造福于所有人。
Here you can see the main loop where every input position is multiplied by the homography matrix h. Then the result is used to copy the pixel from the original position to the destination position.
在这里您可以看到主循环,其中每个输入位置都乘以单应矩阵 h。然后将结果用于将像素从原始位置复制到目标位置。
for (tempIn[0] = 0; tempIn[0] < stride; tempIn[0]++)
{
for (tempIn[1] = 0; tempIn[1] < rows; tempIn[1]++)
{
double w = h[6] * tempIn[0] + h[7] * tempIn[1] + 1; // very important!
//H_20 = H_21 = 0 and normalized to H_22 = 1 to obtain 8 DOF. <-- this is wrong
tempOut[0] = ((h[0] * tempIn[0]) + (h[1] * tempIn[1]) + h[2])/w;
tempOut[1] =(( h[3] * tempIn[0]) +(h[4] * tempIn[1]) + h[5])/w;
if (tempOut[1] < destSize && tempOut[0] < destSize && tempOut[0] >= 0 && tempOut[1] >= 0)
dest_[destStride * tempOut[1] + tempOut[0]] = src_[stride * tempIn[1] + tempIn[0]];
}
}
After such process an image with some kind of grid will be produced. Some kind of filter is needed to remove the grid. In my code i have used a simple linear filter.
经过这样的过程,将产生具有某种网格的图像。需要某种过滤器来去除网格。在我的代码中,我使用了一个简单的线性过滤器。
Note: Only the central part of the original image is really required for producing a correct image. Some rows and columns can be safely discarded.
注意:真正需要原始图像的中心部分才能生成正确的图像。可以安全地丢弃某些行和列。