The OpenCV findChessboardCorners function does not work in a (apparently) simple script

I am trying to find the corners of a chessboard using OpenCV.

The image I use contains two chessboards, but I'm only interested in the subregion of one of them. The following image shows the original image.

Original image

Using GIMP, I selected the area of โ€‹โ€‹interest, and I set all other pixels to the default value.

Cropped image

I actually did not crop the image because I already calibrated the camera using this image size and I did not want to change it. The operation should be equivalent to changing the values โ€‹โ€‹in the image matrix, but I preferred to do this using GIMP. This is a one-time experiment, and itโ€™s faster to use this operation with a graphical tool instead of using code.

The resulting image contains a chessboard with angles of 24x5, but the findChessboardCorners function cannot find anything.

Here is the Python code I'm using:

>>> img = cv2.imread('C:\\Path\\To\\C4-Cropped.png', 0) >>> cv2.findChessboardCorners(img, (24, 5)) (False, None) >>> cv2.findChessboardCorners(img, (5, 24)) (False, None) 

I also tried setting the adaptive threshold, but it still does not work

 >>> cv2.findChessboardCorners(img, (24, 5), flags=cv2.cv.CV_CALIB_CB_ADAPTIVE_THRESH) (False, None) 

It seems really weird. I have used this OpenCV feature many times in the past and have always worked, even with images that looked much more complicated than this. The coverage of the area is not uniform, but the function must be strong enough to handle it.

Is there a problem with the artificial image created by ad hoc with GIMP? How to find the corners?

Any suggestion would be greatly appreciated.

+5
source share
5 answers

Two changes need to be made to make this image acceptable for the very finicky cv2.findChessboardCorners function. Firstly, a chessboard needs a white background. I just got it by adjusting the contrast on your image. Secondly, I also had to shade the dark horizontal line connecting the black squares above and below your chessboard. This is the resulting image: enter image description here

Thanks to these improvements, cv2.findChessboardCorners can successfully analyze the image. The results were:

 camera matrix = [[ 1.67e+04 0.00e+00 1.02e+03] [ 0.00e+00 1.70e+04 5.45e+02] [ 0.00e+00 0.00e+00 1.00e+00]] distortion coefficients = [ -4.28e+00 1.38e+03 -8.59e-03 -1.49e-02 6.93e+00] 

(Small changes in how the image improves can significantly change the above results. With just one image of a small checkerboard, these results cannot be trusted.)

As you noticed, cv2.findChessboardCorners accepts flags (adaptive threshold, filter_quads and normalization), which are designed to help in recognizing a chessboard. I tried everything, but they did not matter here.

+7
source

I bet you $ 5 that the threshold image inside findChessboardCorners produces garbage due to the background in the masked image.

I recommend making a crop, removing a chessboard, and then compensating for the coordinates of the angles found by the trim position.

+6
source

Add a blank space around the checkerboard pattern. "Note: the function requires white space (for example, the width of the square thickness, the wider the better) around the board to make detection more reliable in different conditions. Otherwise, if there is no border and the background is dark, the outer black squares cannot be are segmented properly, and therefore the grouping and ordering of squares algorithm is not performed. "

+4
source

I am working on almost the same problem, but in C ++. The findChessboardCorners function findChessboardCorners not always recognize a chessboard with a given size, and seeing that your image does not extend to a chessboard, I think it matters. My suggestion is to reduce the size of your chessboard until you find something. !!

+1
source

The detectCheckerboardPoints function in the System Vision System Toolbox for Matlab detects it, but it gives an additional column:

Result of detectCheckerboardPoints from Matlab

You should be able to get rid of the extra column on the right, but by moving the right edge of the selected region a little to the left.

+1
source

Source: https://habr.com/ru/post/1494721/


All Articles