OpenCV findChessboardCorners 函数在(显然)简单的场景中失败

问题描述

我正在尝试使用 OpenCV 找到棋盘的角.

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

我使用的图像包含两个棋盘,但我只对其中一个的子区域感兴趣.下图为原图.

The image I'm using contains two chessboards, but I'm interested only in a sub-region of one of those. The following image shows the original image.

使用 GIMP,然后我选择了感兴趣的区域,并将所有其他像素设置为默认值.

Using GIMP, I've then selected the area of interest and I've set all the other pixel to a default value.

我实际上并没有裁剪图像,因为我已经使用此图像尺寸校准了相机并且我不想更改它.该操作应该等同于更改图像矩阵中的值,但我更喜欢使用 GIMP 进行操作.这是一个一次性的实验,使用图形工具而不是使用代码执行该操作更快.

I haven't actually cropped the image because I've already calibrated the camera using this image size and I didn't want to change it. The operation should be equivalent to change the values in the image matrix, but I preferred to do it with GIMP. It is a one time experiment and it is faster to do that operation with a graphic tool instead of using the code.

生成的图像包含一个 24x5 角的棋盘,但函数 findChessboardCorners 找不到任何东西.

The resulting image contains a chessboard with 24x5 corners, but the function findChessboardCorners is not able to find anything.

这是我正在使用的 Python 代码:

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 to set the adaptive threshold, but it is still not working

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

这看起来很奇怪.我过去曾多次使用过 OpenCV 的这个功能,它总是有效,即使图像看起来比这个复杂得多.复杂.该区域的照明不是均匀的,但函数应该足够强大以处理它.

That seems really strange. I used this function of OpenCV many times in the past and it always worked, even with images that looked much more complicated than this one. The illumination of the area is not homogeneous but the function should be robust enough to handle that.

使用 GIMP 临时创建的 人工 图像有什么问题吗?我怎样才能找到角落?

Is there any problem with the artificial image created ad hoc with GIMP? How can I find the corners?

任何建议将不胜感激.


解决方案

需要进行两项更改才能使该图像为非常挑剔的 cv2.findChessboardCorners 函数所接受.首先,棋盘需要白色背景.我只是通过调整图像的对比度来获得这个.其次,我还必须将连接棋盘顶部和底部黑色方块的黑色水平线涂成白色.这是生成的图像:

There are two changes needed to make that image acceptable to the very finicky cv2.findChessboardCorners function. First, the chess board needs a white background. I obtained this simply by adjusting the contrast on your image. Second, I also had to white-out the dark horizontal line that connects the black squares at the top and bottom of your chess board. This is the resulting image:

通过这些增强功能,cv2.findChessboardCorners 可以成功分析图像.结果是:

With these enhancements, 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 to how the image is enhanced can change the above results greatly. With only one image of a small chess board, these results are not to be trusted.)

如您所述,cv2.findChessboardCorners 接受旨在帮助识别棋盘的标志(自适应阈值、filter_quads 和规范化).我尝试了所有方法,但它们在这里没有任何区别.

As you noted, cv2.findChessboardCorners accepts flags (adaptive threshold, filter_quads, and normalization) that are intended to help with chess board recognition. I tried all but they made no difference here.

相关文章