flut1
flut1

Reputation: 295

Inconsistent outcome of findChessboardCorners() in opencv

I am writing C++ code with OpenCV where I'm trying to detect a chessboard on an image (loaded from a .jpg file) to warp the perspective of the image. When the chessboard is found by findChessboardCorners(), the rest of my code is working perfectly. But sometimes the function does not detect the pattern, and this behavior seems to be random.

For example, there is one image that works on it's original resolution 2560x1920, but not if I scale it down with GIMP first to 800x600. However, another image seems to do the opposite: doesn't work in original resolution, but does work scaled down.

Here's the bit of my code that does the detection:

Mat grayimg = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
if (img.data == NULL) {
    printf("Unable to read image");
    return 0;
}
bool patternfound = findChessboardCorners(grayimg, patternsize, corners,
        CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_FAST_CHECK);
if (!patternfound) {
    printf("Chessboard not found");
    return 0;
}

Is there some kind of bug in opencv causing this behavior? Does anyone has any tips on how to pre-process your image, so the function will work more consistently?

I already tried playing around with the parameters CALIB_CB_ADAPTIVE_THRESH, CALIB_CB_NORMALIZE_IMAGE, CALIB_CB_FILTER_QUADS and CALIB_CB_FAST_CHECK. I'm also having the same results when I pass in a color image.

Thanks in advance

EDIT: I'm using OpenCV version 2.4.1

Upvotes: 2

Views: 2813

Answers (4)

br1
br1

Reputation: 443

CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_FAST_CHECK is not the same as CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FAST_CHECK, you should use | (binary or)

Upvotes: 0

tomriddle_1234
tomriddle_1234

Reputation: 3213

Actually, try to remove the CALIB_CB_FAST_CHECK option, and give it a try.

Upvotes: 1

Dave Thomas
Dave Thomas

Reputation: 91

I had a very hard time getting findChessboardCorners to work until I added a white boarder around the chessboard.

I found that as hint somewhere in the more recent documenation.

Before adding the border, it would sometimes be impossible to recognize the keyboard, but with the white border it works every time.

Upvotes: 2

Francesco Callari
Francesco Callari

Reputation: 11785

Welcome to the joys of real-world computer vision :-)

You don't post any images, and findChessboardCorners is a bit too high-level to debug. I suggest to display (in octave, or matlab, or with more OpenCV code) the location of the detected corners on top of the image, to see if enough are detected. If none, try to run cvCornerHarris by itself on the image.

Sometimes the cause of the problem is the excessive graininess of the image: try to blur is just a little and see if it helps.

Upvotes: 1

Related Questions