Skip to content

Commit 8ce79ad

Browse files
author
Yash Srivastava
committed
Add height
1 parent 0cd1573 commit 8ce79ad

File tree

3 files changed

+239
-55
lines changed

3 files changed

+239
-55
lines changed

Assignment-3/Q1/1.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import matplotlib
2+
matplotlib.use('TkAgg')
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
import PIL
6+
from PIL import Image
7+
8+
def get_input_lines(im, min_lines=3):
9+
10+
n = 0
11+
lines = np.zeros((3, 0))
12+
centers = np.zeros((3, 0))
13+
14+
plt.figure()
15+
plt.imshow(im)
16+
print('Set at least %d lines to compute vanishing point' % min_lines)
17+
while True:
18+
print('Click the two endpoints, use the right key to undo, and use the middle key to stop input')
19+
clicked = plt.ginput(2, timeout=0, show_clicks=True)
20+
if not clicked or len(clicked) < 2:
21+
if n < min_lines:
22+
print('Need at least %d lines, you have %d now' % (min_lines, n))
23+
continue
24+
else:
25+
# Stop getting lines if number of lines is enough
26+
break
27+
28+
# Unpack user inputs and save as homogeneous coordinates
29+
pt1 = np.array([clicked[0][0], clicked[0][1], 1])
30+
pt2 = np.array([clicked[1][0], clicked[1][1], 1])
31+
# Get line equation using cross product
32+
# Line equation: line[0] * x + line[1] * y + line[2] = 0
33+
line = np.cross(pt1, pt2)
34+
lines = np.append(lines, line.reshape((3, 1)), axis=1)
35+
# Get center coordinate of the line segment
36+
center = (pt1 + pt2) / 2
37+
centers = np.append(centers, center.reshape((3, 1)), axis=1)
38+
39+
# Plot line segment
40+
plt.plot([pt1[0], pt2[0]], [pt1[1], pt2[1]], color='b')
41+
42+
n += 1
43+
44+
return n, lines, centers
45+
46+
def plot_lines_and_vp(im, lines, vp):
47+
48+
bx1 = min(1, vp[0] / vp[2]) - 10
49+
bx2 = max(im.shape[1], vp[0] / vp[2]) + 10
50+
by1 = min(1, vp[1] / vp[2]) - 10
51+
by2 = max(im.shape[0], vp[1] / vp[2]) + 10
52+
53+
plt.figure()
54+
plt.imshow(im)
55+
for i in range(lines.shape[1]):
56+
if lines[0, i] < lines[1, i]:
57+
pt1 = np.cross(np.array([1, 0, -bx1]), lines[:, i])
58+
pt2 = np.cross(np.array([1, 0, -bx2]), lines[:, i])
59+
else:
60+
pt1 = np.cross(np.array([0, 1, -by1]), lines[:, i])
61+
pt2 = np.cross(np.array([0, 1, -by2]), lines[:, i])
62+
pt1 = pt1 / pt1[2]
63+
pt2 = pt2 / pt2[2]
64+
plt.plot([pt1[0], pt2[0]], [pt1[1], pt2[1]], 'g')
65+
66+
print(float(vp[0]) , vp[2], " ", float(vp[1]) , vp[2])
67+
68+
plt.plot(float(vp[0]) / vp[2], float(vp[1]) / vp[2], 'ro')
69+
plt.show()
70+
71+
def get_top_and_bottom_coordinates(im, obj):
72+
73+
plt.figure()
74+
plt.imshow(im)
75+
76+
print('Click on the top coordinate of %s' % obj)
77+
clicked = plt.ginput(1, timeout=0, show_clicks=True)
78+
x1, y1 = clicked[0]
79+
# Uncomment this line to enable a vertical line to help align the two coordinates
80+
# plt.plot([x1, x1], [0, im.shape[0]], 'b')
81+
print('Click on the bottom coordinate of %s' % obj)
82+
clicked = plt.ginput(1, timeout=0, show_clicks=True)
83+
x2, y2 = clicked[0]
84+
85+
plt.plot([x1, x2], [y1, y2], 'b')
86+
87+
return np.array([[x1, x2], [y1, y2], [1, 1]])
88+
89+
def get_vanishing_point(lines):
90+
91+
vp = np.zeros((3, 1))
92+
#vp[0] = (lines[0])/(-1*lines[2])
93+
#vp[1] = (lines[1])/(-1*lines[3])
94+
#vp[0:1] = (lines[0:1,:])/(-1*lines[2,:])
95+
b = []
96+
print(lines)
97+
for i in range (0,2):
98+
b.append(lines[i])
99+
c = lines[2]
100+
#print(lines.shape())
101+
102+
b = np.array(b)
103+
c = np.array(c)
104+
#b = b.reshape(1,-1)
105+
c = c.reshape(1,-1)
106+
print ("c",c)
107+
print ("b",b)
108+
109+
b = b.T
110+
c = c.T
111+
print ("c_new",c)
112+
print ("b_new",b)
113+
#print("b and c",b.T," ", c)
114+
d = np.matmul(b.T,c)
115+
print ("d",d)
116+
117+
a = np.matmul(np.linalg.inv(np.matmul(np.transpose(b),b)), np.matmul(b.T,c))
118+
# print (c)
119+
# print (b)
120+
print ("a",a)
121+
vp[2] = 1
122+
pass
123+
124+
def get_horizon_line(vpts):
125+
126+
print("Hello!")
127+
print("vpts",vpts)
128+
horizon = real(np.matmul(np.transpose(vpts[2]), np.transpose(vpts[0])))
129+
length = sqrt(horizon[0]^2 + horizon[1]^2)
130+
horizon = horizon/length
131+
print(horizon)
132+
pass
133+
134+
def plot_horizon_line(im,vpts):
135+
136+
plt.figure()
137+
plt.imshow(im)
138+
plt.plot([pt1[0], pt2[0]], [pt1[1], pt2[1]], color='b')
139+
plt.show()
140+
141+
pass
142+
143+
im = np.asarray(Image.open('img1.jpg'))
144+
145+
# Part 1
146+
# Get vanishing points for each of the directions
147+
num_vpts = 3
148+
vpts = np.zeros((3, num_vpts))
149+
150+
for i in range(num_vpts):
151+
152+
print('Getting vanishing point %d' % i)
153+
# Get at least three lines from user input
154+
n, lines, centers = get_input_lines(im)
155+
# <YOUR IMPLEMENTATION> Solve for vanishing point
156+
vpts[:, i] = get_vanishing_point(lines)
157+
# Plot the lines and the vanishing point
158+
plot_lines_and_vp(im, lines, vpts[:, i])
159+
160+
# <YOUR IMPLEMENTATION> Get the ground horizon line
161+
horizon_line = get_horizon_line(vpts)
162+
# <YOUR IMPLEMENTATION> Plot the ground horizon line
163+
plot_horizon_line(im,vpts)
164+
165+
# Part 2
166+
# <YOUR IMPLEMENTATION> Solve for the camera parameters (f, u, v)
167+
f, u, v = get_camera_parameters()

Assignment-3/Q1/img1.jpg

3.58 MB
Loading

Assignment-3/Q2/height.py

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
import cv2
22
import math
3-
from time import time
43

54
boxes = []
6-
7-
xCount = 0
8-
yCount = 0
95
iter = 0
106

117
def on_mouse(event, x, y, flags, params):
128

139
global iter
14-
t = time()
1510

1611
if event == cv2.EVENT_LBUTTONDOWN:
17-
print ('Start Mouse Position: '+str(x)+', '+str(y))
18-
sbox = (x, y)
19-
boxes.append(sbox)
12+
13+
print ('Start Mouse Position: ' + str(x) + ', ' + str(y))
14+
15+
start_point = (x, y)
16+
boxes.append(start_point)
2017

2118
elif event == cv2.EVENT_LBUTTONUP:
19+
2220
print ('End Mouse Position: '+str(x)+', '+str(y))
23-
ebox = (x, y)
24-
boxes.append(ebox)
21+
22+
end_point = (x, y)
23+
boxes.append(end_point)
24+
2525
cv2.line(img, boxes[-1], boxes[-2],(0,255,0),10)
26-
# print boxes
26+
2727
iter += 1
28-
# print iter
2928

3029

3130
def line_intersection(line1, line2):
@@ -37,12 +36,14 @@ def det(a, b):
3736
return a[0] * b[1] - a[1] * b[0]
3837

3938
div = det(xdiff, ydiff)
39+
4040
if div == 0:
4141
raise Exception('lines do not intersect')
4242

4343
d = (det(*line1), det(*line2))
4444
x = det(d, xdiff) / div
4545
y = det(d, ydiff) / div
46+
4647
return x, y
4748

4849
def norm(point1, point2):
@@ -51,78 +52,94 @@ def norm(point1, point2):
5152
ydiff = point1[1] - point2[1]
5253

5354
norm = math.sqrt(xdiff*xdiff + ydiff*ydiff)
54-
# print norm
55-
return norm
5655

56+
return norm
5757

5858
print ("-------------------------INSTRUCTIONS----------------------------")
5959
print ("Draw 8 line segments, holding mouse while drawing")
60-
print ("First two for xVanish")
61-
print ("Next two for yVanish")
62-
'''print "Next two for objects whose lengths are to be compared"
63-
print "First draw for shorter object in image plane starting from bottom"
64-
print "Then for other object again starting from bottom"
65-
print "Finally two for zVanish"
66-
print "-----------------------------END---------------------------------"'''
60+
print ("First two lines for a pair of parallel lines")
61+
print ("Next two lines for another pair of parallel lines")
62+
print ("Finally two lines for a third pair of parallel lines to find Vz")
63+
print ("Now, two lines for objects whose lengths are to be compared")
64+
print ("First draw line for shorter object in image plane starting from bottom")
65+
print ("Then for other object again starting from bottom")
66+
print ("-----------------------------END---------------------------------")
67+
font = cv2.FONT_HERSHEY_SIMPLEX
6768

6869
count = 0
6970
img = cv2.imread('img2.jpg')
70-
# img = cv2.blur(img, (3,3))
71-
# img = cv2.resize(img, None, fx = 0.8,fy = 0.8)
71+
number_of_objects = int(raw_input("Number of Objects to be measured:"))
72+
7273
while(1):
73-
# print count
74-
if iter == 8:
75-
break
74+
75+
#if iter == 7 + number_of_objects:
76+
# break
7677

7778
count += 1
7879

7980
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
80-
# cv2.resizeWindow('image', 600,600)
8181
cv2.setMouseCallback('image', on_mouse, 0)
8282
cv2.imshow('image', img)
8383

8484
if count < 50:
85+
8586
if cv2.waitKey(33) == 27:
87+
8688
cv2.destroyAllWindows()
8789
break
90+
8891
elif count >= 50:
8992
count = 0
93+
9094
if iter==2:
91-
xVanish = line_intersection( [boxes[0],boxes[1]], [boxes[2],boxes[3]] )
92-
cv2.circle(img,xVanish,10,(0,0,255),20)
93-
print("XPOINT")
95+
96+
parallel_1 = line_intersection( [boxes[0],boxes[1]], [boxes[2],boxes[3]] )
97+
cv2.circle(img,parallel_1,10,(0,0,255),20)
98+
9499
if iter==4:
95-
yVanish = line_intersection( [boxes[4],boxes[5]], [boxes[6],boxes[7]] )
96-
cv2.circle(img,yVanish,10,(0,0,255),10)
97-
x = img.shape[0]
98-
y = ((xVanish[1]-yVanish[1])*(x-yVanish[0])/(xVanish[0]-yVanish[0]))+yVanish[1]
99-
cv2.line(img, (x,y), yVanish,(0,255,0),20)
100-
print("XPOINT")
101100

102-
# print (xVanish)
101+
parallel_2 = line_intersection( [boxes[4],boxes[5]], [boxes[6],boxes[7]] )
102+
cv2.circle(img,parallel_2,10,(0,0,255),10)
103+
104+
x1 = img.shape[0]
105+
y1 = ((parallel_1[1]-parallel_2[1])*(x1-parallel_2[0])/(parallel_1[0]-parallel_2[0])) + parallel_2[1]
106+
107+
x2 = 0
108+
y2 = ((parallel_1[1]-parallel_2[1])*(x2-parallel_2[0])/(parallel_1[0]-parallel_2[0])) + parallel_2[1]
109+
110+
cv2.line(img, (x1,y1), (x2,y2), (0,255,0), 20)
111+
112+
parallel_1 = line_intersection( [boxes[0],boxes[1]], [boxes[2],boxes[3]] )
113+
print (parallel_1)
114+
115+
parallel_2 = line_intersection( [boxes[4],boxes[5]], [boxes[6],boxes[7]] )
116+
print (parallel_2)
103117

104-
xVanish = line_intersection( [boxes[0],boxes[1]], [boxes[2],boxes[3]] )
105-
print (xVanish)
118+
Vz_Parallel = line_intersection( [boxes[8],boxes[9]], [boxes[10],boxes[11]] )
119+
print (Vz_Parallel)
106120

107-
yVanish = line_intersection( [boxes[4],boxes[5]], [boxes[6],boxes[7]] )
108-
print (yVanish)
121+
for i in range(0,number_of_objects):
109122

110-
zVanish = line_intersection( [boxes[12],boxes[13]], [boxes[14],boxes[15]] )
111-
print (zVanish)
123+
print ("Assuming bottom is given as first input for each object")
124+
vertex = line_intersection( [parallel_1,parallel_2], [boxes[12],boxes[13+(2*i)+1]] )
112125

113-
print ("Assuming bottom is given as first input for each object")
114-
vertex = line_intersection( [xVanish,yVanish], [boxes[8],boxes[10]] )
126+
bot = boxes[13+(2*i)+1]
127+
ref = line_intersection( [vertex,boxes[13]], [boxes[13+(2*i)+1],boxes[13+(2*i)+2]] )
128+
top = boxes[13+(2*i)+2]
115129

116-
bot = boxes[10]
117-
ref = line_intersection( [vertex,boxes[9]], [boxes[10],boxes[11]] )
118-
top = boxes[11]
130+
response1 = float(raw_input("Please enter height of shorter object, enter 0 if unknown: "))
131+
response2 = float(raw_input("Please enter height of other object, enter 0 if unknown: "))
119132

120-
response1 = float(raw_input("Please enter height of shorter object, enter 0 if unknown: "))
121-
response2 = float(raw_input("Please enter height of other object, enter 0 if unknown: "))
133+
response = response1 + response2
122134

123-
response = response1 + response2
124-
# print "Assuming Vz at infinity"
125-
# print response
135+
print ("Length of unknown object is")
136+
print (( (norm(top,bot)/norm(ref,bot))*(norm(Vz_Parallel,ref)/norm(Vz_Parallel,top))*response ) )
137+
value = str(( (norm(top,bot)/norm(ref,bot))*(norm(Vz_Parallel,ref)/norm(Vz_Parallel,top))*response ) )
138+
cv2.putText(img,value,top, font, 4,(0,0,0),10,cv2.LINE_AA)
126139

127-
print ("Length of unknown object is")
128-
print (( (norm(top,bot)/norm(ref,bot))*(norm(zVanish,ref)/norm(zVanish,top))*response ) )
140+
cv2.namedWindow('image1',cv2.WINDOW_NORMAL)
141+
cv2.imshow('image1', img)
142+
k=0
143+
while k!=27:
144+
k = cv2.waitKey(33) # Esc key to stop
145+

0 commit comments

Comments
 (0)