@@ -312,7 +312,9 @@ def mask_2d_via_pixel_coordinates_from(
312312 return buffed_mask_2d_from (mask_2d = mask_2d , buffer = buffer )
313313
314314
315- @numba_util .jit ()
315+ from scipy .ndimage import convolve
316+
317+
316318def blurring_mask_2d_from (
317319 mask_2d : np .ndarray , kernel_shape_native : Tuple [int , int ]
318320) -> np .ndarray :
@@ -348,32 +350,20 @@ def blurring_mask_2d_from(
348350
349351 """
350352
351- blurring_mask_2d = np .full (mask_2d .shape , True )
353+ # Create a (3, 3) kernel of ones
354+ kernel = np .ones (kernel_shape_native , dtype = np .uint8 )
352355
353- for y in range (mask_2d .shape [0 ]):
354- for x in range (mask_2d .shape [1 ]):
355- if not mask_2d [y , x ]:
356- for y1 in range (
357- (- kernel_shape_native [0 ] + 1 ) // 2 ,
358- (kernel_shape_native [0 ] + 1 ) // 2 ,
359- ):
360- for x1 in range (
361- (- kernel_shape_native [1 ] + 1 ) // 2 ,
362- (kernel_shape_native [1 ] + 1 ) // 2 ,
363- ):
364- if (
365- 0 <= x + x1 <= mask_2d .shape [1 ] - 1
366- and 0 <= y + y1 <= mask_2d .shape [0 ] - 1
367- ):
368- if mask_2d [y + y1 , x + x1 ]:
369- blurring_mask_2d [y + y1 , x + x1 ] = False
370- else :
371- raise exc .MaskException (
372- "setup_blurring_mask extends beyond the edge "
373- "of the mask - pad the datas array before masking"
374- )
375-
376- return blurring_mask_2d
356+ # Convolve the mask with the kernel, applying logical AND to maintain 'True' regions
357+ convolved_mask = convolve (mask_2d .astype (np .uint8 ), kernel , mode = "reflect" , cval = 0 )
358+
359+ # We want to return the mask where the convolved value is the full kernel size (i.e., 9 for a 3x3 kernel)
360+ result_mask = convolved_mask == np .prod (kernel_shape_native )
361+
362+ blurring_mask = ~ mask_2d + result_mask
363+
364+ print (blurring_mask * convolved_mask )
365+
366+ return blurring_mask
377367
378368
379369@numba_util .jit ()
0 commit comments