【问题标题】:Opencv python camera calibration : objp matrixOpencv python相机校准:objp矩阵
【发布时间】:2015-03-16 03:13:07
【问题描述】:

这是 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

此外,objpoints 是 3D 真实世界坐标。这应该是手动测量的吧?为什么我们从 (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) 分配点? 谢谢。任何帮助将不胜感激。

【问题讨论】:

    标签: python opencv numpy camera-calibration


    【解决方案1】:

    您应该发布错误代码和异常,以便我们帮助您修复它。

    -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 真实世界坐标,但长度单位是任意的,所以如果所有框的边长相同,则无需手动测量。如果边长为16cm,那么objp中的1表示16cm。

    【讨论】:

      猜你喜欢
      • 2016-12-29
      • 2020-01-25
      • 2018-10-19
      • 2013-10-07
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 2014-10-23
      相关资源
      最近更新 更多