findChessboardCorners fails for calibration image

laurenelizabeth picture laurenelizabeth · Jul 16, 2013 · Viewed 25.8k times · Source

I am trying to get OpenCV 2.4.5 to recognize a checkerboard pattern from my webcam. I couldn't get that working, so I decided to try to get it working just using a "perfect" image:

checkerboard with white border

but it still won't work--patternFound returns false every time. Does anyone have any idea what I'm doing wrong?

#include <stdio.h>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main(){
    Size patternsize(8,8); //number of centers
    Mat frame = imread("perfect.png"); //source image
    vector<Point2f> centers; //this will be filled by the detected centers

    bool patternfound = findChessboardCorners(frame,patternsize,centers);

    cout<<patternfound<<endl;
    drawChessboardCorners(frame, patternsize, Mat(centers), patternfound);

    cvNamedWindow("window");
    while(1){
        imshow("window",frame);
        cvWaitKey(33);
    }
}

Answer

laurenelizabeth picture laurenelizabeth · Jul 16, 2013

Through trial and error, I realized that patternsize should be 7x7 since it is counting internal corners. This parameter has to be exact--8x8 won't work, but neither will anything less than 7x7.