使用 C++ 进行 OpenCv 相机校准(不支持的格式或格式组合错误)

OpenCv camera calibration with C++ (Unsupported format or combination of formats error)

我正在使用 OpenCV 使用 C++ 进行相机校准。问题是对于 calibrateCamera() 函数我得到这个错误:

OpenCV Error: Unsupported format or combination of formats (objectPoints should contain vector of vectors of points of type Point3f) in cv::collectCalibrationData, file C:\builds\master_PackSlave-win64-vc12-shared\opencv\modules\calib3d\src\calibration.cpp, line 3060

这是我的代码:

template <typename T>
string NumberToString ( T Number )
{
    stringstream ss;
    ss << Number;
    return ss.str();
}

int main()
{
    int numBoards = 20;
    int numCornersHor = 8;
    int numCornersVer = 5;
    float squareLenght = 30.0;

    string commonFileName = "D:\IMG_11"; 
    string fileType = ".JPG";
    int fileNum = 7; 
    string fileNumStr;
    Size ImgSize(816, 612); 

    int numSquares = numCornersHor * numCornersVer;
    Size board_sz = Size(numCornersHor, numCornersVer);

    vector<vector<Vec3f>>object_points;
    vector<vector<Vec2f>>image_points;

    vector<Vec3f>obj;
    vector<Vec2f>corners;

    Mat cameraMatrix, distCoeffs;
    Size imageSize;

    int successes=0;

    Mat image;
    Mat gray_image;

    fileNumStr = NumberToString(fileNum);
    if (fileNum <10)
        fileNumStr = "0"+ NumberToString(fileNum);

    image = imread(commonFileName+fileNumStr+fileType,1);
    resize(image,image, ImgSize);
    fileNum++;

    obj.clear();
    for(int i = 0; i<numCornersVer; i++){
        for(int j=0;j<numCornersHor;j++){
            obj.push_back(Vec3f(float(j*squareLenght/1.0),float(i*squareLenght/1.0),0.0f));
        }
    }

    while(successes<numBoards)
    {
        cvtColor(image, gray_image, CV_BGR2GRAY);

        bool found = findChessboardCorners(image, board_sz, corners, CV_CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_NORMALIZE_IMAGE);

        if(found)
        {            
            cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS+TermCriteria::COUNT, 30, 0.1));
            drawChessboardCorners(image, board_sz, Mat(corners), found);

        image_points.push_back(corners);
            object_points.push_back(obj);

            successes++;

            if(successes>=numBoards)
                break;
        }

        //imshow("win1 "+ fileNumStr, image);
        //imshow("win2 " + fileNumStr, gray_image);

        fileNumStr = NumberToString(fileNum);
        if (fileNum <10)
            fileNumStr = "0"+ NumberToString(fileNum); 
        image = imread(commonFileName+fileNumStr+fileType,1);
        resize(image,image, ImgSize);
        fileNum++;

        int key = waitKey(1);

        if(key==27)
            return 0;
    }

    vector<Mat> rvecs;
    vector<Mat> tvecs;

    cameraMatrix = Mat::eye(3, 3, CV_64F);
    distCoeffs = Mat::zeros(8, 1, CV_64F);

    intrinsic.ptr<float>(0)[0] = 1;
    intrinsic.ptr<float>(1)[1] = 1;


    double rms = calibrateCamera(object_points, image_points, image.size(), cameraMatrix, distCoeffs, rvecs, tvecs);

    Mat imageUndistorted;

    return 0;
}

我发现另一个人对 python 也有类似的问题,但我找不到在 C++ 中测试这些答案的正确方法。 请帮我解决这个错误。

谢谢。

尝试将 object_points 转换为 Point3f 向量的向量,并将 image_points 转换为 Point2f 向量的向量,您粘贴的错误暗示了这一点。在您的代码中,您使用的是 Vec3f 和 Vec2f 而不是 Point3f/2f

谢谢大家对我的帮助:)

使用cmake再次安装opencv解决了这个问题。