Zen
Zen

Reputation: 5500

How to use findChessboardCorners and calibrateCamera?

I want to use findChessboardCorners with calibrateCamera but encounter errors when using calibrateCamera. The output is not very helpful.

OpenCV Error: Assertion failed (i < 0) in cv::_OutputArray::create

static const Size patterSize(8, 6);
auto image = imread("x.jpg", IMREAD_GRAYSCALE);

Mat corners;
auto found = findChessboardCorners(image, patterSize, corners);

//constructing objectPoints
vector<vector<Vec3f>> objectPoints;
objectPoints.push_back(vector<Vec3f>());
for (int row = 0; row < patterSize.height; row++) {
    for (int col = 0; col < patterSize.width; col++) {
        objectPoints.at(0).push_back(Vec3f(row, col, 0));
    }
}

vector<vector<Vec2f>> imagePoints;
imagePoints.push_back(vector<Vec2f>());
for (int row = 0; row < patterSize.height; row++) {
    for (int col = 0; col < patterSize.width; col++) {
        int num = row*patterSize.width + col;
        imagePoints.at(0).push_back(corners.at<Vec2f>(num, 0));
    }
}

Mat cameraMatrix, distMatrix, rvecs, tvecs;

calibrateCamera(objectPoints, imagePoints, Size(image.size().width, image.size().height), cameraMatrix, distMatrix, rvecs, tvecs);

Upvotes: 1

Views: 997

Answers (1)

Zen
Zen

Reputation: 5500

The decleration of rvecs, tvecs should be vector<Mat> rather than Mat, and then everything will be fine. For more information, visit 3calibration.cpp.

Upvotes: 1

Related Questions