我在树莓派3上运行python 3并安装了opencv。我拍了10张棋盘图片,它检测到所有10张图片并显示它们,但是当它到达最后一行时,它会抛出一个错误。这是我使用的图片:https://imgur.com/gallery/IDfHH这是我的代码:
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)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].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:
print('test')
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (6,9),None)
# If found, add object points, image points (after refining them)
if ret == True:
print('test2')
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
imgpoints.append(corners2)
# Draw and display the corners
img = cv2.drawChessboardCorners(img, (6,9), corners2,ret)
cv2.imshow('img',img)
cv2.waitKey(500)
print('test3')
cv2.destroyAllWindows()
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
答案 0 :(得分:1)
该示例假设您有一个6x7的棋盘图像,我认为您有6x9。 你必须为6x9校准图像准备objp变量,所以代码必须是这样的:objp = np.zeros(( 6 * 9 ,3),np.float32)
代码:
objp = np.zeros((6*9,3), np.float32)
答案 1 :(得分:0)
感谢@Rui Sebastiao。
我使用的是14 x 10,所以我更改了以下几行,至少没有错误:)
objp = np.zeros((14*10, 3), np.float32)
objp[:, :2] = np.mgrid[0:14, 0:10].T.reshape(-1, 2)