def CallBackFunc(self, event, x, y, flags, params): # If while selecting the region, mouse goes out of the frame, then clip it position # to the nearest corner/edge of the frame if self.selecting and self.CorrectXYWhileSelecting: x, y = hf.Correct_xy_While_Selecting(x, y, [0, self.CanvasShape[1]-1], [0, self.CanvasShape[0]-1]) # Storing mouse pointer values to self variable for access outside the function self.x, self.y = x, y # Starts selecting - Left button is pressed down if event == cv2.EVENT_LBUTTONDOWN or event == cv2.EVENT_RBUTTONDOWN: self.selecting = True self.isSelected = False self.SetRegionMask_Selecting() self.SetCanvasFrame() # Selecting the region elif event == cv2.EVENT_MOUSEMOVE: if self.selecting: self.SetRegionMask_Selecting() self.SetCanvasFrame() # Stop selecting the layer. elif event == cv2.EVENT_LBUTTONUP or event == cv2.EVENT_RBUTTONUP: self.selecting = False self.isSelected = True self.ApplyGrabcut(ItCount=1) self.SetCanvasFrame() # Reset region masks elif event == cv2.EVENT_LBUTTONDBLCLK: self.selecting = False self.isSelected = False self.RegionMask = np.ones((self.CanvasShape[0], self.CanvasShape[1], 1), dtype=np.uint8) * cv2.GC_PR_BGD self.SetCanvasFrame()
def CallBackFunc(self, event, x, y, flags, params): # If while selecting the region, mouse goes out of the frame, then clip it position # to the nearest corner/edge of the frame if self.selecting: self.x, self.y = hf.Correct_xy_While_Selecting( self.x, self.y, [0, self.CanvasShape[1] - 1], [0, self.CanvasShape[0] - 1]) # Storing mouse pointer values to self variable for access outside the function self.x, self.y = x, y # Starts selecting - Left button is pressed down if event == cv2.EVENT_FLAG_LBUTTON: self.selecting = True if self.isSelected: # If already selected, start drawing a new one self.isSelected = False self.Selected_Points = [] self.Selected_Points.append([[x, y]]) # Selecting the region elif event == cv2.EVENT_MOUSEMOVE: if self.selecting: self.SetCanvasFrame() # Stop selecting the layer. elif event == cv2.EVENT_LBUTTONDBLCLK: self.isSelected = True self.selecting = False if len(self.Selected_Points) <= 2: self.Selected_Points = [] self.isSelected = False else: self.Selected_Points.append([[x, y]]) self.SetCanvasFrame()
def CallBackFunc(self, event, x, y, flags, params): # If while selecting the region, mouse goes out of the frame, then clip it position # to the nearest corner/edge of the frame if self.selecting and self.CorrectXYWhileSelecting: x, y = hf.Correct_xy_While_Selecting(x, y, [0, self.CanvasShape[1] - 1], [0, self.CanvasShape[0] - 1]) # Storing mouse pointer values to self variable for access outside the function self.x, self.y = x, y # Starts selecting - Left button is pressed down if event == cv2.EVENT_LBUTTONDOWN: self.selecting = True self.isSelected = False self.Mouse_EVENT_LBUTTONDOWN() # Selecting the region elif event == cv2.EVENT_MOUSEMOVE: if self.selecting: self.Mouse_EVENT_MOUSEMOVE_selecting() self.SetCanvasFrame() # Stop selecting the layer. elif event == cv2.EVENT_LBUTTONUP: self.selecting = False self.isSelected = True self.Mouse_EVENT_LBUTTONUP() self.SetCanvasFrame() else: self.Other_MouseEvents(event)
def CallBackFunc_MagLassoTool(event, x, y, flags, params): # Taking global params global selecting, isSelected, CombinedFrame, FrameToShow, CanvasShape, SelectedContour, \ dij_src_F, dij_end_F, RunningPoints_F, FinalPoints_F, RunningPoints_roi # If while selecting the region, mouse goes out of the frame, then clip it position # to the nearest corner/edge of the frame if selecting: x, y = hf.Correct_xy_While_Selecting(x, y, [0, CanvasShape[1] - 1], [0, CanvasShape[0] - 1]) # Setting dijsktra's end point to mouse cursor's current coordinate dij_end_F = [x, y] # Starts selecting - Left button is pressed down if event == cv2.EVENT_FLAG_LBUTTON: if isSelected: isSelected = False selecting = True dij_src_F = [x, y] RunningPoints_F = [] RunningPoints_roi = [] FinalPoints_F = [] SelectedContour = [] else: if not selecting: selecting = True dij_src_F = [x, y] RunningPoints_F = [] RunningPoints_roi = [] FinalPoints_F = [] else: # Add shortest path to final points FinalPoints_F += RunningPoints_F[:-15] dij_src_F = RunningPoints_F[-15] #[x, y] # Selecting the region elif event == cv2.EVENT_MOUSEMOVE: if selecting: # Call dijsktra's Dij_ShortestPath() # Draw selected and running DrawPointsOnFrame() # Stop selecting the layer. elif event == cv2.EVENT_LBUTTONDBLCLK: isSelected = True selecting = False # Check final points size here if len(FinalPoints_F) <= 2: FinalPoints_F = [] isSelected = False # Add last running points to final points FinalPoints_F += RunningPoints_F # Convert finalPoints to Selected Contour SelectedContour = CvtPointsToContour(FinalPoints_F) # Draw contour FrameToShow = CombinedFrame.copy() cv2.drawContours(FrameToShow, [np.array(SelectedContour)], -1, (127, 127, 127), 1)