问题描述
我试图用 OpenCV 对一些点进行三角测量,我发现了这个 cv::triangulatePoints()
函数.问题是几乎没有文档或示例.
I am trying to triangulate some points with OpenCV and I found this cv::triangulatePoints()
function. The problem is that there is almost no documentation or examples of it.
我对此有些怀疑.
它使用什么方法?我对三角剖分进行了一项小型研究,有几种方法(线性、线性 LS、本征、迭代 LS、迭代本征等),但我找不到它在 OpenCV 中使用的是哪一种.
What method does it use?I've making a small research about triangulations and there are several methods (Linear, Linear LS, eigen, iterative LS, iterative eigen,...) but I can't find which one is it using in OpenCV.
我应该如何使用它? 作为输入,它似乎需要一个投影矩阵和 3xN 齐次 2D 点.我将它们定义为 std::vector;pnts
,但作为输出,它需要 4xN 数组,显然我无法创建 std::vector
因为它没有t 存在,那么我应该如何定义输出向量?
How should I use it? It seems that as an input it needs a projection matrix and 3xN homogeneous 2D points. I have them defined as std::vector<cv::Point3d> pnts
, but as an output it needs 4xN arrays and obviously I can't create a std::vector<cv::Point4d>
because it doesn't exist, so how should I define the output vector?
对于我尝试的第二个问题:cv::Mat pnts3D(4,N,CV_64F);
和 cv::Mat pnts3d;
,似乎都不起作用(它抛出异常).
For the second question I tried: cv::Mat pnts3D(4,N,CV_64F);
and cv::Mat pnts3d;
, neither seems to work (it throws an exception).
推荐答案
1.- 使用的方法是最小二乘法.还有比这更复杂的算法.它仍然是最常见的一种,因为其他方法在某些情况下可能会失败(即,如果点在平面上或无限上,则其他一些方法会失败).
1.- The method used is Least Squares. There are more complex algorithms than this one. Still it is the most common one, as the other methods may fail in some cases (i.e. some others fails if points are on plane or on infinite).
该方法可以在 Multiple View Geometry in Computer Vision 中找到,Richard Hartley 和 Andrew Zisserman (p312)
The method can be found in Multiple View Geometry in Computer Vision by Richard Hartley and Andrew Zisserman (p312)
2.-用法:
cv::Mat pnts3D(1,N,CV_64FC4);
cv::Mat cam0pnts(1,N,CV_64FC2);
cv::Mat cam1pnts(1,N,CV_64FC2);
用图像中的点填充 2 个 chanel 点矩阵.
Fill the 2 chanel point Matrices with the points in images.
cam0
和 cam1
是 Mat3x4
相机矩阵(内在和外在参数).您可以通过乘以 A*RT 来构建它们,其中 A 是内在参数矩阵,RT 是旋转平移 3x4 姿态矩阵.
cam0
and cam1
are Mat3x4
camera matrices (intrinsic and extrinsic parameters). You can construct them by multiplying A*RT, where A is the intrinsic parameter matrix and RT the rotation translation 3x4 pose matrix.
cv::triangulatePoints(cam0,cam1,cam0pnts,cam1pnts,pnts3D);
注意:pnts3D
需要是 4 通道 1xN cv::Mat
定义时,抛出异常如果不是,但结果是 cv::Mat(4,N,cv_64FC1)
矩阵.真的很混乱,但这是我没有例外的唯一方法.
NOTE: pnts3D
NEEDs to be a 4 channel 1xN cv::Mat
when defined, throws exception if not, but the result is a cv::Mat(4,N,cv_64FC1)
matrix. Really confusing, but it is the only way I didn't got an exception.
UPDATE:从 3.0 版或更早的版本开始,这不再正确,并且 pnts3D
也可以是 Mat(4,N,CV_64FC1)
或者可以完全留空(像往常一样,它是在函数内部创建的).
UPDATE: As of version 3.0 or possibly earlier, this is no longer true, and pnts3D
can also be of type Mat(4,N,CV_64FC1)
or may be left completely empty (as usual, it is created inside the function).
这篇关于如何正确使用 cv::triangulatePoints()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!