2017-03-09 330 views
0

我的python代码在图像上找不到棋盘。 我用这个代码来解决这个任务:opencv findChessboardCorners失败

import numpy as np 
import cv2 
import glob 

# termination criteria 
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) 

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) 
a = 7 
b = 6 
objp = np.zeros((b*a,3), np.float32) 
objp[:,:2] = np.mgrid[0:a,0:b].T.reshape(-1,2) 

# Arrays to store object points and image points from all the images. 
objpoints = [] # 3d point in real world space 
imgpoints = [] # 2d points in image plane. 

images = glob.glob('*.jpg') 

for fname in images: 
    img = cv2.imread(fname) 
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 

    # Find the chess board corners 
    ret, corners = cv2.findChessboardCorners(gray, (a,b), None) 
    print(fname, ret) 

    # If found, add object points, image points (after refining them) 
    if ret == True: 
    objpoints.append(objp) 

    cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria) 
    imgpoints.append(corners) 

    # Draw and display the corners 
    cv2.drawChessboardCorners(img, (a,b), corners, ret) 
    cv2.imshow('img',img) 
    cv2.waitKey(-1) 

cv2.destroyAllWindows() 

我用这个测试图片:

First test image Second test image

我sucsesfully使用findCirclesGrid。我做错了什么? 我试着增强我相似图片在this post

更新1

我用这样的图像和findChessboardCorners再次失败。 Another test 1 Another test 2

当我使用Harris角检测返回许多错误,这是很难用哈里斯的结果摄像机标定。

+0

可能是这个[OpenCV Birdseye视图没有数据丢失](http://stackoverflow.com/a/39316776/2521214)会有点帮助 – Spektre

回答

2

这是一个9x6板,而不是7x6板。更改a = 9。边界线并不重要。

+0

我尝试过了,它完美的作品。分配'a = 9'的任何特殊原因? –

+0

谢谢!这是完美的工作! –

1

UPDATE:

正如贵所提到的,我换成a = 9它完美地工作。

图片1:

enter image description here

图2:

enter image description here

图3:

enter image description here

在使用cv2.findChessboardCorners包含棋盘的图像必须有边框。在您提供的图像中没有边框,因此该功能失败。考虑在棋盘周围绘制黑色边框,然后执行相同的操作。

在有一个更简单的方式同时....

THIS PAGE

这给你本来可以使用Harris角点检测尝试是我作为结果得到:

enter image description here

+0

谢谢!我更新了我的帖子。 –

+0

谢谢!是工作! –

+1

我还是不明白改变'a = 9'的原因。如果你遇到这个问题,请张贴它:D –