带有 ChAruCo 标记的 OpenCV 4.7 相机校准

问题描述 投票:0回答:1

我在使用 charuco 标记设置相机校准功能时遇到了问题。 我正在使用带有 VS217 的 Win10 和从下载安装的 openCV 4.7。真的,代码可以使用AruCo标记并设置Charuco Board,但是缺少校准功能。

#include <iostream>
#include <fstream>
#include <string>
#include <dirent.h>
#include <opencv2/opencv.hpp> 

const std::string in_path = "E:/CameraCalibration/images_original/";
const std::string out_path = "E:/CameraCalibration/images_calibrated/";

int main(int argc, char\*\* argv) {

// Create a Charuco board with 6x9 squares and 20x20 pixel squares  
cv::aruco::Dictionary AruCoDict =
 cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250); 
cv::aruco::CharucoBoard ChAruCoboard(cv::Size(5, 7), 0.04f, 0.02f, AruCoDict); 

// Create aruco marker detector  
cv::aruco::DetectorParameters detectorParams = cv::aruco::DetectorParameters(); 
cv::aruco::ArucoDetector detector(AruCoDict, detectorParams); 

// Define the camera calibration parameters  
std::vector<std::vector<cv::Point2f>> allCharucoCorners; 
std::vector<std::vector<int>> allCharucoIds; 
std::vector<cv::Mat> allImages; 
cv::Size imageSize; 
cv::Mat cameraMatrix, distCoeffs; 
std::vector<cv::Mat> rvecs, tvecs; 

// Detect Charuco markers in each image and add them to the calibration data  
for (int i = 1; i <= 3; i++) { 
    // Load the image  
    cv::Mat image = cv::imread(in_path + cv::format("image%d.bmp", i)); 
    std::cout << cv::format("image%d.bmp") << std::endl; 
    allImages.push_back(image); 

    // Detect markers in the image  
    std::vector<int> ids; 
    std::vector<std::vector<cv::Point2f>> corners, corners_rejected; 
    detector.detectMarkers(image, corners, ids, corners_rejected); 

    // Identify Charuco markers in the image  
    if (ids.size() > 0) { 
        std::vector<cv::Point2f> charucoCorners; 
        std::vector<int> charucoIds; 
        cv::aruco::interpolateCornersCharuco(corners, ids, image, &ChAruCoboard, 
 charucoCorners, charucoIds); 

        // Add the Charuco markers to the calibration data  
        if (charucoIds.size() > 0) { 
            allCharucoCorners.push_back(charucoCorners); 
            allCharucoIds.push_back(charucoIds); 
            imageSize = image.size(); 
        } 
    } 
} 
// Calibrate the camera using the Charuco markers  
double repError = cv::aruco::calibrateCameraCharuco(allCharucoCorners,
 allCharucoIds, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs,
calibrationFlags);

// Print the calibration results  
std::cout << "Camera matrix:\n" << cameraMatrix << "\n\n"; 
std::cout << "Distortion coefficients:\n" << distCoeffs << "\n\n"; 
std::cout << "Rotation vectors:\n";  
for (const auto& rvec : rvecs) { std::cout << rvec << "\n"; } std::cout << "\n\n"; 
std::cout << "Translation vectors:\n";     
for (const auto& tvec : tvecs) { std::cout << tvec << "\n"; } std::cout << "\n\n"; 
std::cout << "Reprojection error: " << repError << "\n"; 

return 0; 
}

对于

cv::aruco::interpolateCornersCharuco
我得到错误

namespace "cv::aruco" has no member "interpolateCornersCharuco"

cv::aruco::calibrateCameraCharuco
错误

namespace "cv::aruco" has no member "calibrateCameraCharuco"

非常感谢帮助!

我试过 OpenCV 4.0。也无法正常工作。

c++ opencv visual-studio-2017 aruco
1个回答
0
投票

谢谢 Christoph,你引导我走上了正确的轨道。我回到了OpenCV4.0。虽然在目标检测中实现了marker检测,但ChAruCo标定的函数仍在aruco文件夹中。 (cv::aruco::interpolateCornersCharuco 和 cv::aruco::calibrateCameraCharuco) 都可以通过 #include "opencv2/aruco/charuco.hpp" 使用。 还有一件事。根据教程,创建指向 ChAruCoboard 的指针不起作用。我将代码更改为:

// 创建一个 Charuco 板

cv::aruco::Dictionary AruCoDict = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_250);

// 指向 ChAruCoboard 对象的指针

cv::Ptr<cv::aruco::CharucoBoard> ptrChAruCoboard = new cv::aruco::CharucoBoard(cv::Size(8, 6), 0.005f, 0.003f, AruCoDict);

现在它像冠军一样工作!干杯

© www.soinside.com 2019 - 2024. All rights reserved.