c++opencvvisual-studio-2017aruco

OpenCV 4.7 camera calibration with ChAruCo markers


I have trouble setting up the camera calibration features with charuco markes. I am using Win10 with VS217 and openCV 4.7 installed from the download. Genuinely, the code can use AruCo markers and set up the Charuco Board, but the calibration functions are missing.

#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; 
}

for cv::aruco::interpolateCornersCharuco I get the error

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

and for cv::aruco::calibrateCameraCharuco the error

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

Help is much appreciated!

I tried OpenCV 4.0. Could not get it working either.


Solution

  • Thanks Christoph, you guided me onto the right track. I went back to OpenCV4.0. While the marker detection has been implemented in the object detection, the functions for ChAruCo calibration are still in the aruco folder. Both (cv::aruco::interpolateCornersCharuco and cv::aruco::calibrateCameraCharuco) can made be available with #include "opencv2/aruco/charuco.hpp". one further thing. Creating the pointer to the ChAruCoboard did not work for according to the tutorial. I changed the code to:

    // Create a Charuco board

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

    // pointer to ChAruCoboard object

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

    Now it is working like a champ! cheers