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