Skip to content

Commit 870f668

Browse files
committed
Completed problem set 4 + refactorings
1 parent 2a50334 commit 870f668

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+594
-113
lines changed

Diff for: ps_0/ps0.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ def p5():
6060
imsave(im_blue, 'output/ps0-5-b-1.png')
6161

6262

63-
p1()
64-
p2()
65-
p3()
66-
p4()
67-
p5()
63+
if __name__ == '__main__':
64+
p1()
65+
p2()
66+
p3()
67+
p4()
68+
p5()

Diff for: ps_1/ps1.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ def update_threshold(x, axs, sliders, buttons):
6060
print(sliders[0].val, sliders[1].val)
6161
return [1], [cv.Canny(im, sliders[0].val, sliders[1].val)]
6262

63-
imshow([im, edges], ['original', 'canny edges'], slider_attr=slider_attr,
64-
slider_callback=[update_threshold] * 2)
63+
imshow([im, edges], ['original', 'canny edges'], slider_attr=slider_attr, slider_callback=[update_threshold] * 2)
6564

6665

6766
def hough_lines_acc(edges, theta_range=np.arange(-180, 180)):
@@ -110,14 +109,14 @@ def round_range(start, end, max):
110109
size = [[size[i] + 1, size[i]][size[i] % 2] for i in range(len(acc.shape))]
111110
bounds = [(size[i] - 1) // 2 for i in range(len(acc.shape))]
112111
pts = []
113-
max_peak = np.max(acc)
112+
threshold_val = acc.min()*(1-threshold) + acc.max()*threshold
114113
while len(pts) < npeaks:
115114
index = np.unravel_index(np.asarray(acc).argmax(), acc.shape)
116-
if len(index) == 2 and acc[index[0], index[1]] >= threshold * max_peak and acc[index[0], index[1]] > 0:
115+
if len(index) == 2 and acc[index[0], index[1]] >= threshold_val and acc[index[0], index[1]] > 0:
117116
pts.append((index[0], index[1]))
118117
acc[np.ix_(round_range(index[0] - bounds[0], index[0] + bounds[0] + 1, acc.shape[0]),
119118
round_range(index[1] - bounds[1], index[1] + bounds[1] + 1, acc.shape[1]))] = 0
120-
elif len(index) == 3 and acc[index[0], index[1], index[2]] >= threshold * max_peak and acc[
119+
elif len(index) == 3 and acc[index[0], index[1], index[2]] >= threshold_val and acc[
121120
index[0], index[1], index[2]] > 0:
122121
pts.append((index[0], index[1], index[2]))
123122
acc[np.ix_(round_range(index[0] - bounds[0], index[0] + bounds[0] + 1, acc.shape[0]),
@@ -239,8 +238,7 @@ def update_exp(x, axs, sliders, buttons):
239238
return [0, 1], [draw_line_on_im(np.zeros(im.shape), lines_p_c), draw_line_on_im(np.zeros(im.shape), lines_p_m)]
240239

241240
imshow([draw_line_on_im(np.zeros(im.shape), lines_p_c), draw_line_on_im(np.zeros(im.shape), lines_p_m)],
242-
['custom hough peak', 'matlab\'s hough peak'],
243-
slider_attr=slider_attr, slider_callback=[update_exp] * 2)
241+
['custom hough peak', 'matlab\'s hough peak'], slider_attr=slider_attr, slider_callback=[update_exp] * 2)
244242

245243

246244
def p3():
@@ -266,8 +264,8 @@ def update_exp(x, axs, sliders, buttons):
266264
slider_attr = [{'label': 'num peaks', 'valmin': 0, 'valmax': 99, 'valstep': 1},
267265
{'label': 'hough peak threshold', 'valmin': 0.1, 'valmax': .99}]
268266
imshow([im_noise, blur_im_noise, edge_noise, edge_blur_noise, zeros, zeros],
269-
['im_noise', 'blur_im_noise', 'edge_noise', 'edge_blur_noise', 'noise lines', 'blur lines'],
270-
slider_attr=slider_attr, slider_callback=[update_exp] * 2, shape=(3, 2))
267+
['im_noise', 'blur_im_noise', 'edge_noise', 'edge_blur_noise', 'noise lines', 'blur lines'], shape=(3, 2),
268+
slider_attr=slider_attr, slider_callback=[update_exp] * 2)
271269

272270

273271
def p4():
@@ -434,7 +432,7 @@ def p8():
434432
imshow([edge, im_color])
435433

436434

437-
""" Run just the methods you need, else you will go through a lot of interactive canvas popups """
435+
""" Run just the methods that are needed, else the session will go through a lot of interactive canvas popups """
438436
# testing_imshow()
439437
# p1()
440438
# p2_experiment()

Diff for: ps_2/disparity_ncorr.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def compute_stereo_on_slice(i_th):
2525
for j in range(w_l, left.shape[1] - w_l):
2626
(d, ncorr) = (0, np.NINF)
2727
for j_i in range(max(w_l, j - max_disp), min(left.shape[1] - w_l, j + max_disp)):
28-
l_w = left[get_window_ix(left, (i_th, j), window_shape)] * 1.0
29-
r_w = right[get_window_ix(right, (i_th, j_i), window_shape)] * 1.0
28+
l_w = left[get_window_ix(left.shape, (i_th, j), window_shape)] * 1.0
29+
r_w = right[get_window_ix(right.shape, (i_th, j_i), window_shape)] * 1.0
3030
l_w = (l_w - np.average(l_w))
3131
r_w = (r_w - np.average(r_w))
3232
std_p = l_w.std() * r_w.std()

Diff for: ps_2/disparity_ssd.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def compute_stereo_on_slice(i):
2626
for j in range(w_l, left.shape[1] - w_l):
2727
(d, ssd) = (0, np.PINF)
2828
for j_i in range(max(w_l, j - max_disp), min(left.shape[1] - w_l, j + max_disp)):
29-
l_w = left[get_window_ix(left, (i, j), window_shape)] * 1.0
30-
r_w = right[get_window_ix(right, (i, j_i), window_shape)] * 1.0
29+
l_w = left[get_window_ix(left.shape, (i, j), window_shape)] * 1.0
30+
r_w = right[get_window_ix(right.shape, (i, j_i), window_shape)] * 1.0
3131
ssd_i = np.sum(np.square(np.subtract(l_w, r_w)))
3232
(d, ssd) = (j_i - j if ssd_i < ssd else d, min(ssd_i, ssd))
3333
d_map_slice[j] = d
125 KB
Binary file not shown.
6.25 KB
Binary file not shown.

Diff for: ps_2/objects/p4_disparities(pair0-L,pair0-R).npy

256 KB
Binary file not shown.
125 KB
Binary file not shown.
516 KB
Binary file not shown.

Diff for: ps_2/observations/Harris Response.png

1.11 MB
Loading

Diff for: ps_3/helper.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import numpy as np
2+
3+
4+
def construct_a_mat(pts_world, pts_proj):
5+
a = []
6+
for (x, y, z), (u, v) in zip(pts_world, pts_proj):
7+
a.append([x, y, z, 1, 0, 0, 0, 0, -u * x, -u * y, -u * z, -u])
8+
a.append([0, 0, 0, 0, x, y, z, 1, -v * x, -v * y, -v * z, -v])
9+
return np.asarray(a)
10+
11+
12+
def construct_f_mat(pts_im_left, pts_im_right):
13+
f = []
14+
for (u, v, *rest), (u_, v_, *rest) in zip(pts_im_left, pts_im_right):
15+
f.append([u_ * u, u_ * v, u_, v_ * u, v_ * v, v_, u, v, 1])
16+
return np.asarray(f)
17+
18+
19+
def solve_for_svd(a):
20+
w, v = np.linalg.eig(np.matmul(a.T, a))
21+
m = v[:, w.argmin()]
22+
return m
23+
24+
25+
def solve_with_leastsq(a):
26+
b = -a[:, -1]
27+
a = a[:, 0:-1]
28+
m, res, _, _ = np.linalg.lstsq(a, b, rcond=None)
29+
return np.append(m, 1)
30+
31+
32+
def solve_for_m_svd(pts_world, pts_proj):
33+
a = construct_a_mat(pts_world, pts_proj)
34+
return solve_for_svd(a).reshape((3, 4))
35+
36+
37+
def solve_for_m_leastsq(pts_world, pts_proj):
38+
a = construct_a_mat(pts_world, pts_proj)
39+
return solve_with_leastsq(a).reshape((3, 4))
40+
41+
42+
def solve_for_f_svd(pts_im1, pts_im2):
43+
a = construct_f_mat(pts_im1, pts_im2)
44+
return solve_for_svd(a).reshape((3, 3))
45+
46+
47+
def solve_for_f_leastsq(pts_im1, pts_im2):
48+
a = construct_f_mat(pts_im1, pts_im2)
49+
return solve_with_leastsq(a).reshape((3, 3))
50+
51+
52+
def fix_f_rank(f):
53+
u, s, vh = np.linalg.svd(f)
54+
s[s.argmin()] = 0
55+
return np.matmul(np.matmul(u, np.diag(s)), vh)
56+
57+
58+
def get_epipolar_line_ends(f, pts_b, im_shape, t=None):
59+
pts_b_t = np.append(np.asarray(pts_b), np.ones((len(pts_b), 1)), axis=1).T
60+
l_l = np.cross([0, 0, 1], [im_shape[0], 0, 1])
61+
l_r = np.cross([0, im_shape[1], 1], [im_shape[0], im_shape[1], 1])
62+
l_a = np.matmul(f.T, pts_b_t)
63+
if t is not None:
64+
l_a = np.matmul(t.T, l_a)
65+
skew = lambda x: np.array([[0, -x[2], x[1]], [x[2], 0, -x[0]], [-x[1], x[0], 0]])
66+
p_l = np.matmul(skew(l_l).T, l_a).T
67+
p_r = np.matmul(skew(l_r).T, l_a).T
68+
p_l = np.asarray([(int(x[0] / x[2]), int(x[1] / x[2])) for x in p_l])
69+
p_r = np.asarray([(int(x[0] / x[2]), int(x[1] / x[2])) for x in p_r])
70+
return tuple(zip(p_l, p_r))
71+
72+
73+
def get_transformation_mat_2d(points):
74+
s_a = 1 / np.max(points)
75+
t_a_s = np.diag([s_a, s_a, 1])
76+
c_a = np.mean(points, axis=0)
77+
t_a_c = np.asarray([[1, 0, -c_a[0]], [0, 1, -c_a[1]], [0, 0, 1]])
78+
return np.matmul(t_a_s, t_a_c)
79+
80+
81+
def calculate_residual(m, pts_world, pts_proj):
82+
pts_world_mat = np.append(np.asarray(pts_world), np.ones((len(pts_world), 1)), axis=1).T
83+
pts_proj_est = np.matmul(m, pts_world_mat)
84+
pts_proj_est_non_homo = pts_proj_est.T[:, 0:2] / pts_proj_est.T[:, [2, 2]]
85+
ssd_error = np.linalg.norm(pts_proj_est_non_homo - np.asarray(pts_proj))
86+
return np.sqrt(ssd_error)

Diff for: ps_3/ps3.py

+6-84
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,14 @@
22
import cv2 as cv
33
import numpy as np
44

5+
from ps_3.helper import construct_a_mat, solve_for_m_svd, calculate_residual, solve_for_m_leastsq, solve_for_f_svd, \
6+
fix_f_rank, get_epipolar_line_ends, get_transformation_mat_2d
57
from ps_hepers.helpers import read_points, imshow, imread, imsave
68

7-
8-
def construct_a_mat(pts_world, pts_proj):
9-
a = []
10-
for (x, y, z), (u, v) in zip(pts_world, pts_proj):
11-
a.append([x, y, z, 1, 0, 0, 0, 0, -u * x, -u * y, -u * z, -u])
12-
a.append([0, 0, 0, 0, x, y, z, 1, -v * x, -v * y, -v * z, -v])
13-
return np.asarray(a)
14-
15-
16-
def construct_f_mat(pts_im_left, pts_im_right):
17-
f = []
18-
for (u, v, *rest), (u_, v_, *rest) in zip(pts_im_left, pts_im_right):
19-
f.append([u_ * u, u_ * v, u_, v_ * u, v_ * v, v_, u, v, 1])
20-
return np.asarray(f)
21-
22-
23-
def solve_for_svd(a):
24-
w, v = np.linalg.eig(np.matmul(a.T, a))
25-
m = v[:, w.argmin()]
26-
return m
27-
28-
29-
def solve_with_leastsq(a):
30-
b = -a[:, -1]
31-
a = a[:, 0:-1]
32-
m, res, _, _ = np.linalg.lstsq(a, b, rcond=None)
33-
return np.append(m, 1)
34-
35-
36-
def solve_for_m_svd(pts_world, pts_proj):
37-
a = construct_a_mat(pts_world, pts_proj)
38-
return solve_for_svd(a).reshape((3, 4))
39-
40-
41-
def solve_for_m_leastsq(pts_world, pts_proj):
42-
a = construct_a_mat(pts_world, pts_proj)
43-
return solve_with_leastsq(a).reshape((3, 4))
44-
45-
46-
def solve_for_f_svd(pts_im1, pts_im2):
47-
a = construct_f_mat(pts_im1, pts_im2)
48-
return solve_for_svd(a).reshape((3, 3))
49-
50-
51-
def solve_for_f_leastsq(pts_im1, pts_im2):
52-
a = construct_f_mat(pts_im1, pts_im2)
53-
return solve_with_leastsq(a).reshape((3, 3))
54-
55-
56-
def fix_f_rank(f):
57-
u, s, vh = np.linalg.svd(f)
58-
s[s.argmin()] = 0
59-
return np.matmul(np.matmul(u, np.diag(s)), vh)
60-
61-
62-
def get_epipolar_line_ends(f, pts_b, im_shape, t=None):
63-
pts_b_t = np.append(np.asarray(pts_b), np.ones((len(pts_b), 1)), axis=1).T
64-
l_l = np.cross([0, 0, 1], [im_shape[0], 0, 1])
65-
l_r = np.cross([0, im_shape[1], 1], [im_shape[0], im_shape[1], 1])
66-
l_a = np.matmul(f.T, pts_b_t)
67-
if t is not None:
68-
l_a = np.matmul(t.T, l_a)
69-
skew = lambda x: np.array([[0, -x[2], x[1]], [x[2], 0, -x[0]], [-x[1], x[0], 0]])
70-
p_l = np.matmul(skew(l_l).T, l_a).T
71-
p_r = np.matmul(skew(l_r).T, l_a).T
72-
p_l = np.asarray([(int(x[0] / x[2]), int(x[1] / x[2])) for x in p_l])
73-
p_r = np.asarray([(int(x[0] / x[2]), int(x[1] / x[2])) for x in p_r])
74-
return tuple(zip(p_l, p_r))
75-
76-
77-
def get_transformation_mat_2d(points):
78-
s_a = 1 / np.max(points)
79-
t_a_s = np.diag([s_a, s_a, 1])
80-
c_a = np.mean(points, axis=0)
81-
t_a_c = np.asarray([[1, 0, -c_a[0]], [0, 1, -c_a[1]], [0, 0, 1]])
82-
return np.matmul(t_a_s, t_a_c)
83-
84-
85-
def calculate_residual(m, pts_world, pts_proj):
86-
pts_world_mat = np.append(np.asarray(pts_world), np.ones((len(pts_world), 1)), axis=1).T
87-
pts_proj_est = np.matmul(m, pts_world_mat)
88-
pts_proj_est_non_homo = pts_proj_est.T[:, 0:2] / pts_proj_est.T[:, [2, 2]]
89-
ssd_error = np.linalg.norm(pts_proj_est_non_homo - np.asarray(pts_proj))
90-
return np.sqrt(ssd_error)
9+
"""
10+
Problem Set - 3
11+
Problems: https://docs.google.com/document/d/1XsW9k_exgVwCy6CdwgUV3wLKwmFliVdmfAH74Ba4drc/pub?embedded=true
12+
"""
9113

9214

9315
def p1():

0 commit comments

Comments
 (0)