4

我的问题是:

  • 如何确定我的基本矩阵是否正确?
  • 我在下面发布的代码是否对此做出了很好的努力?

我的最终目标是进行某种 3D 重建。现在我正在尝试计算基本矩阵,以便我可以估计两个相机之间的差异。我在 openFrameworks 中使用 ofxCv 插件执行此操作,但在大多数情况下,它只是纯粹的 OpenCV。由于 ofxCv 也在开发中,因此很难发布隔离问题的代码。

我的代码基本上读取了我的网络摄像头从稍微不同的位置拍摄的两个 640x480 帧(基本上只是水平滑动笔记本电脑一点点)。我已经有一个校准矩阵,它是从 ofxCv 的校准代码中获得的,它使用findChessboardCorners. 不失真示例代码似乎表明校准矩阵是准确的。它计算图片(calcOpticalFlowPyrLKcalcOpticalFlowFarneback)之间的光流,并将这些点对馈送到findFundamentalMatrix

为了测试基本矩阵是否有效,我将其分解为旋转和平移矩阵。然后我将旋转矩阵乘以第二张图像的点,看看相机之间的旋转差异是多少。我认为任何差异都应该很小,但我得到了很大的差异。

这是我最后一个代码的基本和旋转矩阵,如果有帮助的话:

fund: [-8.413948689969405e-07, -0.0001918870646474247, 0.06783422344973795;
    0.0001877654679452431, 8.522397812179886e-06, 0.311671691674232;
    -0.06780237856576941, -0.3177275967586101, 1]
R: [0.8081771697692786, -0.1096128431920695, -0.5786490187247098;
    -0.1062963539438068, -0.9935398408215166, 0.03974506055610323;
    -0.5792674230456705, 0.02938723035105822, -0.8146076621848839]
t: [0, 0.3019063882496216, -0.05799044915951077;
    -0.3019063882496216, 0, -0.9515721940769112;
    0.05799044915951077, 0.9515721940769112, 0]

这是我的部分代码,发生在拍摄第二张照片之后:

const ofImage& image1 = images[images.size() - 2];
const ofImage& image2 = images[images.size() - 1];

std::vector<cv::Point2f> points1 = flow->getPointsPrev();
std::vector<cv::Point2f> points2 = flow->getPointsNext();

std::vector<cv::KeyPoint> keyPoints1 = convertFrom(points1);
std::vector<cv::KeyPoint> keyPoints2 = convertFrom(points2);

std::cout << "points1: " << points1.size() << std::endl;
std::cout << "points2: " << points2.size() << std::endl;


fundamentalMatrix = (cv::Mat)cv::findFundamentalMat(points1, points2);
cv::Mat cameraMatrix = (cv::Mat)calibration.getDistortedIntrinsics().getCameraMatrix();
cv::Mat cameraMatrixInv = cameraMatrix.inv();
std::cout << "fund: " << fundamentalMatrix << std::endl;

essentialMatrix = cameraMatrix.t() * fundamentalMatrix * cameraMatrix;

cv::SVD svd(essentialMatrix);
Matx33d W(0,-1,0,   //HZ 9.13
          1,0,0,
          0,0,1);

cv::Mat_<double> R = svd.u * Mat(W).inv() * svd.vt; //HZ 9.19

std::cout << "R: " << (cv::Mat)R << std::endl;
Matx33d Z(0, -1, 0,
          1, 0, 0,
          0, 0, 0);
cv::Mat_<double> t = svd.vt.t() * Mat(Z) * svd.vt;
std::cout << "t: " << (cv::Mat)t << std::endl;

Vec3d tVec = Vec3d(t(1,2), t(2,0), t(0,1));

Matx34d P1 = Matx34d(R(0,0),    R(0,1), R(0,2), tVec(0),
                     R(1,0),    R(1,1), R(1,2), tVec(1),
                     R(2,0),    R(2,1), R(2,2), tVec(2));
ofMatrix4x4 ofR(R(0,0),    R(0,1), R(0,2), 0,
                R(1,0),    R(1,1), R(1,2), 0,
                R(2,0),    R(2,1), R(2,2), 0,
                0, 0, 0, 1);
ofRs.push_back(ofR);

cv::Matx34d P(1,0,0,0,
              0,1,0,0,
              0,0,1,0);

for (int y = 0; y < image1.height; y += 10) {
    for (int x = 0; x < image1.width; x += 10) {
        Vec3d vec(x, y, 0);

        Point3d point1(vec.val[0], vec.val[1], vec.val[2]);
        Vec3d result = (cv::Mat)((cv::Mat)R * (cv::Mat)vec);
        Point3d point2 = result;


        mesh.addColor(image1.getColor(x, y));
        mesh.addVertex(ofVec3f(point1.x, point1.y, point1.z));

        mesh.addColor(image2.getColor(x, y));
        mesh.addVertex(ofVec3f(point2.x, point2.y, point2.z));
    }
}

有任何想法吗?我的基本矩阵看起来是正确的,还是我在测试它时有错误的想法?

4

3 回答 3

2

如果你想知道你的基本矩阵是否正确,你应该计算误差。使用极线约束方程,您可以检查一张图像中检测到的特征与另一张图像的极线有多接近。理想情况下,这些点积的总和应为 0,因此,校准误差计算为绝对距离之和 (SAD)。SAD 的平均值报告为立体校准误差。基本上,您正在从相应的极线计算 image_left (可能是棋盘角)中的计算特征的 SAD。此误差以像素 ^ 2 为单位,低于 1 的任何值都是可以接受的。

OpenCV 有代码示例,请查看 Stereo Calibrate cpp 文件,它向您展示了如何计算此错误。 https://code.ros.org/trac/opencv/browser/trunk/opencv/samples/c/stereo_calib.cpp?rev=2614 查看“avgErr”第 260-269 行

安库尔

于 2012-08-24T06:00:59.130 回答
0

我写了一个python函数来做到这一点:

def Ferror(F,pts1,pts2):  # pts are Nx3 array of homogenous coordinates.  
    # how well F satisfies the equation pt1 * F * pt2 == 0
    vals = pts1.dot(F).dot(pts2.T)
    err = np.abs(vals)
    print("avg Ferror:",np.mean(err))
    return np.mean(err)
于 2019-11-26T17:21:38.930 回答
0

我认为您在使用 then 计算 F 之前没有删除不正确的匹配项。此外,我对如何验证 F 有一个想法,从 x'Fx=0,您可以替换公式中的几个 x' 和 x。

凯尔范

于 2016-07-04T09:14:15.487 回答