def remove_small_objects(ar, min_size=64, connectivity=1, in_place=False):
 _check_dtype_supported(ar)
 if in_place:
  out = ar
 else:
  out = ar.copy()
 if (min_size == 0):
  return out
 if (out.dtype == bool):
  selem = ndi.generate_binary_structure(ar.ndim, connectivity)
  ccs = np.zeros_like(ar, dtype=np.int32)
  ndi.label(ar, selem, output=ccs)
 else:
  ccs = out
 try:
  component_sizes = np.bincount(ccs.ravel())
 except ValueError:
  raise ValueError('Negative value labels are not supported. Try relabeling the input with `scipy.ndimage.label` or `skimage.morphology.label`.')
 if (len(component_sizes) == 2):
  warn('Only one label was provided to `remove_small_objects`. Did you mean to use a boolean array?')
 too_small = (component_sizes < min_size)
 too_small_mask = too_small[ccs]
 out[too_small_mask] = 0
 return out

def remove_small_objects ( ar , min_size = 64 , connectivity = 1 , in_place = False ) : _check_dtype_supported ( ar ) if in_place : out = ar else : out = ar . copy ( ) if ( min_size == 0 ) : return out if ( out . dtype == bool ) : selem = ndi . generate_binary_structure ( ar . ndim , connectivity ) ccs = np . zeros_like ( ar , dtype = np . int32 ) ndi . label ( ar , selem , output = ccs ) else : ccs = out try : component_sizes = np . bincount ( ccs . ravel ( ) ) except ValueError : raise ValueError ( 'Negative value labels are not supported. Try relabeling the input with `scipy.ndimage.label` or `skimage.morphology.label`.' ) if ( len ( component_sizes ) == 2 ) : warn ( 'Only one label was provided to `remove_small_objects`. Did you mean to use a boolean array?' ) too_small = ( component_sizes < min_size ) too_small_mask = too_small [ ccs ] out [ too_small_mask ] = 0 return out
remove connected components smaller than the specified size .