@@ -135,47 +135,29 @@ def get_top_left_coords(tensor):
135135
136136 def separate_regions (s , mask ):
137137 threshold = 0.3
138- max_iter = 100
139-
140- device = mask .device
141138 mask_bin = (mask > threshold ).float ()
142- mask = mask_bin .clone ()
143-
139+ mask_np = mask_bin .squeeze ().cpu ().numpy ().astype (np .uint8 )
140+
141+ import cv2
142+ n_labels , labels = cv2 .connectedComponents (mask_np )
144143 regions = []
145- kernel = torch .tensor ([[0. , 1. , 0. ],
146- [1. , 1. , 1. ],
147- [0. , 1. , 0. ]], device = device ).reshape (1 , 1 , 3 , 3 )
148-
149- while mask .sum () > 0 and len (regions ) < max_iter :
150- coords = (mask > 0 ).nonzero (as_tuple = False )[0 ]
151- y , x = coords [1 ], coords [2 ]
152-
153- seed = torch .zeros_like (mask )
154- seed [0 , y , x ] = 1.0
155-
156- region = seed .clone ()
157- prev = torch .zeros_like (region )
158-
159- while not torch .equal (region , prev ):
160- prev = region
161- region = FF .conv2d (region .unsqueeze (0 ), kernel , padding = 1 )[0 ]
162- region = (region > 0 ).float () * mask
163-
164- regions .append (region .clone ())
165-
166- mask = mask * (region == 0 ).float ()
167-
144+ for i in range (1 , n_labels ):
145+ region = (labels == i ).astype (np .float32 )
146+ region_tensor = torch .from_numpy (region ).to (mask .device ).unsqueeze (0 )
147+ regions .append (region_tensor )
148+
149+ # Filter lớn
168150 def is_large_enough (region ):
169151 coords = (region > 0 ).nonzero (as_tuple = False )
170152 if coords .numel () == 0 :
171153 return False
172154 y_min , x_min = coords .min (dim = 0 ).values [1 :]
173155 y_max , x_max = coords .max (dim = 0 ).values [1 :]
174156 return (y_max - y_min + 1 > 50 ) and (x_max - x_min + 1 > 50 )
175-
157+
176158 regions = [r for r in regions if is_large_enough (r )]
177159 regions_sorted = sorted (regions , key = s .get_top_left_coords )
178-
160+
179161 return (regions_sorted ,)
180162
181163class inpaint_crop :
@@ -184,7 +166,7 @@ def INPUT_TYPES(s):
184166 return {
185167 "required" : {
186168 "image" : ("IMAGE" ,),
187- "crop_size" : ([512 ,768 ,896 ,1024 ,1280 ], {"default" : 768 }),
169+ "crop_size" : ([512 ,768 ,896 ,1024 ,1280 , 1408 , 1536 , 1664 , 1792 , 1920 , 2048 ], {"default" : 1024 }),
188170 "extend" : ("FLOAT" , {"default" : 1.2 , "min" : 0 , "max" : 100 }),
189171 },
190172 "optional" : {
0 commit comments