99import trimesh
1010from elf .io import open_file
1111from elf .wrapper .resized_volume import ResizedVolume
12+ from elf .wrapper .base import WrapperBase
13+ from elf .util import normalize_index , squeeze_singletons
1214from nifty .tools import blocking
1315from skimage .measure import marching_cubes , regionprops_table
16+ from skimage .transform import downscale_local_mean
1417from scipy .ndimage import binary_dilation
1518from tqdm import tqdm
1619
@@ -346,12 +349,35 @@ def compute_object_measures(
346349 measures .to_csv (output_table_path , sep = "\t " , index = False )
347350
348351
352+ # Refactor to elf?
353+ class ResizedVolumeLocalMean (WrapperBase ):
354+ def __init__ (self , volume , factors ):
355+ super ().__init__ (volume )
356+ self ._scale = factors
357+ self ._shape = tuple (int (np .ceil (s / f )) for s , f in zip (volume .shape , self ._scale ))
358+
359+ @property
360+ def shape (self ):
361+ return self ._shape
362+
363+ @property
364+ def scale (self ):
365+ return self ._scale
366+
367+ def __getitem__ (self , key ):
368+ index , to_squeeze = normalize_index (key , self .shape )
369+ index = tuple (slice (s .start * f , s .stop * f ) for s , f in zip (index , self ._scale ))
370+ out = self .volume [index ]
371+ out = downscale_local_mean (out , self ._scale )
372+ return squeeze_singletons (out , to_squeeze )
373+
374+
349375def compute_sgn_background_mask (
350376 image_path : str ,
351377 segmentation_path : str ,
352378 image_key : Optional [str ] = None ,
353379 segmentation_key : Optional [str ] = None ,
354- threshold_percentile : float = 35.0 ,
380+ threshold_percentile : int = 35 ,
355381 scale_factor : Tuple [int , int , int ] = (16 , 16 , 16 ),
356382 n_threads : Optional [int ] = None ,
357383 cache_path : Optional [str ] = None ,
@@ -388,7 +414,7 @@ def compute_sgn_background_mask(
388414 return mask
389415
390416 original_shape = image .shape
391- downsampled_shape = tuple (int (np .round (sh / sf )) for sh , sf in zip (original_shape , scale_factor ))
417+ downsampled_shape = tuple (int (np .ceil (sh / sf )) for sh , sf in zip (original_shape , scale_factor ))
392418
393419 low_res_mask = np .zeros (downsampled_shape , dtype = "bool" )
394420
@@ -399,7 +425,7 @@ def compute_sgn_background_mask(
399425 blocks = blocking ((0 , 0 , 0 ), downsampled_shape , chunk_shape )
400426 n_blocks = blocks .numberOfBlocks
401427
402- img_resized = ResizedVolume (image , downsampled_shape )
428+ img_resized = ResizedVolumeLocalMean (image , scale_factor )
403429 seg_resized = ResizedVolume (segmentation , downsampled_shape , order = 0 )
404430
405431 def _compute_block (block_id ):
0 commit comments