def rectify_images_float(img1, x1, img2, x2, K, d, F, shearing=False): imsize = (img1.shape[1], img1.shape[0]) H1, H2, rms, max_error = epipolar.rectify_uncalibrated(x1, x2, F, imsize) if shearing: S = epipolar.rectify_shearing(H1, H2, imsize) H1 = S.dot(H1) rH = la.inv(K).dot(H1).dot(K) lH = la.inv(K).dot(H2).dot(K) map1x, map1y = cv2.initUndistortRectifyMap(K, d, rH, K, imsize, cv.CV_16SC2) map2x, map2y = cv2.initUndistortRectifyMap(K, d, lH, K, imsize, cv.CV_16SC2) rimg1 = cv2.remap(img1, map1x, map1y, interpolation=cv.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)) rimg2 = cv2.remap(img2, map2x, map2y, interpolation=cv.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)) return rimg1, rimg2
def rectify_images(img1, x1, img2, x2, K, d, F, shearing=False): imsize = (img1.shape[1], img1.shape[0]) H1, H2 = epipolar.rectify_uncalibrated(x1, x2, F, imsize) #x1_1d = np.empty((2*x1.shape[1],), dtype=float) #x1_1d[0::2] = x1[0,:] #x1_1d[1::2] = x1[1,:] #x2_1d = np.empty((2*x2.shape[1],), dtype=float) #x2_1d[0::2] = x2[0,:] #x2_1d[1::2] = x2[1,:] #success, cvH1, cvH2 = cv2.stereoRectifyUncalibrated(x1_1d, x2_1d, F, imsize) if shearing: S = epipolar.rectify_shearing(H1, H2, imsize) H1 = S.dot(H1) rH = la.inv(K).dot(H1).dot(K) lH = la.inv(K).dot(H2).dot(K) # TODO: lRect or rRect for img1/img2 ?? map1x, map1y = cv2.initUndistortRectifyMap(K, d, rH, K, imsize, cv.CV_16SC2) map2x, map2y = cv2.initUndistortRectifyMap(K, d, lH, K, imsize, cv.CV_16SC2) # Convert the images to RGBA (add an axis with 4 values) img1 = np.tile(img1[:, :, np.newaxis], [1, 1, 4]) img1[:, :, 3] = 255 img2 = np.tile(img2[:, :, np.newaxis], [1, 1, 4]) img2[:, :, 3] = 255 rimg1 = cv2.remap(img1, map1x, map1y, interpolation=cv.CV_INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)) rimg2 = cv2.remap(img2, map2x, map2y, interpolation=cv.CV_INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)) # Put a red background on the invalid values # TODO: Return a mask for valid/invalid values # TODO: There is aliasing hapenning on the images border. We should # invalidate a margin around the border so we're sure we have only valid # pixels rimg1[rimg1[:, :, 3] == 0, :] = (255, 0, 0, 255) rimg2[rimg2[:, :, 3] == 0, :] = (255, 0, 0, 255) return rimg1, rimg2
def rectify_images(img1, x1, img2, x2, K, d, F, shearing=False): imsize = (img1.shape[1], img1.shape[0]) H1, H2 = epipolar.rectify_uncalibrated(x1, x2, F, imsize) #x1_1d = np.empty((2*x1.shape[1],), dtype=float) #x1_1d[0::2] = x1[0,:] #x1_1d[1::2] = x1[1,:] #x2_1d = np.empty((2*x2.shape[1],), dtype=float) #x2_1d[0::2] = x2[0,:] #x2_1d[1::2] = x2[1,:] #success, cvH1, cvH2 = cv2.stereoRectifyUncalibrated(x1_1d, x2_1d, F, imsize) if shearing: S = epipolar.rectify_shearing(H1, H2, imsize) H1 = S.dot(H1) rH = la.inv(K).dot(H1).dot(K) lH = la.inv(K).dot(H2).dot(K) # TODO: lRect or rRect for img1/img2 ?? map1x, map1y = cv2.initUndistortRectifyMap(K, d, rH, K, imsize, cv.CV_16SC2) map2x, map2y = cv2.initUndistortRectifyMap(K, d, lH, K, imsize, cv.CV_16SC2) # Convert the images to RGBA (add an axis with 4 values) img1 = np.tile(img1[:,:,np.newaxis], [1,1,4]) img1[:,:,3] = 255 img2 = np.tile(img2[:,:,np.newaxis], [1,1,4]) img2[:,:,3] = 255 rimg1 = cv2.remap(img1, map1x, map1y, interpolation=cv.CV_INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0,0)) rimg2 = cv2.remap(img2, map2x, map2y, interpolation=cv.CV_INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0,0)) # Put a red background on the invalid values # TODO: Return a mask for valid/invalid values # TODO: There is aliasing hapenning on the images border. We should # invalidate a margin around the border so we're sure we have only valid # pixels rimg1[rimg1[:,:,3] == 0,:] = (255,0,0,255) rimg2[rimg2[:,:,3] == 0,:] = (255,0,0,255) return rimg1, rimg2
def rectify_images(img1, x1, img2, x2, K, d, F, shearing=False): imsize = (img1.shape[1], img1.shape[0]) H1, H2, rms, max_error = epipolar.rectify_uncalibrated(x1, x2, F, imsize) if shearing: S = epipolar.rectify_shearing(H1, H2, imsize) H1 = S.dot(H1) rH = la.inv(K).dot(H1).dot(K) lH = la.inv(K).dot(H2).dot(K) # check for y parallax max_yparallax = get_y_parallax(x1, x2, rH, lH, imsize) # TODO: lRect or rRect for img1/img2 ?? map1x, map1y = cv2.initUndistortRectifyMap(K, d, rH, K, imsize, cv.CV_16SC2) map2x, map2y = cv2.initUndistortRectifyMap(K, d, lH, K, imsize, cv.CV_16SC2) # Convert the images to RGBA (add an axis with 4 values) img1 = np.tile(img1[:, :, np.newaxis], [1, 1, 4]) img1[:, :, 3] = 255 img2 = np.tile(img2[:, :, np.newaxis], [1, 1, 4]) img2[:, :, 3] = 255 rimg1 = cv2.remap(img1, map1x, map1y, interpolation=cv.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)) rimg2 = cv2.remap(img2, map2x, map2y, interpolation=cv.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)) # Put a red background on the invalid values # TODO: Return a mask for valid/invalid values # TODO: There is aliasing happening on the images border. We should # invalidate a margin around the border so we're sure we have only valid # pixels rimg1[rimg1[:, :, 3] == 0, :] = (255, 0, 0, 255) rimg2[rimg2[:, :, 3] == 0, :] = (255, 0, 0, 255) return rimg1, rimg2, rms, max_error, lH, rH, max_yparallax
def rectify_images(img1, x1, img2, x2, K, d, F, shearing=False): imsize = (img1.shape[1], img1.shape[0]) H1, H2 = epipolar.rectify_uncalibrated(x1, x2, F, imsize) if shearing: S = epipolar.rectify_shearing(H1, H2, imsize) H1 = S.dot(H1) print('The homographies are: \n', H1, '\nR\n', H2) rH = la.inv(K).dot(H1).dot(K) lH = la.inv(K).dot(H2).dot(K) print('The homographies are: \n', lH, '\nR\n', rH) map1x, map1y = cv.initUndistortRectifyMap(K, d, rH, K, imsize, cv.CV_16SC2) map2x, map2y = cv.initUndistortRectifyMap(K, d, lH, K, imsize, cv.CV_16SC2) # Convert the images to RGBA (add an axis with 4 values) img1 = np.tile(img1[:, :, np.newaxis], [1, 1, 4]) img1[:, :, 3] = 255 img2 = np.tile(img2[:, :, np.newaxis], [1, 1, 4]) img2[:, :, 3] = 255 rimg1 = cv.remap(img1, map1x, map1y, interpolation=cv.CV_INTER_LINEAR, borderMode=cv.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)) rimg2 = cv.remap(img2, map2x, map2y, interpolation=cv.CV_INTER_LINEAR, borderMode=cv.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)) # Put a red background on the invalid values # TODO: Return a mask for valid/invalid values # TODO: There is aliasing hapenning on the images border. We should # invalidate a margin around the border so we're sure we have only valid # pixels rimg1[rimg1[:, :, 3] == 0, :] = (255, 0, 0, 255) rimg2[rimg2[:, :, 3] == 0, :] = (255, 0, 0, 255) return rimg1, rimg2
def get_epipolar_rectification_matrices(img1, x1, img2, x2, K, d, F): imsize = (img1.shape[1], img1.shape[0]) H1, H2, rms, max_error = epipolar.rectify_uncalibrated(x1, x2, F, imsize) rH = la.inv(K).dot(H1).dot(K) lH = la.inv(K).dot(H2).dot(K) return rH, lH