-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrectify_images.py
93 lines (73 loc) · 3.75 KB
/
rectify_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import numpy as np
import cv2
import matplotlib.pyplot as plt
from os.path import join as osjoin
def rectify_and_undistort(img_number, raw_images_path, rectified_images_path,
camera_params_path, downsample_image=True,
save_local=True):
K1, D1, K2, D2, R1, R2, P1, P2 = load_stereo_calibration_parameters(camera_params_path)
left = cv2.imread(osjoin(raw_images_path, f'{img_number}_L.jpg'))
right = cv2.imread(osjoin(raw_images_path, f'{img_number}_R.jpg'))
assert left is not None and right is not None
height, width = left.shape[:2]
left_rectified = rectify_and_undistort_single_image(left, K1, D1,
R1, P1, (width, height))
right_rectified = rectify_and_undistort_single_image(right, K2, D2,
R2, P2, (width, height))
return save_rectified_images(downsample_image, img_number, left_rectified,
right_rectified, rectified_images_path,
save_local)
def load_stereo_calibration_parameters(camera_params_path):
K1 = np.load(osjoin(camera_params_path, "K1.npy"))
D1 = np.load(osjoin(camera_params_path, "D1.npy"))
K2 = np.load(osjoin(camera_params_path, "K2.npy"))
D2 = np.load(osjoin(camera_params_path, "D2.npy"))
R1 = np.load(osjoin(camera_params_path, "R1.npy"))
R2 = np.load(osjoin(camera_params_path, "R2.npy"))
P1 = np.load(osjoin(camera_params_path, "P1.npy"))
P2 = np.load(osjoin(camera_params_path, "P2.npy"))
return K1, D1, K2, D2, R1, R2, P1, P2
def rectify_and_undistort_single_image(img, K, D, R, P, size):
map_x, map_y = cv2.initUndistortRectifyMap(K, D, R, P, size, cv2.CV_32FC1)
img_rectified = cv2.remap(img, map_x, map_y, cv2.INTER_LINEAR,
cv2.BORDER_CONSTANT)
return img_rectified
def save_rectified_images(downsample_image, img_number, left_rectified,
right_rectified, rectified_images_path,
save_local):
if downsample_image:
left_downsampled, right_downsampled = downsample_images(left_rectified,
right_rectified)
if save_local:
cv2.imwrite(osjoin(rectified_images_path,
f'downsampled/{img_number}_L_rectified.png'),
left_downsampled)
cv2.imwrite(osjoin(rectified_images_path,
f'downsampled/{img_number}_R_rectified.png'),
right_downsampled)
return left_downsampled, right_downsampled
if save_local:
cv2.imwrite(osjoin(rectified_images_path,
f'{img_number}_L_rectified.png'), left_rectified)
cv2.imwrite(osjoin(rectified_images_path,
f'{img_number}_R_rectified.png'), right_rectified)
return left_rectified, right_rectified
def downsample_images(left_rectified, right_rectified):
h, w = left_rectified.shape[:2]
w_down = int(w / 8)
h_down = int(h / 8)
dim = (w_down, h_down)
left_downsampled = cv2.resize(left_rectified, dim,
interpolation=cv2.INTER_AREA)
right_downsampled = cv2.resize(right_rectified, dim,
interpolation=cv2.INTER_AREA)
return left_downsampled, right_downsampled
def plot_rectified_images(h_new, left_rectified, right_rectiied):
fig, axes = plt.subplots(1, 2, figsize=(15, 10))
axes[0].imshow(left_rectified)
axes[1].imshow(right_rectiied)
for i in range(0, h_new, 400):
axes[0].axhline(i)
axes[1].axhline(i)
plt.suptitle("Rectified images")
plt.show()