棋盘进给图像的OpenCV摄像机标定误差

hkmswyz6  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(168)

我一直在尝试用下面的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)
xoshrz7s

xoshrz7s1#

结果发现我犯了一个非常愚蠢的错误。一个应该很明显的,但我没有注意。
我用来校准的棋盘有9 x6个棋盘。这意味着有8x 5顶点**-我上面的大多数代码使用的输入。
对于我的9 x6棋盘,简单地将chessboardSize = (9,6)更改为chessboardSize = (8,5)就可以了。类似于任何其他尺寸。验证这一点的一个好方法是在下面的代码片段中验证校准图像中顶点是否有彩色线。

# Draw and display the corners
cv.drawChessboardCorners(img, chessboardSize, corners2, ret)
cv.imshow('img', img)

相关问题