1+ import numpy as np
2+ import cv2 as cv
3+ import random
4+
5+ def mouse_event_handler (event , x , y , flags , param ):
6+ if event == cv .EVENT_LBUTTONDOWN :
7+ param .append ((x , y ))
8+
9+ def draw_straight_line (img , line , color , thickness = 1 ):
10+ assert img .ndim >= 2
11+ h , w , * _ = img .shape
12+ a , b , c = line # Line: ax + by + c = 0
13+ if abs (a ) > abs (b ):
14+ pt1 = (int (c / - a ), 0 )
15+ pt2 = (int ((b * h + c ) / - a ), h )
16+ else :
17+ pt1 = (0 , int (c / - b ))
18+ pt2 = (w , int ((a * w + c ) / - b ))
19+ cv .line (img , pt1 , pt2 , color , thickness )
20+
21+ if __name__ == '__main__' :
22+ # Load two images
23+ img1 = cv .imread ('../data/KITTI07/image_0/000000.png' , cv .IMREAD_COLOR )
24+ img2 = cv .imread ('../data/KITTI07/image_0/000023.png' , cv .IMREAD_COLOR )
25+ assert (img1 is not None ) and (img2 is not None ), 'Cannot read the given images'
26+ # Note) `F` is derived from `fundamental_mat_estimation.py`.
27+ F = np .array ([[ 3.34638533e-07 , 7.58547151e-06 , - 2.04147752e-03 ],
28+ [- 5.83765868e-06 , 1.36498636e-06 , 2.67566877e-04 ],
29+ [ 1.45892349e-03 , - 4.37648316e-03 , 1.00000000e+00 ]])
30+
31+ # Register event handlers and show images
32+ wnd1_name , wnd2_name = 'Epipolar Line: Image #1' , 'Epipolar Line: Image #2'
33+ img1_pts , img2_pts = [], []
34+ cv .namedWindow (wnd1_name )
35+ cv .namedWindow (wnd2_name )
36+ cv .setMouseCallback (wnd1_name , mouse_event_handler , img1_pts )
37+ cv .setMouseCallback (wnd2_name , mouse_event_handler , img2_pts )
38+ cv .imshow (wnd1_name , img1 )
39+ cv .imshow (wnd2_name , img2 )
40+
41+ # Get a point from a image and draw its correponding epipolar line on the other image
42+ while True :
43+ if len (img1_pts ) > 0 :
44+ for x , y in img1_pts :
45+ color = (random .randrange (256 ), random .randrange (256 ), random .randrange (256 ))
46+ cv .circle (img1 , (x , y ), 4 , color , - 1 )
47+ epipolar_line = F @ [[x ], [y ], [1 ]]
48+ draw_straight_line (img2 , epipolar_line , color , 2 )
49+ img1_pts .clear ()
50+ if len (img2_pts ) > 0 :
51+ for x , y in img2_pts :
52+ color = (random .randrange (256 ), random .randrange (256 ), random .randrange (256 ))
53+ cv .circle (img2 , (x , y ), 4 , color , - 1 )
54+ epipolar_line = F .T @ [[x ], [y ], [1 ]]
55+ draw_straight_line (img1 , epipolar_line , color , 2 )
56+ img2_pts .clear ()
57+ cv .imshow (wnd2_name , img2 )
58+ cv .imshow (wnd1_name , img1 )
59+ key = cv .waitKey (10 )
60+ if key == 27 : # ESC
61+ break
62+
63+ cv .destroyAllWindows ()
0 commit comments