cv2.calibrateCamera方法的可选参数

时间:2018-04-24 11:50:16

标签: python opencv cv2

根据文档,caliberateCamera函数的参数如下。

cv2.calibrateCamera(objectPoints, imagePoints, imageSize[, cameraMatrix[, distCoeffs[, rvecs[, tvecs[, flags[, criteria]]]]]]) → retval, cameraMatrix, distCoeffs, rvecs, tvecs

据我所知,除了objectPoints,imagePoints和imageSize之外,所有其他参数都是可选的(因为它们放在括号内)。但是当我用前3个参数调用该函数时,它会抛出错误。

cv2.calibrateCamera(objpoints, imgpoints, img_size)

如果按照以下方式运行它,它可以正常工作。

cv2.calibrateCamera(objpoints, imgpoints, img_size, None, None)

这里发生了什么。

这是我的python代码:

import numpy as np
import cv2
import glob


def caliberate_camera():
    "returns objpoints ans imgpoints"

    nx = 9
    ny = 6
    # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
    objp = np.zeros((ny*nx,3), np.float32)
    objp[:,:2] = np.mgrid[0:nx, 0:ny].T.reshape(-1,2)

    # Arrays to store object points and image points from all the images.
    objpoints = [] # 3d points in real world space
    imgpoints = [] # 2d points in image plane.

    # Make a list of calibration images
    images = glob.glob("camera_cal/calibration*.jpg")

    # Step through the list and search for chessboard corners
    for idx, fname in enumerate(images):
       img = cv2.imread(fname)
       gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

       # Find the chessboard corners
       ret, corners = cv2.findChessboardCorners(gray, (nx,ny), None)

       # If found, add object points, image points
       if ret == True:
          objpoints.append(objp)
          imgpoints.append(corners)

    img = cv2.imread('camera_cal/calibration11.jpg')
    img_size = (img.shape[1], img.shape[0])

    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size, None, None)


caliberate_camera()

此函数运行时没有任何错误。但是,如果我将caliberate_camera()函数的最后一行更改为此

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size)

我收到此错误:

  

文件" test.py",第42行,in       caliberate_camera()文件" test.py",第39行,在caliberate_camera中       ret,mtx,dist,rvecs,tvecs = cv2.calibrateCamera(objpoints,imgpoints,img_size)TypeError:必需参数' cameraMatrix' (POS   4)没找到

0 个答案:

没有答案