<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from scipy import ndimage as ndi <EOL> from . _skeletonize_cy import _fast_skeletonize , _skeletonize_loop , _table_lookup_index <EOL> def skeletonize ( image ) : <EOL> """<STR_LIT>""" <EOL> image = image . astype ( np . uint8 ) <EOL> if image . ndim != <NUM_LIT:2> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not np . all ( np . in1d ( image . flat , ( <NUM_LIT:0> , <NUM_LIT:1> ) ) ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return _fast_skeletonize ( image ) <EOL> _eight_connect = ndi . generate_binary_structure ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> def medial_axis ( image , mask = None , return_distance = False ) : <EOL> """<STR_LIT>""" <EOL> global _eight_connect <EOL> if mask is None : <EOL> masked_image = image . astype ( np . bool ) <EOL> else : <EOL> masked_image = image . astype ( bool ) . copy ( ) <EOL> masked_image [ ~ mask ] = False <EOL> center_is_foreground = ( np . arange ( <NUM_LIT> ) & <NUM_LIT:2> ** <NUM_LIT:4> ) . astype ( bool ) <EOL> table = ( center_is_foreground <EOL> & <EOL> ( np . array ( [ ndi . label ( _pattern_of ( index ) , _eight_connect ) [ <NUM_LIT:1> ] != <EOL> ndi . label ( _pattern_of ( index & ~ <NUM_LIT:2> ** <NUM_LIT:4> ) , <EOL> _eight_connect ) [ <NUM_LIT:1> ] <EOL> for index in range ( <NUM_LIT> ) ] ) <EOL> | <EOL> np . array ( [ np . sum ( _pattern_of ( index ) ) < <NUM_LIT:3> for index in range ( <NUM_LIT> ) ] ) ) <EOL> ) <EOL> distance = ndi . distance_transform_edt ( masked_image ) <EOL> if return_distance : <EOL> store_distance = distance . copy ( ) <EOL> cornerness_table = np . array ( [ <NUM_LIT:9> - np . sum ( _pattern_of ( index ) ) <EOL> for index in range ( <NUM_LIT> ) ] ) <EOL> corner_score = _table_lookup ( masked_image , cornerness_table ) <EOL> i , j = np . mgrid [ <NUM_LIT:0> : image . shape [ <NUM_LIT:0> ] , <NUM_LIT:0> : image . shape [ <NUM_LIT:1> ] ] <EOL> result = masked_image . copy ( ) <EOL> distance = distance [ result ] <EOL> i = np . ascontiguousarray ( i [ result ] , dtype = np . intp ) <EOL> j = np . ascontiguousarray ( j [ result ] , dtype = np . intp ) <EOL> result = np . ascontiguousarray ( result , np . uint8 ) <EOL> generator = np . random . RandomState ( <NUM_LIT:0> ) <EOL> tiebreaker = generator . permutation ( np . arange ( masked_image . sum ( ) ) ) <EOL> order = np . lexsort ( ( tiebreaker , <EOL> corner_score [ masked_image ] , <EOL> distance ) ) <EOL> order = np . ascontiguousarray ( order , dtype = np . int32 ) <EOL> table = np . ascontiguousarray ( table , dtype = np . uint8 ) <EOL> _skeletonize_loop ( result , i , j , order , table ) <EOL> result = result . astype ( bool ) <EOL> if not mask is None : <EOL> result [ ~ mask ] = image [ ~ mask ] <EOL> if return_distance : <EOL> return result , store_distance <EOL> else : <EOL> return result <EOL> def _pattern_of ( index ) : <EOL> """<STR_LIT>""" <EOL> return np . array ( [ [ index & <NUM_LIT:2> ** <NUM_LIT:0> , index & <NUM_LIT:2> ** <NUM_LIT:1> , index & <NUM_LIT:2> ** <NUM_LIT:2> ] , <EOL> [ index & <NUM_LIT:2> ** <NUM_LIT:3> , index & <NUM_LIT:2> ** <NUM_LIT:4> , index & <NUM_LIT:2> ** <NUM_LIT:5> ] , <EOL> [ index & <NUM_LIT:2> ** <NUM_LIT:6> , index & <NUM_LIT:2> ** <NUM_LIT:7> , index & <NUM_LIT:2> ** <NUM_LIT:8> ] ] , bool ) <EOL> def _table_lookup ( image , table ) : <EOL> """<STR_LIT>""" <EOL> if image . shape [ <NUM_LIT:0> ] < <NUM_LIT:3> or image . shape [ <NUM_LIT:1> ] < <NUM_LIT:3> : <EOL> image = image . astype ( bool ) <EOL> indexer = np . zeros ( image . shape , int ) <EOL> indexer [ <NUM_LIT:1> : , <NUM_LIT:1> : ] += image [ : - <NUM_LIT:1> , : - <NUM_LIT:1> ] * <NUM_LIT:2> ** <NUM_LIT:0> <EOL> indexer [ <NUM_LIT:1> : , : ] += image [ : - <NUM_LIT:1> , : ] * <NUM_LIT:2> ** <NUM_LIT:1> <EOL> indexer [ <NUM_LIT:1> : , : - <NUM_LIT:1> ] += image [ : - <NUM_LIT:1> , <NUM_LIT:1> : ] * <NUM_LIT:2> ** <NUM_LIT:2> <EOL> indexer [ : , <NUM_LIT:1> : ] += image [ : , : - <NUM_LIT:1> ] * <NUM_LIT:2> ** <NUM_LIT:3> <EOL> indexer [ : , : ] += image [ : , : ] * <NUM_LIT:2> ** <NUM_LIT:4> <EOL> indexer [ : , : - <NUM_LIT:1> ] += image [ : , <NUM_LIT:1> : ] * <NUM_LIT:2> ** <NUM_LIT:5> <EOL> indexer [ : - <NUM_LIT:1> , <NUM_LIT:1> : ] += image [ <NUM_LIT:1> : , : - <NUM_LIT:1> ] * <NUM_LIT:2> ** <NUM_LIT:6> <EOL> indexer [ : - <NUM_LIT:1> , : ] += image [ <NUM_LIT:1> : , : ] * <NUM_LIT:2> ** <NUM_LIT:7> <EOL> indexer [ : - <NUM_LIT:1> , : - <NUM_LIT:1> ] += image [ <NUM_LIT:1> : , <NUM_LIT:1> : ] * <NUM_LIT:2> ** <NUM_LIT:8> <EOL> else : <EOL> indexer = _table_lookup_index ( np . ascontiguousarray ( image , np . uint8 ) ) <EOL> image = table [ indexer ] <EOL> return image </s>
