使用OpenCV进行相机校准



我正在使用OpenCV Python进行相机校准。我找到了使用 cv2.calibratecamera 函数的相机矩阵,但不同图像的相机矩阵不同这是我正在使用的代码-

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((7 * 7, 3), np.float32)
objp[:, :2] = np.mgrid[0:7, 0:7].T.reshape(-1, 2)
#objp = objp*(2.4625)
# 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')
print('hello')
for r12 in images:
    img = cv2.imread('r12.jpg')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (7, 7), None)
    # If found, add object points, image points (after refining them)
    if ret:
        objpoints.append(objp)
        print('yes')
        cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
        imgpoints.append(corners)
        # Draw and display the corners
        cv2.drawChessboardCorners(img, (7, 7), corners, ret)
        cv2.imshow('img', img)
        cv2.waitKey(500)
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
#np.savez('coefficientsr',cameramatrixr = mtx,distcoeffsr = dist,rotationalvectorsr = rvecs,translationalvectorsr = tvecs)
print(mtx)
print(dist)
cv2.destroyAllWindows()

您可以尝试获取重新投影误差,以查看每次校准的参数的准确性。还要确保使用足够的图像并包括对象的不同角度。

本教程演示如何计算重投影误差:http://docs.opencv.org/3.1.0/dc/dbb/tutorial_py_calibration.html

相关内容

  • 没有找到相关文章

最新更新