4

我在使用 OpenCV 从 iPad 摄像头获取正确的摄像头姿势时遇到问题。

我正在使用定制的 2D 标记(基于AruCo 库) - 我想使用 OpenGL 在该标记上渲染 3D 立方体。

为了接收相机姿势,我使用了 OpenCV 的 solvePnP 函数。

根据THIS LINK我这样做是这样的:

cv::solvePnP(markerObjectPoints, imagePoints, [self currentCameraMatrix], _userDefaultsManager.distCoeffs, rvec, tvec);

tvec.at<double>(0, 0) *= -1; // I don't know why I have to do it, but translation in X axis is inverted

cv::Mat R;
cv::Rodrigues(rvec, R); // R is 3x3

R = R.t();  // rotation of inverse
tvec = -R * tvec; // translation of inverse

cv::Mat T(4, 4, R.type()); // T is 4x4
T(cv::Range(0, 3), cv::Range(0, 3)) = R * 1; // copies R into T
T(cv::Range(0, 3), cv::Range(3, 4)) = tvec * 1; // copies tvec into T
double *p = T.ptr<double>(3);
p[0] = p[1] = p[2] = 0;
p[3] = 1;

相机矩阵和 dist 系数来自findChessboardCorners函数,imagePoints是手动检测到的标记角(您可以在下面发布的视频中看到它们为绿色方块),而markerObjectPoints是表示标记角的手动硬编码点:

markerObjectPoints.push_back(cv::Point3d(-6, -6, 0));
markerObjectPoints.push_back(cv::Point3d(6, -6, 0));
markerObjectPoints.push_back(cv::Point3d(6, 6, 0));
markerObjectPoints.push_back(cv::Point3d(-6, 6, 0));

因为标记在现实世界中是 12 厘米长,所以我选择了相同的尺寸以便于调试。

结果,我收到了 4x4 矩阵 T,我将在 OpenCV 中将其用作 ModelView 矩阵。使用 GLKit 绘图功能看起来或多或少是这样的:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
    // preparations
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    float aspect = fabsf(self.bounds.size.width / self.bounds.size.height);
    effect.transform.projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(39), aspect, 0.1f, 1000.0f);

    // set modelViewMatrix
    float mat[16] = generateOpenGLMatFromFromOpenCVMat(T);
    currentModelMatrix = GLKMatrix4MakeWithArrayAndTranspose(mat);
    effect.transform.modelviewMatrix = currentModelMatrix;

    [effect prepareToDraw];

    glDrawArrays(GL_TRIANGLES, 0, 36); // draw previously prepared cube
}

我没有将所有东西都围绕 X 轴旋转 180 度(正如之前链接的文章中提到的那样),因为我看起来没有必要。

问题是它不起作用!平移向量看起来不错,但 X 和 Y 旋转搞砸了 :(

我录制了一段视频来介绍这个问题:

http://www.youtube.com/watch?v=EMNBT5H7-os

我几乎尝试了所有方法(包括将所有轴一一反转),但实际上没有任何效果。

我应该怎么办?我应该如何正确显示该 3D 立方体?来自 solvePnP 的平移/旋转向量看起来很合理,所以我想我无法正确地将这些向量映射到 OpenGL 矩阵。

4

1 回答 1

2

感谢来自http://answers.opencv.org/的 Djo1509,我发现问题是不必要的转置旋转矩阵 R 矩阵用作矩阵 T 的一部分,以及不必要的tvec = -R * tvec操作。

更多信息看那里

于 2013-10-28T23:31:07.347 回答