如何使用opencv pnpRansac()函数中的平移矩阵和旋转矩阵设置Rajawali相机的旋转?

时间:2018-05-09 07:54:17

标签: opencv rajawali

我正在使用Android AR应用。我正在使用RAJAWALI库进行AR渲染,使用openCV来检测和跟踪目标。我正在使用opencv的PNPRansac函数计算旋转和平移矩阵。

    cv::Mat rotation=cv::Mat::zeros(3, 1, CV_64FC1);
    cv::Mat translation=cv::Mat::zeros(3, 1, CV_64FC1);
    TranslationMatrix=cv::Mat::zeros(3, 1, CV_64FC1);
    RotationMatrix=cv::Mat::zeros(3, 3, CV_64FC1);
    if (!cv::solvePnPRansac(objectPoints_float,
                            scenePoints_float,
                            cameraParams.cameraMatrix,
                            cameraParams.distortionMatrix,
                            rotation,
                            translation,
                            false,
                            iterationsCount,
                            cameraParams.reprojectionError,
                            confidence)) {

        rotation.release();
        translation.release();
        return;
    }

    TranslationMatrix = translation;

    cv::Rodrigues(rotation,
                  RotationMatrix); // converts Rotation Vector to Matrix
    cv::Mat R_t, A;
    R_t = cv::Mat::zeros(3, 3, CV_64FC1);
    A = cv::Mat::zeros(3, 1, CV_64FC1);
    A.at<double>(0, 0) = 0;//[0]
    A.at<double>(1, 0) = 0;//[0]
    A.at<double>(2, 0) = 1;//[1]
    //transpose of rotation matrix
    R_t = RotationMatrix.t();

    // Reference for this formula https://en.wikipedia.org/wiki/Camera_resectioning
    cameraPosition = -1 * (R_t * TranslationMatrix);

    //CameraOrientation = R_t * A;
    //CameraOrientation = rot2euler(RotationMatrix).clone();

我正在尝试将此旋转和位置传递给Rajawali相机以实现AR体验。该位置似乎是正确的,但未正确设置旋转。 我在Rajawali场景的onRender中有这个:

    // Get camera position and rotation from C++ to Java using native method
    getCameraPositionRotation(cameraPosition.getNativeObjAddr(), cameraOrientation.getNativeObjAddr());

    double POSx = cameraPosition.get(0, 0)[0] / 1000;
    double POSy = cameraPosition.get(1, 0)[0] / 1000;
    double POSz = cameraPosition.get(2, 0)[0] / 1000;
    double ROTx = Math.toDegrees(cameraOrientation.get(0, 0)[0]);
    double ROTy = Math.toDegrees(cameraOrientation.get(1, 0)[0]);
    double ROTz = Math.toDegrees(cameraOrientation.get(2, 0)[0]);
    getCurrentCamera().setPosition(POSx, POSy, POSz);
    getCurrentCamera().setRotation(ROTx,ROTy,ROTz);

如何正确设置Rajawali相机的旋转?

0 个答案:

没有答案