2017-03-09 307 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 –