微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Aruco奇怪的Z轴绘制

如何解决Aruco奇怪的Z轴绘制

我一直在尝试解决此问题,但是我找不到问题所在。 Image is here!不管标记相对于相机成什么角度,Z轴始终从左上角开始。

我主要遵循here提供的代码,直到姿势估计都很好为止。然后,我发现 another post提到了错误的校准,因此我废弃了之前的相机校准代码,并使用了OpenCV lib随附的示例代码。我使用ChArUco板进行了校准,并获得了相似的cameraMatrix参数,但仍然无法解决问题。

我尝试运行在OpenCV库中找到的example_aruco_detect_markers.exe,并使用charuco校准参数,它运行良好。有人可以指出错误吗?

谢谢!

cameraMatrix and distCoefficients values from charuco calibration:
3
3
6.1281775951664292e+02
0
3.2562786642941285e+02
0
6.1063209706919008e+02
2.4533932079963614e+02
0
0
1
5
1
8.3874469408101907e-02
-1.2335583906210612e-02
-3.5221660057632941e-03
1.7090716825533996e-03
-8.5416258423982583e-01

#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/aruco.hpp>
#include <opencv2/aruco/charuco.hpp>

#include <sstream>
#include <iostream>
#include <fstream>

using namespace cv;
using namespace std;

const float arucoSize = 0.13129f; //12.90mm

bool loadCameraCalibration(string name,Mat cameraMatrix,Mat distCoefficients)
{
    ifstream inStream(name);
    if (inStream)
    {
        //cameraMatrix
        uint16_t rows;
        uint16_t columns;

        inStream >> rows;
        inStream >> columns;

        cameraMatrix = Mat(Size(rows,columns),CV_64F);

        for (int r = 0; r < rows; r++)
        {
            for (int c = 0; c < columns; c++)
            {
                double read = 0.0f;
                inStream >> read;
                cameraMatrix.at<double>(r,c) = read;
                cout << cameraMatrix.at<double>(r,c) << "\n";
            }
        }

        //distCoefficients
        inStream >> rows;
        inStream >> columns;

        distCoefficients = Mat::zeros(rows,columns,CV_64F);

        for (int r = 0; r < rows; r++)
        {
            for (int c = 0; c < columns; c++)
            {
                double read = 0.0f;
                inStream >> read;
                distCoefficients.at<double>(r,c) = read;
                cout << distCoefficients.at<double>(r,c) << "\n";
            }
        }

        inStream.close();
        return true;
    }
    return false;
}

int startWebcamMonitoring(Mat &cameraMatrix,Mat &distCoefficients,float arucoSquareDimension)
{
    Mat frame;

    vector<int> markerIds;
    vector<vector<Point2f>> markerCorners,rejectedCandidates;
    vector<Vec3d> rotationVectors,translationVectors;
    aruco::DetectorParameters parameters;

    Ptr<aruco::Dictionary> markerDictionary = aruco::getPredefinedDictionary(aruco::PredefineD_DICTIONARY_NAME::DICT_4X4_50);

    //show video with aruco
    VideoCapture vid(0);

    if (!vid.isOpened())
    {
        cout << "error: Webcam connect unsuccessful\n";
        return -1;
    }

    namedWindow("Webcam",WINDOW_AUTOSIZE);

    while (true)
    {
        if (!vid.read(frame))
            break;

        //cvtColor(frame,frame,COLOR_BGR2GRAY);

        aruco::detectMarkers(frame,markerDictionary,markerCorners,markerIds);
        aruco::estimatePoseSingleMarkers(markerCorners,arucoSquareDimension,cameraMatrix,distCoefficients,rotationVectors,translationVectors);

        for (int i = 0; i < markerIds.size(); i++)
        {
            aruco::drawAxis(frame,rotationVectors[i],translationVectors[i],0.1f);
            aruco::drawDetectedMarkers(frame,markerIds);

            cout << translationVectors[i] << "; " << rotationVectors[i] << endl;
        }

        imshow("Webcam",frame);
        if (waitKey(30) >= 0) break;
    }
    return 1;
}

int main(int argv,char** argc)
{
    Mat cameraMatrix = Mat::eye(3,3,CV_64F);
    Mat distCoefficients;

    loadCameraCalibration("Lenovo-charuco",distCoefficients);
    startWebcamMonitoring(cameraMatrix,arucoSize);

    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。