# 使用双目相机进行三维重建 第一部分：相机校准

（图 3D重建）

（图）

https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_calib3d/py_table_of_contents_calib3d/py_table_of_contents_calib3d.html

• 相机校准
• 姿态估计
• 对极几何
• 立体图像的深度图

## 设置

```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)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)

# 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')

for fname in images:
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), corners2,ret)
cv2.imshow('img',img)
cv2.waitKey(500)

cv2.destroyAllWindows()```

`ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)`

## 畸变校正

`img = cv2.imread('left12.jpg')h,  w = img.shape[:2]newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))`

```# undistortdst = cv2.undistort(img, mtx, dist, None, newcameramtx)
# crop the imagex,y,w,h = roidst = dst[y:y+h, x:x+w]cv2.imwrite('calibresult.png',dst)```

0 条评论

## 相关文章

12640

7810

### 从高通加盟小鹏汽车负责自动驾驶后，吴新宙：L3方案下月亮相

3月13日，他们又有技术高管加盟。吴新宙，前高通自动驾驶业务负责人，正式官宣加盟小鹏汽车，出任自动驾驶副总裁，全面负责小鹏汽车自动驾驶美国及国内的整体技术路线规...

12210

13530

8540

10220

10540

11310

7520

8220