-
Notifications
You must be signed in to change notification settings - Fork 0
/
Disparity_DP.py
84 lines (64 loc) · 2.56 KB
/
Disparity_DP.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
import cv2
import random
import numpy as np
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
left_img = cv2.imread('output/calibration_binocular/rectifiedleft04.jpg',
cv2.IMREAD_GRAYSCALE) # read it as a grayscale image
right_img = cv2.imread('output/calibration_binocular/rectifiedright04.jpg', cv2.IMREAD_GRAYSCALE)
# Disparity Computation for Left Image
# OcclusionCost = 20 (You can adjust this, depending on how much threshold you want to give for noise)
OcclusionCost = 20
rows, cols = left_img.shape
d_map_l = np.zeros(left_img.shape)
d_map_r = np.zeros(right_img.shape)
def optimal_match():
for x in range(rows):
CostMatrix = np.zeros((cols + 1, cols + 1))
DirectionMatrix = np.zeros((cols + 1, cols + 1))
for i in range(1, cols + 1):
CostMatrix[i, 0] = i * OcclusionCost
CostMatrix[0, i] = i * OcclusionCost
for r in range(1, cols + 1):
for c in range(1, cols + 1):
min1 = CostMatrix[r - 1, c - 1] + np.abs(int(left_img[x, r - 1]) - int(right_img[x, c - 1]))
min2 = CostMatrix[r - 1, c] + OcclusionCost
min3 = CostMatrix[r, c - 1] + OcclusionCost
CostMatrix[r, c] = min([min1, min2, min3])
cmin = CostMatrix[r, c]
if cmin == min1:
DirectionMatrix[r, c] = 1
elif cmin == min2:
DirectionMatrix[r, c] = 2
elif cmin == min3:
DirectionMatrix[r, c] = 3
p = cols
q = cols
while p != 0 and q != 0:
if DirectionMatrix[p, q] == 1:
p = p - 1
q = q - 1
d_map_l[x][p] = np.abs(p - q)
d_map_r[x][q] = np.abs(p - q)
elif DirectionMatrix[p][q] == 2:
p = p - 1
d_map_l[x][p] = np.abs(p - q)
elif DirectionMatrix[p][q] == 3:
q = q - 1
d_map_r[x][q] = np.abs(p - q)
def show_image(title, image):
max_val = image.max()
# image = np.absolute(image)
image = np.divide(image, max_val)
# cv2.imshow(title, image)
cv2.imwrite(title, image * 255)
def main():
optimal_match()
print("========================Left Disparity=========================")
print(d_map_l)
print("========================Right Disparity=========================")
print(d_map_r)
show_image('output/Disparity/DPright04.jpg', d_map_l)
show_image('output/Disparity/DPleft04.jpg', d_map_r)
main()