@@ -99,6 +99,37 @@ def was_key_pressed(self, key: str) -> bool:
9999 return False
100100
101101
102+ class DrawSingleLineWidget (object ):
103+ def __init__ (self , background_img , window_name , preview_scaling ):
104+ self .original_image = background_img
105+ self .clone = self .original_image .copy ()
106+ self .window_name = window_name
107+ self .preview_scaling = preview_scaling
108+
109+ cv2 .namedWindow (window_name )
110+ cv2 .setMouseCallback (window_name , self .extract_coordinates )
111+
112+ self .start_coordinate = None
113+ self .end_coordinate = None
114+
115+ def extract_coordinates (self , event , x , y , flags , parameters ):
116+ x = round (x / self .preview_scaling )
117+ y = round (y / self .preview_scaling )
118+
119+ if event == cv2 .EVENT_LBUTTONDOWN :
120+ self .start_coordinate = (x ,y )
121+
122+ elif event == cv2 .EVENT_LBUTTONUP :
123+ self .end_coordinate = (x ,y )
124+ self .clone = self .original_image .copy ()
125+ cv2 .line (self .clone , self .start_coordinate , self .end_coordinate , (36 ,255 ,12 ), 2 )
126+
127+ def show_image (self ):
128+ return self .clone
129+
130+ def get_result (self ):
131+ return [ self .start_coordinate , self .end_coordinate ]
132+
102133
103134class OpenCV_GUI (KeypressHandler ):
104135 """ High Level OpenCV GUI.
@@ -473,6 +504,33 @@ def play_beep():
473504 self .logger .warning ("Notification sound file not found (%s)" , self .params .notification_sound_file )
474505
475506
507+ def line_selector (self ,
508+ image : np .ndarray ,
509+ txt : str ):
510+ """ Line Selector Widget
511+
512+ Args:
513+ image (np.ndarray): background image
514+ txt (str): text to display
515+
516+ Returns:
517+ list: start and endpoint of line
518+ """
519+
520+ line_widget = DrawSingleLineWidget (image , self .window_name , self .monitor_preview_scaling )
521+ while True :
522+ self .set_background_image (line_widget .show_image (), copy_image = False )
523+ self .print_text (txt )
524+ self .print_text ("Press 'space' to continue" )
525+ ret = self .show (5 )
526+
527+ if self .was_any_accept_key_pressed () or any (ret == x for x in [ord (' ' ), 13 ]):
528+ result = line_widget .get_result ()
529+ if result [0 ] is not None and result [1 ] is not None :
530+ if abs (result [0 ][0 ] - result [1 ][0 ]) + abs (result [0 ][1 ] - result [1 ][1 ]) > 10 :
531+ return result
532+
533+
476534 def min_max_selector (self ,
477535 image_min : np .ndarray ,
478536 image_max : np .ndarray ,
0 commit comments