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

时间:2016-03-09 22:14:06

标签: c++ opencv computer-vision camera-calibration

我正在使用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 here有类似的问题,但我找不到在C ++中测试这些答案的正确方法。 请帮我解决这个错误。

谢谢。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

谢谢大家的帮助:)

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