双目相机进行双目标定是绝对路径然后极线校正然后绘制出极线python代码
时间: 2023-07-23 21:40:09 浏览: 200
以下是双目相机进行双目标定、绝对路径、极线校正、绘制极线的 Python 代码:
```python
import numpy as np
import cv2
# 定义标定板的大小和格子数
pattern_size = (8, 6)
square_size = 20
# 读取左右相机的图像
img_left = cv2.imread('left.png')
img_right = cv2.imread('right.png')
# 导入标定板的世界坐标和图像坐标
obj_points = []
img_points_left = []
img_points_right = []
objp = np.zeros((pattern_size[0]*pattern_size[1], 3), np.float32)
objp[:, :2] = np.mgrid[0:pattern_size[0], 0:pattern_size[1]].T.reshape(-1, 2) * square_size
for i in range(2):
if i == 0:
img = img_left
else:
img = img_right
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, pattern_size, None)
if ret:
obj_points.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
if i == 0:
img_points_left.append(corners2)
else:
img_points_right.append(corners2)
# 对左右相机进行标定
ret_left, mtx_left, dist_left, rvecs_left, tvecs_left = cv2.calibrateCamera(obj_points, img_points_left, gray.shape[::-1], None, None)
ret_right, mtx_right, dist_right, rvecs_right, tvecs_right = cv2.calibrateCamera(obj_points, img_points_right, gray.shape[::-1], None, None)
# 进行双目标定
flags = cv2.CALIB_FIX_INTRINSIC
criteria_stereo = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, 100, 1e-5)
ret, mtx_left, dist_left, mtx_right, dist_right, R, T, E, F = cv2.stereoCalibrate(obj_points, img_points_left, img_points_right, mtx_left, dist_left, mtx_right, dist_right, gray.shape[::-1], criteria=criteria_stereo, flags=flags)
# 计算校正映射和立体校正
R_left, R_right, P_left, P_right, Q, valid_roi_left, valid_roi_right = cv2.stereoRectify(mtx_left, dist_left, mtx_right, dist_right, gray.shape[::-1], R, T)
# 计算映射表
map_left_x, map_left_y = cv2.initUndistortRectifyMap(mtx_left, dist_left, R_left, P_left, gray.shape[::-1], cv2.CV_16SC2)
map_right_x, map_right_y = cv2.initUndistortRectifyMap(mtx_right, dist_right, R_right, P_right, gray.shape[::-1], cv2.CV_16SC2)
# 映射图像
img_left_rectified = cv2.remap(img_left, map_left_x, map_left_y, cv2.INTER_LINEAR)
img_right_rectified = cv2.remap(img_right, map_right_x, map_right_y, cv2.INTER_LINEAR)
# 计算视差图
window_size = 5
min_disp = 16
num_disp = 112 - min_disp
stereo = cv2.StereoSGBM_create(minDisparity=min_disp, numDisparities=num_disp, blockSize=window_size, P1=8*3*window_size**2, P2=32*3*window_size**2)
disparity = stereo.compute(img_left_rectified, img_right_rectified)
# 极线校正和绘制极线
lines_left = cv2.computeCorrespondEpilines(img_points_left, 1, F)
lines_left = lines_left.reshape(-1, 3)
img_left_rectified_color = cv2.cvtColor(img_left_rectified, cv2.COLOR_GRAY2BGR)
for r, pt in zip(lines_left, img_points_right):
color = tuple(np.random.randint(0, 255, 3).tolist())
x0, y0 = map(int, [0, -r[2]/r[1]])
x1, y1 = map(int, [img_left_rectified_color.shape[1], -(r[2]+r[0]*img_left_rectified_color.shape[1])/r[1]])
img_left_rectified_color = cv2.line(img_left_rectified_color, (x0, y0), (x1, y1), color, 1)
cv2.imshow('Left Rectified', img_left_rectified_color)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文