<s> from __future__ import division <EOL> import numpy as np <EOL> from . morph import get_structuring_elem <EOL> from . import _labeled <EOL> from . internal import _get_output <EOL> from . _filters import _checked_mode2int <EOL> import mahotas as mh <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:label>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def label ( array , Bc = None , out = None , output = None ) : <EOL> '''<STR_LIT>''' <EOL> output = _get_output ( array , out , '<STR_LIT>' , np . int32 , output = output ) <EOL> output [ : ] = ( array != <NUM_LIT:0> ) <EOL> Bc = get_structuring_elem ( output , Bc ) <EOL> nr_objects = _labeled . label ( output , Bc ) <EOL> return output , nr_objects <EOL> def relabel ( labeled , inplace = False ) : <EOL> '''<STR_LIT>''' <EOL> labeled = _as_labeled ( labeled , labeled , '<STR_LIT>' , inplace = inplace ) <EOL> n = _labeled . relabel ( labeled ) <EOL> return labeled , n <EOL> def is_same_labeling ( labeled0 , labeled1 ) : <EOL> '''<STR_LIT>''' <EOL> labeled0 = _convert_labeled ( labeled0 ) <EOL> labeled1 = _convert_labeled ( labeled1 ) <EOL> return _labeled . is_same_labeling ( labeled0 , labeled1 ) <EOL> def remove_regions ( labeled , regions , inplace = False ) : <EOL> '''<STR_LIT>''' <EOL> labeled = _as_labeled ( labeled , labeled , '<STR_LIT>' , inplace = inplace ) <EOL> regions = np . asarray ( regions , dtype = np . intc ) <EOL> regions = np . unique ( regions ) <EOL> _labeled . remove_regions ( labeled , regions ) <EOL> return labeled <EOL> def remove_regions_where ( labeled , conditions , inplace = False ) : <EOL> '''<STR_LIT>''' <EOL> regions , = np . where ( conditions ) <EOL> return remove_regions ( labeled , regions , inplace = inplace ) <EOL> def remove_bordering ( labeled , rsize = <NUM_LIT:1> , out = None , output = None ) : <EOL> '''<STR_LIT>''' <EOL> im = labeled <EOL> invalid = set ( ) <EOL> index = [ slice ( None , None , None ) for _ in range ( im . ndim ) ] <EOL> if type ( rsize ) is not tuple : <EOL> rsize = ( rsize , ) * im . ndim <EOL> for dim in range ( im . ndim ) : <EOL> for bordering in ( <EOL> slice ( rsize [ dim ] ) , <EOL> slice ( im . shape [ dim ] - rsize [ dim ] , None ) <EOL> ) : <EOL> index [ dim ] = bordering <EOL> for val in np . unique ( im [ tuple ( index ) ] . ravel ( ) ) : <EOL> if val != <NUM_LIT:0> : <EOL> invalid . add ( val ) <EOL> index [ dim ] = slice ( None , None , None ) <EOL> if out is None and output is not None : <EOL> import warnings <EOL> warnings . warn ( '<STR_LIT>' % '<STR_LIT>' , DeprecationWarning ) <EOL> out = output <EOL> if out is None : <EOL> out = im . copy ( ) <EOL> elif out is not im : <EOL> out [ : ] = im <EOL> for val in invalid : <EOL> out *= ( im != val ) <EOL> return out <EOL> def border ( labeled , i , j , Bc = None , out = None , always_return = True , output = None ) : <EOL> '''<STR_LIT>''' <EOL> Bc = get_structuring_elem ( labeled , Bc ) <EOL> output = _get_output ( labeled , out , '<STR_LIT>' , bool , output = output ) <EOL> output . fill ( False ) <EOL> return _labeled . border ( labeled , Bc , output , i , j , bool ( always_return ) ) <EOL> def borders ( labeled , Bc = None , out = None , output = None , mode = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> Bc = get_structuring_elem ( labeled , Bc ) <EOL> output = _get_output ( labeled , out , '<STR_LIT>' , bool , output = output ) <EOL> output . fill ( False ) <EOL> return _labeled . borders ( labeled , Bc , output , _checked_mode2int ( mode , <NUM_LIT:0.0> , '<STR_LIT>' ) ) <EOL> def bwperim ( bw , n = <NUM_LIT:4> , mode = "<STR_LIT>" ) : <EOL> '''<STR_LIT>''' <EOL> bw = ( bw != <NUM_LIT:0> ) <EOL> return bw & borders ( bw , n , mode = mode ) <EOL> def _as_labeled ( array , labeled , funcname , inplace = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if inplace == '<STR_LIT>' : <EOL> labeled = np . require ( labeled , dtype = np . intc , requirements = "<STR_LIT>" ) <EOL> elif not inplace : <EOL> labeled = np . array ( labeled , dtype = np . intc ) <EOL> elif labeled . dtype != np . intc or not labeled . flags . carray : <EOL> raise ValueError ( '<STR_LIT>' % funcname ) <EOL> if array . shape != labeled . shape : <EOL> raise ValueError ( '<STR_LIT>' % funcname ) <EOL> return labeled <EOL> def _convert_labeled ( labeled ) : <EOL> return np . require ( labeled , dtype = np . intc , requirements = "<STR_LIT>" ) <EOL> def labeled_sum ( array , labeled , minlength = None ) : <EOL> '''<STR_LIT>''' <EOL> labeled = _as_labeled ( array , labeled , '<STR_LIT>' ) <EOL> maxv = labeled . max ( ) + <NUM_LIT:1> <EOL> if minlength is not None : <EOL> maxv = max ( maxv , minlength ) <EOL> output = np . empty ( maxv , dtype = array . dtype ) <EOL> _labeled . labeled_sum ( array , labeled , output ) <EOL> return output <EOL> def labeled_max ( array , labeled ) : <EOL> '''<STR_LIT>''' <EOL> labeled = _as_labeled ( array , labeled , '<STR_LIT>' ) <EOL> maxv = labeled . max ( ) + <NUM_LIT:1> <EOL> output = np . empty ( maxv , dtype = array . dtype ) <EOL> _labeled . labeled_max_min ( array , labeled , output , True ) <EOL> return output <EOL> def labeled_min ( array , labeled ) : <EOL> '''<STR_LIT>''' <EOL> labeled = _as_labeled ( array , labeled , '<STR_LIT>' ) <EOL> maxv = labeled . max ( ) + <NUM_LIT:1> <EOL> output = np . empty ( maxv , dtype = array . dtype ) <EOL> _labeled . labeled_max_min ( array , labeled , output , False ) <EOL> return output <EOL> def labeled_size ( labeled ) : <EOL> '''<STR_LIT>''' <EOL> from . histogram import fullhistogram <EOL> return fullhistogram ( labeled . astype ( np . uint32 ) ) <EOL> _perimeter_magic = np . array ( [ <EOL> [ <NUM_LIT:10> , <NUM_LIT:2> , <NUM_LIT:10> ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:2> ] , <EOL> [ <NUM_LIT:10> , <NUM_LIT:2> , <NUM_LIT:10> ] ] , np . uint8 ) <EOL> _perimeter_values = None <EOL> def perimeter ( bwimage , n = <NUM_LIT:4> , mode = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> global _perimeter_values <EOL> perim = bwperim ( bwimage , n , mode ) <EOL> perim = perim . astype ( np . uint8 ) <EOL> histogram = mh . fullhistogram ( <EOL> mh . convolve ( perim , _perimeter_magic ) ) <EOL> if _perimeter_values is None : <EOL> _perimeter_values = np . zeros ( <NUM_LIT> , float ) <EOL> _perimeter_values [ [ <NUM_LIT:5> , <NUM_LIT:7> , <NUM_LIT:15> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] = <NUM_LIT:1> <EOL> _perimeter_values [ [ <NUM_LIT> , <NUM_LIT> ] ] = np . sqrt ( <NUM_LIT:2> ) <EOL> _perimeter_values [ [ <NUM_LIT> , <NUM_LIT> ] ] = ( <NUM_LIT:1> + np . sqrt ( <NUM_LIT:2> ) ) / <NUM_LIT:2> <EOL> size = min ( <NUM_LIT> , len ( histogram ) ) <EOL> return np . dot ( histogram [ : size ] , _perimeter_values [ : size ] ) <EOL> def bbox ( f , as_slice = False ) : <EOL> '''<STR_LIT>''' <EOL> import mahotas . _bbox <EOL> n = f . max ( ) <EOL> output = np . empty ( f . ndim * <NUM_LIT:2> * ( n + <NUM_LIT:1> ) , np . intp ) <EOL> output = mahotas . _bbox . bbox_labeled ( f , output ) <EOL> output = output . reshape ( ( n + <NUM_LIT:1> , <NUM_LIT:2> * f . ndim ) ) <EOL> if as_slice : <EOL> output = [ tuple ( [ slice ( s , e ) for s , e in r . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:2> ) ) ] ) for r in output ] <EOL> return output <EOL> def filter_labeled ( labeled , remove_bordering = False , min_size = None , max_size = None ) : <EOL> '''<STR_LIT>''' <EOL> from mahotas . labeled import remove_regions , labeled_size <EOL> import mahotas as mh <EOL> labeled = _as_labeled ( labeled , labeled , '<STR_LIT>' ) <EOL> if remove_bordering : <EOL> labeled = mh . labeled . remove_bordering ( labeled ) <EOL> labeled , nr = mh . labeled . relabel ( labeled ) <EOL> else : <EOL> nr = labeled . max ( ) <EOL> to_keep = np . ones ( nr + <NUM_LIT:1> , bool ) <EOL> if min_size is not None or max_size is not None : <EOL> sizes = labeled_size ( labeled ) <EOL> if min_size : <EOL> to_keep &= ( sizes >= min_size ) <EOL> if max_size : <EOL> to_keep &= ( sizes <= max_size ) <EOL> to_keep [ <NUM_LIT:0> ] = True <EOL> to_remove = np . where ( ~ to_keep ) <EOL> labeled = remove_regions ( labeled , to_remove ) <EOL> labeled , nr = mh . labeled . relabel ( labeled , inplace = True ) <EOL> return labeled , nr </s>
