Runtime Error using the cv::calibrateCamera() function

53 Views Asked by At

I am working on a project, where I want to use a camera to locate Aruco-Codes. For that I wanted to start with the calibration of my camera.

I followed the tutorial https://docs.opencv.org/4.x/da/d13/tutorial_aruco_calibration.html but unfortunatly I had to make some changes because I kept getting errors. I am working on visual studios on windows

In this line cv::calibrateCamera(allObjectPoints, allImagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, calibrationFlags) when using the function cv::calibrateCamera I get a runtime Error:


KernelBase.dll!00007ffcb706567c()   Unbekannt
    vcruntime140.dll!00007ffc96016ba7() Unbekannt
    opencv_world490.dll!cv::error(class cv::Exception const &)  C++
    opencv_world490.dll!cv::error(int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,char const *,char const *,int)    C++
    opencv_world490.dll!cv::matMulDeriv(class cv::_InputArray const &,class cv::_InputArray const &,class cv::_OutputArray const &,class cv::_OutputArray const &)  C++
    opencv_world490.dll!cv::calibrateCameraRO(class cv::_InputArray const &,class cv::_InputArray const &,class cv::Size_<int>,int,class cv::_InputOutputArray const &,class cv::_InputOutputArray const &,class cv::_OutputArray const &,class cv::_OutputArray const &,class cv::_OutputArray const &,class cv::_OutputArray const &,class cv::_OutputArray const &,class cv::_OutputArray const &,class cv::_OutputArray const &,int,class cv::TermCriteria) C++
    opencv_world490.dll!cv::calibrateCamera(class cv::_InputArray const &,class cv::_InputArray const &,class cv::Size_<int>,class cv::_InputOutputArray const &,class cv::_InputOutputArray const &,class cv::_OutputArray const &,class cv::_OutputArray const &,class cv::_OutputArray const &,class cv::_OutputArray const &,class cv::_OutputArray const &,int,class cv::TermCriteria) C++
    opencv_world490.dll!cv::calibrateCamera(class cv::_InputArray const &,class cv::_InputArray const &,class cv::Size_<int>,class cv::_InputOutputArray const &,class cv::_InputOutputArray const &,class cv::_OutputArray const &,class cv::_OutputArray const &,int,class cv::TermCriteria)  C++
>   arcuroMarker.exe!main() Zeile 97    C++
    [Externer Code] 

but I don't really know why.

Can someone help me how to get it right? Because it does detect the Ids of the charuco board correctly

So right now my code looks like this:

#include <opencv2/opencv.hpp>
#include <opencv2/calib3d.hpp>
#include "opencv2/objdetect/aruco_detector.hpp"

int main()
{
    cv::VideoCapture inputVideo;
    inputVideo.open(0);

    if (!inputVideo.isOpened())
    {
        std::cout << "Cannot open the web cam" << std::endl;
        return -1;
    }

    // Erstelle ein Charuco-Board (ersetze diese Werte durch deine spezifischen Werte)
    const cv::Size sizeCharuco(7,5);
    cv::aruco::CharucoBoard board = cv::aruco::CharucoBoard::CharucoBoard(sizeCharuco, 0.033, 0.011, cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250));

    cv::Mat img;
    cv::Size imageSize;  // Setze die Größe des Kamerabildes entsprechend deiner Kamera

    std::vector<std::vector<cv::Point2f>> allCharucoCorners;
    std::vector<std::vector<int>> allCharucoIds;
    std::vector<std::vector<cv::Point2f>> allImagePoints;
    std::vector<std::vector<cv::Point3f>> allObjectPoints;

    // Kalibrierungsflags setzen (wie in der calibrateCamera() Funktion)
    int calibrationFlags = cv::CALIB_USE_INTRINSIC_GUESS | cv::CALIB_FIX_PRINCIPAL_POINT;

    int counter = 0;

    std::vector<cv::Point3f> objPoints; // 3D-Objektpunkte für ein Charuco-Board

    for (int i = 0; i < board.getChessboardSize().height; ++i) {
        for (int j = 0; j < board.getChessboardSize().width; ++j) {
            objPoints.push_back(cv::Point3f(j * board.getSquareLength(), i * board.getSquareLength(), 0));
        }
    }

    while (counter < 100) {
        counter = counter + 1;
        inputVideo.grab();
        cv::Mat image, imgCopy;
        inputVideo.retrieve(image);
        imageSize = image.size();

        // Erkennung des Charuco-Boards
        std::vector<std::vector<cv::Point2f>> currectMarkerCorners, rejectedImg;
        std::vector<int> currentMarkerIds;

        cv::aruco::ArucoDetector detector(board.getDictionary(), cv::aruco::DetectorParameters());
        detector.detectMarkers(image, currectMarkerCorners, currentMarkerIds, rejectedImg);

        image.copyTo(imgCopy);
        if (currentMarkerIds.size() > 0) {
            
            cv::aruco::drawDetectedMarkers(imgCopy, currectMarkerCorners, currentMarkerIds);
            
            std::vector<cv::Point2f> currentCharucoCorners;
            std::vector<int> currentCharucoIds;

            std::vector<cv::Point3f> currentObjectPoints;
            std::vector<cv::Point2f> currentImagePoints;

            cv::aruco::CharucoDetector detector(board);
            detector.detectBoard(image, currentCharucoCorners, currentCharucoIds);
            
            board.matchImagePoints(currentCharucoCorners, currentCharucoIds, currentObjectPoints, currentImagePoints);
            
            if (currentCharucoIds.size() > 0) {
                cv::aruco::drawDetectedCornersCharuco(image, currentCharucoCorners, currentCharucoIds);
                // Fügen Sie currentObjectPoints und currentImagePoints zu den entsprechenden Vektoren hinzu
                allObjectPoints.push_back(objPoints);
                allImagePoints.push_back(currentCharucoCorners);

                allCharucoCorners.push_back(currentCharucoCorners);
                allCharucoIds.push_back(currentCharucoIds);
            }
        }

        cv::imshow("out", image);
        cv::imshow("out2", imgCopy);

        // Kamerakalibrierung durchführen
        char key = (char)cv::waitKey(30);
        if (key == 27)  // Escape key
            break;
    }
    cv::Mat cameraMatrix, distCoeffs;
    std::vector<cv::Mat> rvecs, tvecs;

    if (allObjectPoints.size() > 0 && allImagePoints.size() > 0) {
        std::cout << imageSize << std::endl;
        double repError = cv::calibrateCamera(allObjectPoints, allImagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, calibrationFlags);
        // ...
    }
    else {
        std::cout << "not possible to calibrate" << std::endl;
    }
    return 0;
}

I tried changing the pictures I am taking, taking it in different angeles, and taking more or less than 10 pictures.

But I don't really understand what is causing the error

0

There are 0 best solutions below