我正在尝试使用棋board方法在python中校准相机。
这是我正在使用的代码:
import numpy as np
import cv2
import glob
x = 3
y = 3
# 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((y*x,3), np.float32)
objp[:,:2] = np.mgrid[0:x,0:y].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, (x,y),None)
# If found, add object points, image points (after refining them)
if ret == True:
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, (x,y), corners2,ret)
cv2.imshow('img', img)
cv2.waitKey(500)
cv2.destroyAllWindows()
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
img = cv2.imread('pic0.jpg')
h, w = img.shape[:2]
newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))
# undistort
mapx,mapy = cv2.initUndistortRectifyMap(mtx,dist,None,newcameramtx,(w,h),5)
dst = cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
# crop the image
x,y,w,h = roi
dst = dst[y:y+h, x:x+w]
cv2.imwrite('calibresult.png',dst)
x和y用于图案的大小。
这是我正在使用的图像。
findChessboardCorners方法似乎无法找到尺寸超过3x3的棋盘图案。我已经尝试过对图像进行二值化并增加对比度,但是我无法获取更大的图案。
我正在处理的图像是否太差或我做错了什么?
最佳答案
正如Nullman所说,您将棋盘内角的大小定义为3x3。在您提供的示例图像中,内角尺寸为14x6。因此,代码将是:
ret, corners = cv2.findChessboardCorners(gray, (14,6),None)
关于python - findChessboardCorners无法找到超过3x3的棋盘,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59251019/