Opencv python 相机校准:objp 矩阵
Opencv python camera calibration : objp matrix
这是 opencv python 文档中的相机校准代码。我想知道 objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2) 是如何工作的。 reshape (-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
此外,objpoints 是 3D 真实世界坐标。这应该是手动测量吧?为什么我们从 (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) 分配点?
谢谢。任何帮助将不胜感激。
您应该 post 错误代码和异常,以便我们帮助您修复它。
-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现实世界坐标,但是长度单位是任意的,所以如果所有的box的边长都一样,就不需要手动测量了。如果边的长度是16cm,那么objp
中的一个就是16cm。
这是 opencv python 文档中的相机校准代码。我想知道 objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2) 是如何工作的。 reshape (-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
此外,objpoints 是 3D 真实世界坐标。这应该是手动测量吧?为什么我们从 (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) 分配点? 谢谢。任何帮助将不胜感激。
您应该 post 错误代码和异常,以便我们帮助您修复它。
-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现实世界坐标,但是长度单位是任意的,所以如果所有的box的边长都一样,就不需要手动测量了。如果边的长度是16cm,那么objp
中的一个就是16cm。