11import cv2
22import math
3- from time import time
43
54boxes = []
6-
7- xCount = 0
8- yCount = 0
95iter = 0
106
117def 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
3130def 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
4849def 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
5858print ("-------------------------INSTRUCTIONS----------------------------" )
5959print ("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
6869count = 0
6970img = 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+
7273while (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