2017-02-06 63 views
0

我正在使用opencv python進行攝像機標定。我已經找到了相機矩陣使用cv2.calibratecamera功能,但攝像頭矩陣是不同勢對不同勢圖像 這就是我using-使用opencv進行攝像機標定

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() 

回答