这是opencv python文档中的相机校准代码。我想知道 objp[:,:2] = np.mgrid[0:7,0:6] 如何。T.reshape(-1,2) 工作。重塑 (-1,2) 有什么作用?我试图更改这行代码中的值,但出现错误。有人可以解释一下这是如何工作的,为什么只有这些数字会起作用吗?
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((6*7,3), np.float32)
print "objp: ",objp
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)
print "objp: ",objp
# 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('left*.jpg')
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (7,6),None)
# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)
cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
imgpoints.append(corners)
# Draw and display the corners
cv2.drawChessboardCorners(img, (7,6), corners,ret)
cv2.imshow('img',img)
cv2.waitKey(500)
cv2.destroyAllWindows()
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None) # camera matrix, distortion coefficients, rotation and translation vectors
此外,目标点是 3D 真实世界的坐标。这应该手动测量吧?为什么我们要从 (0,0,0)、(1,0,0)、(2,0,0) ....,(6,5,0)??谢谢。任何帮助将不胜感激。
您应该发布错误代码和异常,以便我们帮助您修复它。
-1 平均值根据总元素数计算实际长度:
np.mgrid[0:7,0:6].T.reshape(-1,2)
您可以按如下方式拆分代码:
a = np.mgrid[0:7, 0:6]
b = a.T
c = b.reshape(-1, 2)
print a.shape, b.shape, c.shape
输出为:
(2, 7, 6) (6, 7, 2) (42, 2)
如果难以理解代码,则与:
x, y = np.mgrid[0:7, 0:6]
np.c_[x.ravel(), y.ravel()]
objpoints是3D真实世界的坐标,但长度单位是任意的,因此如果所有框都具有相同的边长度,则无需手动测量它。如果边缘的长度为 16 厘米,则 objp
英寸表示 16 厘米。