Skip to content

Commit c3c7320

Browse files
committed
files added
1 parent 5bc0b57 commit c3c7320

34 files changed

+604
-0
lines changed

DisparityMap.py

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import numpy as np
2+
import cv2
3+
import glob
4+
import matplotlib.pyplot as plt
5+
from tqdm import tqdm
6+
7+
8+
9+
def get_blocks(img,window_size):
10+
height,width = img.shape
11+
window_set=[]
12+
window_loc = []
13+
for y in range(window_size, height - window_size):
14+
for x in range(window_size, width - window_size):
15+
img_window = img[y:y + window_size, x:x + window_size]
16+
window_set.append(img_window)
17+
window_loc.append((y,x))
18+
return window_set,window_loc
19+
20+
def get_boundary_condition(x,y,search_range,width,height):
21+
22+
23+
range_min = max(0, x-search_range)
24+
range_max = min(width,x+search_range )
25+
26+
return range_max,range_min
27+
28+
def get_sdd(imgl_window,imgr_window):
29+
max = 1e10
30+
if imgl_window.shape == imgr_window.shape:
31+
diff = np.abs(imgl_window - imgr_window)
32+
33+
SSD = np.sum(np.square(imgl_window - imgr_window))
34+
return SSD
35+
else:
36+
return max
37+
38+
def get_disparity(img1,img2,search_range,type):
39+
40+
if type==1:
41+
print("method 1")
42+
imgl = img1.copy()
43+
imgr = img2.copy()
44+
disparityImg = np.zeros(img2.shape)
45+
window_size = 21
46+
47+
48+
#get a patch in left image
49+
height,width = img1.shape
50+
for y in tqdm(range(window_size,height-window_size)):
51+
for x in range(window_size,width-window_size):
52+
53+
54+
#get window boundary conditions
55+
# range_max,range_min = get_boundary_condition(x,y,50,width,height)
56+
range_min = max(0, x-100)
57+
range_max = min(width,x+100)
58+
imgl_window = imgl[y:y+window_size, x:x+window_size]
59+
60+
first = True
61+
min_val = None
62+
index = None
63+
#search for corresponding patch in right image
64+
for i in range(range_min,range_max):
65+
66+
imgr_window = imgr[y:y+window_size, i:i+window_size]
67+
68+
69+
SSD = get_sdd(imgl_window,imgr_window)
70+
if first:
71+
min_val = SSD
72+
index = i
73+
first = False
74+
else:
75+
if SSD < min_val:
76+
min_val = SSD
77+
index = i
78+
79+
80+
81+
d = np.abs(index - x)
82+
83+
disparityImg[y,x] = d
84+
85+
return disparityImg
86+
87+
if type == 2:
88+
print("method 2")
89+
# img1 = cv2.resize(img1, (int(img1.shape[1]*0.5), int(img1.shape[0] *0.5)))
90+
# img2 = cv2.resize(img2, (int(img2.shape[1]*0.5), int(img2.shape[0] *0.5)))
91+
imgl = img1.copy()
92+
imgr = img2.copy()
93+
disparityImg = np.zeros(img2.shape)
94+
window_size = 15
95+
#get a patch in left image
96+
height,width = img1.shape
97+
98+
imgl = imgl.astype(np.int32)
99+
imgr = imgr.astype(np.int32)
100+
101+
for y in tqdm(range(0,height-window_size)):
102+
103+
imgl_window_set = []
104+
imgr_window_set = []
105+
106+
for x in range(0,width-window_size):
107+
left_window = imgl[y:y+window_size, x:x+window_size]
108+
right_window = imgr[y:y+window_size, x:x+window_size]
109+
imgl_window_set.append(left_window.flatten())
110+
imgr_window_set.append(right_window.flatten())
111+
112+
imgl_window_set = np.array(imgl_window_set)
113+
imgr_window_set = np.array(imgr_window_set)
114+
print(len(imgl_window_set))
115+
for i in range(len(imgl_window_set)):
116+
117+
SSD = np.sum(np.square(imgr_window_set- imgl_window_set[i]),axis=1)
118+
index = np.argmin(SSD)
119+
disparity = np.abs(i - index)
120+
# disparity = ((disparity+1) / 2) * 255
121+
disparityImg[y,i] = np.uint8(disparity)
122+
123+
124+
125+
return disparityImg
126+
127+
else:
128+
return np.zeros(img2.shape)
129+
130+
131+
132+
133+
def get_depth(disparityImg,b,f):
134+
depth = (b * f) / (disparityImg + 1e-10)
135+
depth[depth > 10000] = 10000
136+
137+
depth_map = np.uint8(depth * 255 / np.max(depth))
138+
return depth_map

EstimateFEmatrix.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import numpy as np
2+
import cv2
3+
import glob
4+
5+
def normalize_points(pts):
6+
'''
7+
https://www.cc.gatech.edu/classes/AY2016/cs4476_fall/results/proj3/html/arao83/index.html
8+
'''
9+
mean_ =np.mean(pts,axis=0)
10+
11+
#finding centre
12+
u = pts[:,0] - mean_[0]
13+
v = pts[:,1] - mean_[1]
14+
15+
sd_u = 1/np.std(pts[:,0])
16+
sd_v = 1/np.std(pts[:,1])
17+
Tscale = np.array([[sd_u,0,0],[0,sd_v,0],[0,0,1]])
18+
Ta = np.array([[1,0,-mean_[0]],[0,1,-mean_[1]],[0,0,1]])
19+
T = np.dot(Tscale,Ta)
20+
21+
pt = np.column_stack((pts,np.ones(len(pts))))
22+
norm_pts = (np.dot(T,pt.T)).T
23+
24+
return norm_pts,T
25+
26+
def estimate_F(img1_pts,img2_pts):
27+
28+
#normalize points
29+
img1_pts,T1 = normalize_points(img1_pts)
30+
img2_pts,T2 = normalize_points(img2_pts)
31+
32+
x1 = img1_pts[:,0]
33+
y1 = img1_pts[:,1]
34+
x1dash = img2_pts[:,0]
35+
y1dash = img2_pts[:,1]
36+
A = np.zeros((len(x1),9))
37+
38+
for i in range(len(x1)):
39+
A[i] = np.array([x1dash[i]*x1[i],x1dash[i]*y1[i],x1dash[i], y1dash[i]*x1[i],y1dash[i]*y1[i],y1dash[i],x1[i],y1[i],1])
40+
41+
#taking SVD of A for estimation of F
42+
U, S, V = np.linalg.svd(A,full_matrices=True)
43+
F_est = V[-1, :]
44+
F_est = F_est.reshape(3,3)
45+
46+
# Enforcing rank 2 for F
47+
ua,sa,va = np.linalg.svd(F_est,full_matrices=True)
48+
sa = np.diag(sa)
49+
50+
sa[2,2] = 0
51+
52+
53+
F = np.dot(ua,np.dot(sa,va))
54+
# F, mask = cv2.findFundamentalMat(img1_pts,img2_pts,cv2.FM_LMEDS)
55+
F = np.dot(T2.T, np.dot(F, T1))
56+
57+
return F
58+
59+
def ransac(pt1,pt2):
60+
n_rows = np.array(pt1).shape[0]
61+
no_iter = 1000
62+
threshold = 0.05
63+
inliers = 0
64+
65+
final_indices = []
66+
for i in range(no_iter):
67+
indices = []
68+
69+
#randomly select 8 points
70+
random = np.random.choice(n_rows,size = 8)
71+
img1_8pt = pt1[random]
72+
img2_8pt = pt2[random]
73+
74+
F_est = estimate_F(img1_8pt,img2_8pt)
75+
76+
for j in range(n_rows):
77+
x1 = pt1[j]
78+
x2 = pt2[j]
79+
80+
#error computation
81+
pt1_ = np.array([x1[0],x1[1],1])
82+
pt2_ = np.array([x2[0],x2[1],1])
83+
error = np.dot(pt1_.T,np.dot(F_est,pt2_))
84+
85+
if np.abs(error) < threshold:
86+
indices.append(j)
87+
88+
if len(indices) > inliers:
89+
inliers = len(indices)
90+
final_indices = indices
91+
F = F_est
92+
93+
94+
img1_points = pt1[final_indices]
95+
img2_points = pt2[final_indices]
96+
97+
98+
return img1_points,img2_points, F
99+
100+
101+
102+
103+
104+
def estimate_E(k1,k2,F):
105+
E_est = np.dot(k2.T,np.dot(F,k1))
106+
#reconstructing E by correcting singular values
107+
U, S, V = np.linalg.svd(E_est,full_matrices=True)
108+
S = np.diag(S)
109+
S[0,0],S[1,1],S[2,2] = 1,1,0
110+
E = np.dot(U,np.dot(S,V))
111+
112+
return E

PoseEstimation.py

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
import numpy as np
3+
import cv2
4+
import glob
5+
import matplotlib.pyplot as plt
6+
7+
8+
9+
10+
def point_triangulation(k1,k2,pt1,pt2,R1,C1,R2,C2):
11+
points_3d = []
12+
13+
I = np.identity(3)
14+
C1 = C1.reshape(3,1)
15+
C2 = C2.reshape(3,1)
16+
17+
#calculating projection matrix P = K[R|T]
18+
P1 = np.dot(k1,np.dot(R1,np.hstack((I,-C1))))
19+
P2 = np.dot(k2,np.dot(R2,np.hstack((I,-C2))))
20+
21+
#homogeneous coordinates for images
22+
xy = np.hstack((pt1,np.ones((len(pt1),1))))
23+
xy_cap = np.hstack((pt2,np.ones((len(pt1),1))))
24+
25+
26+
p1,p2,p3 = P1
27+
p1_cap, p2_cap,p3_cap = P2
28+
29+
#constructing contraints matrix
30+
for i in range(len(xy)):
31+
A = []
32+
x = xy[i][0]
33+
y = xy[i][1]
34+
x_cap = xy_cap[i][0]
35+
y_cap = xy_cap[i][1]
36+
37+
A.append((y*p3) - p2)
38+
A.append((x*p3) - p1)
39+
40+
A.append((y_cap*p3_cap)- p2_cap)
41+
A.append((x_cap*p3_cap) - p1_cap)
42+
43+
A = np.array(A).reshape(4,4)
44+
45+
_, _, v = np.linalg.svd(A)
46+
x_ = v[-1,:]
47+
x_ = x_/x_[-1]
48+
x_ = x_[:3]
49+
points_3d.append(x_)
50+
51+
52+
return points_3d
53+
54+
def linear_triangulation(R_Set,T_Set,pt1,pt2,k1,k2):
55+
R1_ = np.identity(3)
56+
T1_ = np.zeros((3,1))
57+
points_3d_set = []
58+
for i in range(len(R_Set)):
59+
points3d = point_triangulation(k1,k2,pt1,pt2,R1_,T1_,R_Set[i],T_Set[i])
60+
points_3d_set.append(points3d)
61+
62+
return points_3d_set
63+
64+
def get_RTset(E):
65+
66+
U, S, V = np.linalg.svd(E,full_matrices=True)
67+
W = np.array([[0,-1,0],[1,0,0],[0,0,1]])
68+
69+
R1 = np.dot(U,np.dot(W,V))
70+
R2 = np.dot(U,np.dot(W,V))
71+
R3 = np.dot(U,np.dot(W.T,V))
72+
R4 = np.dot(U,np.dot(W.T,V))
73+
74+
T1 = U[:,2]
75+
T2 = -U[:,2]
76+
T3 = U[:,2]
77+
T4 = -U[:,2]
78+
79+
R = [R1,R2,R3,R4]
80+
T = [T1,T2,T3,T4]
81+
82+
for i in range(len(R)):
83+
if (np.linalg.det(R[i]) < 0):
84+
R[i] = -R[i]
85+
T[i] = -T[i]
86+
87+
return R, T
88+
89+
def compute_cheriality(pt,r3,t):
90+
count_depth = 0
91+
for xy in pt:
92+
if np.dot(r3,(xy-t)) > 0 and t[2] > 0:
93+
count_depth +=1
94+
return count_depth
95+
96+
def extract_pose(E,pt1,pt2,k1,k2):
97+
#get four rotation and translation matrices
98+
R_set, T_set = get_RTset(E)
99+
100+
#get 3D points using triangulation
101+
pts_3d = linear_triangulation(R_set,T_set,pt1,pt2,k1,k2)
102+
threshold = 0
103+
#Four sets are available for each possibility
104+
for i in range(len(R_set)):
105+
R = R_set[i]
106+
T = T_set[i]
107+
r3 = R[2]
108+
pt3d = pts_3d[i]
109+
110+
#calculating which R satisfies the condition
111+
num_depth_positive = compute_cheriality(pt3d,r3,T)
112+
if num_depth_positive > threshold:
113+
index = i
114+
threshold = num_depth_positive
115+
116+
R_best = R_set[index]
117+
T_best = T_set[index]
118+
119+
return R_best,T_best

0 commit comments

Comments
 (0)