def set_alpha(self, a):
"""
Set the alpha value for the calibrated camera solution. The
alpha value is a zoom, and ranges from 0 (zoomed in, all pixels
in calibrated image are valid) to 1 (zoomed out, all pixels in
original image are in calibrated image).
"""
cv2.stereoRectify(self.l.intrinsics,
self.l.distortion,
self.r.intrinsics,
self.r.distortion,
self.size,
self.R,
self.T,
self.l.R, self.r.R, self.l.P, self.r.P,
alpha = a)
cv2.initUndistortRectifyMap(self.l.intrinsics, self.l.distortion, self.l.R, self.l.P, self.size, cv2.CV_32FC1,
self.l.mapx, self.l.mapy)
cv2.initUndistortRectifyMap(self.r.intrinsics, self.r.distortion, self.r.R, self.r.P, self.size, cv2.CV_32FC1,
self.r.mapx, self.r.mapy)
python类stereoRectify()的实例源码
def compute_stereo_rectification_maps(stereo_rig, im_size, size_factor):
new_size = (int(im_size[1] * size_factor), int(im_size[0] * size_factor))
rotation1, rotation2, pose1, pose2 = \
cv2.stereoRectify(cameraMatrix1=stereo_rig.cameras[0].intrinsics.intrinsic_mat,
distCoeffs1=stereo_rig.cameras[0].intrinsics.distortion_coeffs,
cameraMatrix2=stereo_rig.cameras[1].intrinsics.intrinsic_mat,
distCoeffs2=stereo_rig.cameras[1].intrinsics.distortion_coeffs,
imageSize=(im_size[1], im_size[0]),
R=stereo_rig.cameras[1].extrinsics.rotation,
T=stereo_rig.cameras[1].extrinsics.translation,
flags=cv2.CALIB_ZERO_DISPARITY,
newImageSize=new_size
)[0:4]
map1x, map1y = cv2.initUndistortRectifyMap(stereo_rig.cameras[0].intrinsics.intrinsic_mat,
stereo_rig.cameras[0].intrinsics.distortion_coeffs,
rotation1, pose1, new_size, cv2.CV_32FC1)
map2x, map2y = cv2.initUndistortRectifyMap(stereo_rig.cameras[1].intrinsics.intrinsic_mat,
stereo_rig.cameras[1].intrinsics.distortion_coeffs,
rotation2, pose2, new_size, cv2.CV_32FC1)
return map1x, map1y, map2x, map2y
def rectify(mtx1, dist1, mtx2, dist2, R, T):
# R????????????P?3*4????????Q?4*4??????
R1, R2, P1, P2, Q, roi1, roi2 = cv2.stereoRectify(
mtx1, dist1,
mtx2, dist2,
(BINIMG_W, BINIMG_H),
R,
T,
flags=cv2.CALIB_ZERO_DISPARITY,
alpha=-1,
newImageSize=(BINIMG_W, BINIMG_H)
)
if __name__ == '__main__':
printMat(R1, R2, P1, P2, Q, roi1, roi2)
# ??????????????mapx, mapy?
mapx1, mapy1 = cv2.initUndistortRectifyMap(
mtx1, dist1,
R1, P1,
(BINIMG_W, BINIMG_H),
cv2.CV_16SC2
)
mapx2, mapy2 = cv2.initUndistortRectifyMap(
mtx2, dist2,
R2, P2,
(BINIMG_W, BINIMG_H),
cv2.CV_16SC2
)
return mapx1, mapy1, mapx2, mapy2, Q, roi1, roi2
def compute_rectification(ms_rig):
"""
:type ms_rig: MultiStereoRig
:param ms_rig:
:return:
"""
for stereo_rig in ms_rig.rigs:
im_size = stereo_rig.cameras[0].intrinsics.resolution
rotation1, rotation2, pose1, pose2, Q = \
cv2.stereoRectify(cameraMatrix1=stereo_rig.cameras[0].intrinsics.intrinsic_mat,
distCoeffs1=stereo_rig.cameras[0].intrinsics.distortion_coeffs,
cameraMatrix2=stereo_rig.cameras[1].intrinsics.intrinsic_mat,
distCoeffs2=stereo_rig.cameras[1].intrinsics.distortion_coeffs,
imageSize=(im_size[1], im_size[0]),
R=stereo_rig.cameras[1].extrinsics.rotation,
T=stereo_rig.cameras[1].extrinsics.translation,
flags=cv2.CALIB_ZERO_DISPARITY)[0:5]
stereo_rig.cameras[0].stereo_rotation = rotation1
stereo_rig.cameras[1].stereo_rotation = rotation2
stereo_rig.cameras[0].stereo_pose = pose1
stereo_rig.cameras[1].stereo_pose = pose2
stereo_rig.extrinsics.inv_rotation = np.linalg.inv(stereo_rig.extrinsics.rotation)
stereo_rig.Q = Q
stereo_rig.f = Q[2, 3]
stereo_rig.inv_baseline = Q[3, 2]
stereo_rig.ox = Q[0, 3]
stereo_rig.oy = Q[1, 3]