1010import trimesh
1111from elf .io import open_file
1212from elf .wrapper .resized_volume import ResizedVolume
13+ from elf .wrapper .base import WrapperBase
14+ from elf .util import normalize_index , squeeze_singletons
1315from nifty .tools import blocking
1416from skimage .measure import marching_cubes , regionprops_table
17+ from skimage .transform import downscale_local_mean
1518from scipy .ndimage import binary_dilation
1619from tqdm import tqdm
1720
@@ -366,12 +369,35 @@ def compute_object_measures(
366369 measures .to_csv (output_table_path , sep = "\t " , index = False )
367370
368371
372+ # Refactor to elf?
373+ class ResizedVolumeLocalMean (WrapperBase ):
374+ def __init__ (self , volume , factors ):
375+ super ().__init__ (volume )
376+ self ._scale = factors
377+ self ._shape = tuple (int (np .ceil (s / f )) for s , f in zip (volume .shape , self ._scale ))
378+
379+ @property
380+ def shape (self ):
381+ return self ._shape
382+
383+ @property
384+ def scale (self ):
385+ return self ._scale
386+
387+ def __getitem__ (self , key ):
388+ index , to_squeeze = normalize_index (key , self .shape )
389+ index = tuple (slice (s .start * f , s .stop * f ) for s , f in zip (index , self ._scale ))
390+ out = self .volume [index ]
391+ out = downscale_local_mean (out , self ._scale )
392+ return squeeze_singletons (out , to_squeeze )
393+
394+
369395def compute_sgn_background_mask (
370396 image_path : str ,
371397 segmentation_path : str ,
372398 image_key : Optional [str ] = None ,
373399 segmentation_key : Optional [str ] = None ,
374- threshold_percentile : float = 35.0 ,
400+ threshold_percentile : int = 35 ,
375401 scale_factor : Tuple [int , int , int ] = (16 , 16 , 16 ),
376402 n_threads : Optional [int ] = None ,
377403 cache_path : Optional [str ] = None ,
@@ -408,7 +434,7 @@ def compute_sgn_background_mask(
408434 return mask
409435
410436 original_shape = image .shape
411- downsampled_shape = tuple (int (np .round (sh / sf )) for sh , sf in zip (original_shape , scale_factor ))
437+ downsampled_shape = tuple (int (np .ceil (sh / sf )) for sh , sf in zip (original_shape , scale_factor ))
412438
413439 low_res_mask = np .zeros (downsampled_shape , dtype = "bool" )
414440
@@ -419,7 +445,7 @@ def compute_sgn_background_mask(
419445 blocks = blocking ((0 , 0 , 0 ), downsampled_shape , chunk_shape )
420446 n_blocks = blocks .numberOfBlocks
421447
422- img_resized = ResizedVolume (image , downsampled_shape )
448+ img_resized = ResizedVolumeLocalMean (image , scale_factor )
423449 seg_resized = ResizedVolume (segmentation , downsampled_shape , order = 0 )
424450
425451 def _compute_block (block_id ):
0 commit comments