def Region_isSelected(self): if self.Key == 87 or self.Key == 119: # If 'W'/'w' - move up self.SelectedContour = hf.ShiftContour(self.SelectedContour, ToOrigin=False, ShiftBy=[0, -1]) elif self.Key == 65 or self.Key == 97: # If 'A'/'a' - move left self.SelectedContour = hf.ShiftContour(self.SelectedContour, ToOrigin=False, ShiftBy=[-1, 0]) elif self.Key == 83 or self.Key == 115: # If 'S'/'s' - move down self.SelectedContour = hf.ShiftContour(self.SelectedContour, ToOrigin=False, ShiftBy=[0, 1]) elif self.Key == 68 or self.Key == 100: # If 'D'/'d' - move right self.SelectedContour = hf.ShiftContour(self.SelectedContour, ToOrigin=False, ShiftBy=[1, 0])
def Region_isSelected(self): if self.Key == 87 or self.Key == 119: # If 'W'/'w' - move up for i in range(len(self.SelectedContours)): self.SelectedContours[i] = hf.ShiftContour(self.SelectedContours[i], ToOrigin=False, ShiftBy=[0, -1]) elif self.Key == 65 or self.Key == 97: # If 'A'/'a' - move left for i in range(len(self.SelectedContours)): self.SelectedContours[i] = hf.ShiftContour(self.SelectedContours[i], ToOrigin=False, ShiftBy=[-1, 0]) elif self.Key == 83 or self.Key == 115: # If 'S'/'s' - move down for i in range(len(self.SelectedContours)): self.SelectedContours[i] = hf.ShiftContour(self.SelectedContours[i], ToOrigin=False, ShiftBy=[0, 1]) elif self.Key == 68 or self.Key == 100: # If 'D'/'d' - move right for i in range(len(self.SelectedContours)): self.SelectedContours[i] = hf.ShiftContour(self.SelectedContours[i], ToOrigin=False, ShiftBy=[1, 0])
def GetSelectedRegionDetails(self): # Finding Selected regions bounding box, its contour shifted to origin, and its shifted mask image # Selected_BB = list(cv2.boundingRect(np.array(SelectedContour))) _, self.Selected_Mask, self.Selected_BB = hf.ShiftContour(self.SelectedContour, Get_Mask_BB=True) # Redrawing mask to conver any uncovered area OuterContour, _ = cv2.findContours(self.Selected_Mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) cv2.drawContours(self.Selected_Mask, OuterContour, -1, 255, -1)
def MagneticLassoTool(Canvas, window_title): hf.Clear() # Taking layer numbers user wants to copy Canvas.PrintLayerNames() layer_nos_to_copy = AskLayerNumsToCopy(-1, len(Canvas.layers) - 1) print( "\nPress 'Y' to confirm selection and copy it in a new layer else press 'N' to abort." ) print("You can also used the keys 'W', 'A', 'S', and 'D', to move the") print("selected region Up, Left, Down, and Right respectively.") # Clearing mouse buffer data (old mouse data) - this is a bug in OpenCV probably cv2.namedWindow(window_title) cv2.setMouseCallback(window_title, hf.EmptyCallBackFunc) Canvas.Show(Title=window_title) cv2.waitKey(1) # Setting mouse callback cv2.setMouseCallback(window_title, CallBackFunc_MagLassoTool) # Setting some params used in callback function global selecting, isSelected, CombinedFrame, FrameToShow, CanvasShape, SelectedContour, \ dij_src_F, dij_end_F, RunningPoints_F, FinalPoints_F, Weights, \ ROI_Frame, ROI_Shape, dij_src_roi, dij_end_roi, ROI_Rect, RunningPoints_roi, ROI_Weights selecting = False # True if region is being selected isSelected = False # True if region is selected Canvas.CombineLayers() CombinedFrame = Canvas.CombinedImage.copy( ) # the combined frame of the canvas FrameToShow = CombinedFrame.copy( ) # The frame which will be shown (with the selected region) CanvasShape = Canvas.Shape # Shape of the canvas SelectedContour = [] # Contour of the selected region RunningPoints_F = [] # The points that are being selected FinalPoints_F = [] # The points that are finalized dij_src_F = None # Strat point of dijstra's algo dij_end_F = None # End point of dijstra's algo Weights = None # Weights of all the neighbours for each pixel in the image # Same params wrt the ROI ROI_Frame, ROI_Shape, dij_src_roi, dij_end_roi, ROI_Rect, ROI_Weights = None, None, None, None, None, None RunningPoints_roi = [] IsAborted = False while True: # Showing canvas cv2.imshow(window_title, FrameToShow) Key = cv2.waitKey(1) if Key == 8: # If Backspace pressed if len(FinalPoints_F) > 1: PoppedEle = FinalPoints_F.pop() dij_src_F = PoppedEle RunningPoints_F.insert(0, PoppedEle) DrawPointsOnFrame() if Key == 89 or Key == 121: # If 'Y'/'y' - confirm if isSelected: # If the region is selected break else: # If the region is not selected yet print("\nSelect a region first to confirm.") continue elif Key == 78 or Key == 110: # If 'N'/'n' - abort IsAborted = True break # If the region is selected, check if the user is trying to move it if isSelected: if Key == 87 or Key == 119: # If 'W'/'w' - move up SelectedContour = hf.ShiftContour(SelectedContour, ToOrigin=False, ShiftBy=[0, -1]) if Key == 65 or Key == 97: # If 'A'/'a' - move left SelectedContour = hf.ShiftContour(SelectedContour, ToOrigin=False, ShiftBy=[-1, 0]) if Key == 83 or Key == 115: # If 'S'/'s' - move down SelectedContour = hf.ShiftContour(SelectedContour, ToOrigin=False, ShiftBy=[0, 1]) if Key == 68 or Key == 100: # If 'D'/'d' - move right SelectedContour = hf.ShiftContour(SelectedContour, ToOrigin=False, ShiftBy=[1, 0]) FrameToShow = CombinedFrame.copy() cv2.drawContours(FrameToShow, [np.array(SelectedContour)], -1, (127, 127, 127), 1) if not IsAborted: # Finding Selected regions bounding box, its contour shifted to origin, and its shifted mask image SelectedContourToOrigin, Selected_Mask, Selected_BB = hf.ShiftContour( SelectedContour, Get_Mask_BB=True) # Redrawing mask to conver any uncovered area OuterContour, _ = cv2.findContours(Selected_Mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) cv2.drawContours(Selected_Mask, OuterContour, -1, 255, -1) # Extracting the selected regions and copying them in a new layer ExtractSelectedRegion(Canvas, Selected_BB, Selected_Mask, layer_nos_to_copy) else: print("\nRegion selection aborted.")