OpenCV ProjectPoints继续绘制屏幕左上方的所有图像点

发布于 2025-01-20 12:44:42 字数 3591 浏览 1 评论 0原文

我正在尝试通过OPENCV ProjectPoints函数在校准后通过OpenCV ProjectPoints函数投射3D(X,Y,Z)轴,但是每次运行代码时,轴都会指向屏幕上的特定投影图像点。 示例输出图像

cameramatrix和distCoeffs和distCoeffs读取在校准期间写入文件的数据中。它们是:

CameraMatrix[1372.852997982289, 0, 554.2708806543288;
 0, 1372.852997982289, 906.4327368600385;
 0, 0, 1]
distCoeff[0.02839203221556521;
 0.442572399014994;
 -0.01755006951285373;
 -0.0008989327508155589;
 -1.836490953232962]

每次通过按键打开布尔时,旋转和翻译值都会通过solvepnp实时计算。其输出值的一个示例是:

R = 
 [-0.9065211432378315;
 0.3787201875924527;
 -0.2788943269946833]

T = 
 [-0.4433059282649063;
 -0.6745750872705997;
 1.13753594660495]

在计算solvePNP时,我按另一个按键按照下面的代码中写入的原点绘制3D轴。旋转和翻译值将传递到项目点函数中。但是,每个轴的axesprodentpoints图像点始终非常相似,并且在以下范围内,

[100.932, 127.418]
[55.154, 157.192]
[70.3054, 162.585]
  • 请注意,axesprojectivepoints以vector< point2f> gt; axesprojectightpoints

重新投影错误相当好,在1像素下。

项目点代码:

if (found) {
    // read calibration data -- function that reads calibration data saved to a file
    readCameraConfig(cameraMatrix, distCoeffs);

    // draw corners
    drawChessboardCorners(convertedImage, patternsize, corners, found);

    // draw 3D axes using projectPoints
    // used 0.04 because the chessboard square is 0.01778 m
    std::vector<Point3f> axis;
    axis.push_back(cv::Point3f(0.04, 0, 0));
    axis.push_back(cv::Point3f(0, 0.04, 0));
    axis.push_back(cv::Point3f(0, 0, 0.04));

    // the rotation_values and translation values are outputs from the openCV solvePnp function that is being computed separately in real-time every time i press a keypress 
    projectPoints(axis, rotation_values, translation_values, cameraMatrix, distCoeffs, axesProjectedPoints);

    cout << "image points" << endl;
    for (auto &n : axesProjectedPoints) {
        cout << n << endl;
    }

    cv::line(convertedImage, corners[0], axesProjectedPoints[0], {255,0,0}, 5);
    cv::line(convertedImage, corners[0], axesProjectedPoints[1], {0,255,0}, 5);
    cv::line(convertedImage, corners[0], axesProjectedPoints[2], {0,0,255}, 5);
}

代码的solvePNP部分:

/* calculate board's pose (rotation and translation) */
bool flag = false;
if (flag) {
    printf("Calculating board's pose (rotation and translation) ...\n");
    // read calibration data
    readCameraConfig(cameraMatrix, distCoeffs);


    // create undistorted corners or image points
    undistortPoints(corners, imagePoints, cameraMatrix, distCoeffs);


    //cout << "POINTS" << endl;
    std::vector<Point3d> objp;
    for(auto &i : points) {
        objp.push_back(i);
        //cout << i << endl;
    }

    //cout << "CORNERS" << endl;
    std::vector<Point2d> imagep;
    for(auto &j : imagePoints) {
        imagep.push_back(j);
        //cout << j << endl;
    }

    cout << "point size" << endl;
    cout << objp.size() << endl;

    // calculate pose
    solvePnP(objp, imagep, cameraMatrix, distCoeffs, rotation_values, translation_values, true, SOLVEPNP_ITERATIVE);


    // print rotation and translation values
    cout << "R = " << endl << " "  << rotation_values << endl << endl;
    cout << "T = " << endl << " "  << translation_values << endl << endl;
            
    }

    
}

I am trying to project 3D (x,y,z) axes via the openCV projectPoints function onto a chessboard after calibration, but every time I run my code, the axes all point to a specific projected image point on the screen.
example output image

The cameraMatrix and distCoeffs are read in from data that was wrote to a file during calibration. They are:

CameraMatrix[1372.852997982289, 0, 554.2708806543288;
 0, 1372.852997982289, 906.4327368600385;
 0, 0, 1]
distCoeff[0.02839203221556521;
 0.442572399014994;
 -0.01755006951285373;
 -0.0008989327508155589;
 -1.836490953232962]

The rotation and translation values are being computed in real-time via SolvePnP every time a bool is turned on via keypress. An example of a their output values are:

R = 
 [-0.9065211432378315;
 0.3787201875924527;
 -0.2788943269946833]

T = 
 [-0.4433059282649063;
 -0.6745750872705997;
 1.13753594660495]

While SolvePnP is being computed, I press another keypress to draw the 3D axes from the origin as written in the code below. And the rotation and translation values are passed into the projectPoint function. However, the axesProjectedPoints image points output for each axis is always very similar and in the range of:

[100.932, 127.418]
[55.154, 157.192]
[70.3054, 162.585]
  • Note the axesProjectedPoints is initialized out of the loop as a vector<Point2f> axesProjectedPoints

The reprojection error is fairly good, and under 1 pixel.

The projectPoints code:

if (found) {
    // read calibration data -- function that reads calibration data saved to a file
    readCameraConfig(cameraMatrix, distCoeffs);

    // draw corners
    drawChessboardCorners(convertedImage, patternsize, corners, found);

    // draw 3D axes using projectPoints
    // used 0.04 because the chessboard square is 0.01778 m
    std::vector<Point3f> axis;
    axis.push_back(cv::Point3f(0.04, 0, 0));
    axis.push_back(cv::Point3f(0, 0.04, 0));
    axis.push_back(cv::Point3f(0, 0, 0.04));

    // the rotation_values and translation values are outputs from the openCV solvePnp function that is being computed separately in real-time every time i press a keypress 
    projectPoints(axis, rotation_values, translation_values, cameraMatrix, distCoeffs, axesProjectedPoints);

    cout << "image points" << endl;
    for (auto &n : axesProjectedPoints) {
        cout << n << endl;
    }

    cv::line(convertedImage, corners[0], axesProjectedPoints[0], {255,0,0}, 5);
    cv::line(convertedImage, corners[0], axesProjectedPoints[1], {0,255,0}, 5);
    cv::line(convertedImage, corners[0], axesProjectedPoints[2], {0,0,255}, 5);
}

the solvePnP part of code:

/* calculate board's pose (rotation and translation) */
bool flag = false;
if (flag) {
    printf("Calculating board's pose (rotation and translation) ...\n");
    // read calibration data
    readCameraConfig(cameraMatrix, distCoeffs);


    // create undistorted corners or image points
    undistortPoints(corners, imagePoints, cameraMatrix, distCoeffs);


    //cout << "POINTS" << endl;
    std::vector<Point3d> objp;
    for(auto &i : points) {
        objp.push_back(i);
        //cout << i << endl;
    }

    //cout << "CORNERS" << endl;
    std::vector<Point2d> imagep;
    for(auto &j : imagePoints) {
        imagep.push_back(j);
        //cout << j << endl;
    }

    cout << "point size" << endl;
    cout << objp.size() << endl;

    // calculate pose
    solvePnP(objp, imagep, cameraMatrix, distCoeffs, rotation_values, translation_values, true, SOLVEPNP_ITERATIVE);


    // print rotation and translation values
    cout << "R = " << endl << " "  << rotation_values << endl << endl;
    cout << "T = " << endl << " "  << translation_values << endl << endl;
            
    }

    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文