opencv findChessboardCorners cannot find corners

293 Views Asked by At

I am using Python3.10 and Opencv 4.7.

This is my code:

img = cv2.imread('1_1691050309204.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (5, 6))
print(ret)
print(corners)

returns False(ret) and None(corners).

What's wrong with the code or the image?

And attach the chessboard image.
enter image description here

2

There are 2 best solutions below

2
diyImma On

It seems that it worked once I resized the image.

import cv2

img = cv2.imread(r'd:\chess.jpg')
img = cv2.resize(img, (int(img.shape[1]/2), int(img.shape[0]/2)))
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (5, 6))

fnl = cv2.drawChessboardCorners(img, (5, 6), corners, ret)
cv2.imshow("fnl", fnl)
cv2.waitKey(0)
0
xuanzhui On

I give up on findChessboardCorners, while its analog findChessboardCornersSB seems to be a good choice.

According to the doc, findChessboardCornersSB is more robust to all sort of noise, faster on larger images and is able to directly return the sub-pixel position of the internal chessboard corners.

So changing the code to this way, corners are returned:

flags = cv2.CALIB_CB_EXHAUSTIVE + cv2.CALIB_CB_ACCURACY
ret, corners = cv2.findChessboardCornersSB(gray, (5, 6), flags=flags)