gpt4 book ai didi

c++ - OpenCV:从 triangulatePoints 获取 3D 坐标

转载 作者:太空狗 更新时间:2023-10-29 23:14:57 51 4
gpt4 key购买 nike

我尝试使用 OpenCV 从两个立体图像中获取点云,但无法获取坐标。

  1. 我使用光流找到了点坐标
  2. 我找到了相机的投影矩阵。
Mat RT1;<br>
hconcat(R, T1, RT1);<br>
Mat P1 = C*RT1;<br>


R为3x3旋转矩阵,T为3x1变换矩阵(列),P1-投影矩阵。

  1. 我将它们传递给 triangulatePoints 函数
triangulatePoints(P1, P2, leftPoints, rightPoints, out);

P1 和 P2 是 3x4 投影矩阵 (Mat_ )。leftPoints 和 rightPoints 是 Point2f 的 std::vector。什么出来了?它应该是 4D 坐标的 1xN 矩阵。这是 Vec4f 吗?

我正在尝试获取坐标

for (int i = 0; i < out.cols; i++)
{
Vec4f vec = out.at<Vec4f>(0, i);
float w = vec[3];
stream << "v " << vec[0] / w << " " << vec[1]/w << " " << vec[2]/w << "\n";
}

但是我有两个问题:

  • 这个循环抛出异常(适用于小 i,大约 out.cols 的 20%)

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)si ze.p[0] && (unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p1 * cha nnels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file c:\opencv\build\includ e\opencv2\core\mat.inl.hpp, line 89

我认为这是某种索引超出范围异常

所以,我做错了什么。如何正确使用此功能并获取点的 3D 坐标?我希望你能帮助我。

最佳答案

我不完全理解您获取坐标的方法。

据我所知,您不应该访问像

这样的元素
out.at<Vec4f>(0, i);

相反,这样做:

float x = out.at<float>(0, i);
float y = out.at<float>(1, i);
float z = out.at<float>(2, i);
float w = out.at<float>(3, i);
stream << "v " << x << " " << y << " " << z << "\n";

或使用 double ...取决于你是否out类型为 CV_32FCV_64F .

这是我的做法:

Mat points3DHomogeneous;
triangulatePoints(projectionMatrixL, projectionMatrixR, pointsL, pointsR, points3DHomogeneous);

projectionMatrixL :

enter image description here

projectionMatrixR :

enter image description here

pointsL :

    700 250
200 300
600 350
400 400
500 450
600 500
700 550
800 600
150 650
1000 700

pointsR :

    690 250
180 300
590 350
385 400
495 450
575 500
691 550
782 600
120 650
960 700

points3DHomogeneous结果是:

enter image description here

关于c++ - OpenCV:从 triangulatePoints 获取 3D 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31284357/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com