我一直在尝试使用以下 opencv 代码来校准我的相机的内在函数。我一直收到错误,
error: (-215:Assertion failed) nimages > 0 in function 'cv::calibrateCameraRO'
。
我已经检查了图像是否正在使用 cv.imshow()
函数读取,并且我能够在代码循环时查看图像。我确实认为正在提供图像并且地址是正确的。关于问题可能是什么的任何线索?
import numpy as np
import cv2 as cv
import glob
import pickle
chessboardSize = (9,6)
frameSize = (1500,2000)
# termination criteria
criteria = (cv.TERM_CRITERIA_EPS + cv.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((chessboardSize[0] * chessboardSize[1], 3), np.float32)
objp[:,:2] = np.mgrid[0:chessboardSize[0],0:chessboardSize[1]].T.reshape(-1,2)
size_of_chessboard_squares_mm = 20
objp = objp * size_of_chessboard_squares_mm
# 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('\images\*.png')
# images = ['images\img0.png', 'images\img1.png', 'images\img10.png', 'images\img2.png', 'images\img3.png', 'images\img4.png', 'images\img5.png', 'images\img6.png', 'images\img7.png', 'images\img8.png', 'images\img9.png']
# images = ['images\img0.png', 'images\img1.png']
for image in images:
img = cv.imread(image)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv.findChessboardCorners(gray, chessboardSize, None)
# print(ret)
# print(corners)
# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)
corners2 = cv.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
imgpoints.append(corners)
# Draw and display the corners
cv.drawChessboardCorners(img, chessboardSize, corners2, ret)
cv.imshow('img', img)
cv.waitKey(1000)
cv.destroyAllWindows()
############## CALIBRATION #######################################################
ret, cameraMatrix, dist, rvecs, tvecs = cv.calibrateCamera(objpoints, imgpoints, frameSize, None, None)
事实证明我犯了一个非常愚蠢的错误。一个应该很明显但我没有太注意的。
我用于校准的棋盘有 9x6 棋盘。这意味着有 8x5 个顶点——我上面的大部分代码使用的输入。
对于我的 9x6 棋盘案例 - 只需将
chessboardSize = (9,6)
更改为 chessboardSize = (8,5)
即可。与任何其他尺寸相似。验证这一点的一个好方法是在以下代码片段中验证校准图像中的顶点是否有彩色线条。
# Draw and display the corners
cv.drawChessboardCorners(img, chessboardSize, corners2, ret)
cv.imshow('img', img)