ó
 ‰\c          @   sã  d  Z  d d l m Z d d l Z d d l m Z d d l m	 Z	 m
 Z
 m Z d d l m Z d	 „  Z d
 „  Z e j d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d g d e j ƒZ e j d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d g d e j ƒZ d d „ Z e j d d ƒ a d e d „ Z d „  Z d „  Z d S(   s9   
Algorithms for computing the skeleton of a binary image
iÿÿÿÿ(   t   rangeN(   t   ndimagei   (   t   _fast_skeletonizet   _skeletonize_loopt   _table_lookup_indexi   (   t	   assert_nDc         C   sg   |  j  t j ƒ }  |  j d k r0 t d ƒ ‚ n  t j t j |  j d ƒ ƒ s] t d ƒ ‚ n  t |  ƒ S(   sU	  Return the skeleton of a binary image.

    Thinning is used to reduce each connected component in a binary image
    to a single-pixel wide skeleton.

    Parameters
    ----------
    image : numpy.ndarray
        A binary image containing the objects to be skeletonized. '1'
        represents foreground, and '0' represents background. It
        also accepts arrays of boolean values where True is foreground.

    Returns
    -------
    skeleton : ndarray
        A matrix containing the thinned image.

    See also
    --------
    medial_axis

    Notes
    -----
    The algorithm [Zha84]_ works by making successive passes of the image,
    removing pixels on object borders. This continues until no
    more pixels can be removed.  The image is correlated with a
    mask that assigns each pixel a number in the range [0...255]
    corresponding to each possible pattern of its 8 neighbouring
    pixels. A look up table is then used to assign the pixels a
    value of 0, 1, 2 or 3, which are selectively removed during
    the iterations.

    Note that this algorithm will give different results than a
    medial axis transform, which is also often referred to as
    "skeletonization".

    References
    ----------
    .. [Zha84] A fast parallel algorithm for thinning digital patterns,
           T. Y. Zhang and C. Y. Suen, Communications of the ACM,
           March 1984, Volume 27, Number 3.


    Examples
    --------
    >>> X, Y = np.ogrid[0:9, 0:9]
    >>> ellipse = (1./3 * (X - 4)**2 + (Y - 4)**2 < 3**2).astype(np.uint8)
    >>> ellipse
    array([[0, 0, 0, 1, 1, 1, 0, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 1, 1, 0, 0],
           [0, 0, 0, 1, 1, 1, 0, 0, 0]], dtype=uint8)
    >>> skel = skeletonize(ellipse)
    >>> skel.astype(np.uint8)
    array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)

    i   s   Skeletonize requires a 2D arrayi    i   s(   Image contains values other than 0 and 1(   i    i   (	   t   astypet   npt   uint8t   ndimt
   ValueErrort   allt   in1dt   flatR   (   t   image(    (    s>   lib/python2.7/site-packages/skimage/morphology/_skeletonize.pyt   skeletonize   s    Ic             s%  d „  ‰  ‡  f d †  }  t  j g  t d ƒ D] } |  | ƒ ^ q+ ƒ } ‡  f d †  } t  j g  t d ƒ D] } | | ƒ ^ qh ƒ } | | @} ‡  f d †  } ‡  f d †  } t  j g  t d ƒ D] } | | ƒ ^ q¾ ƒ } t  j g  t d ƒ D] } | | ƒ ^ qì ƒ }	 | | @}
 | |	 @} |
 | f S(   s4   generate LUTs for thinning algorithm (for reference)c         S   s=   t  j g  t d d ƒ D] } |  | ?d @^ q ƒ j t  j ƒ S(   Ni    i	   i   (   R   t   arrayR    R   t   bool(   t   nt   i(    (    s>   lib/python2.7/site-packages/skimage/morphology/_skeletonize.pyt   nabem   s    c            se   d } ˆ  |  ƒ } xF d D]> } | | r | | d sJ | | d d r | d 7} q q W| d k S(   Ni    i   i   i   i   i   (   i    i   i   i   (    (   R   t   st   bitsR   (   R   (    s>   lib/python2.7/site-packages/skimage/morphology/_skeletonize.pyt   G1p   s    +i   c            sŠ   d \ } } ˆ  |  ƒ } x\ d	 D]T } | | s= | | d rJ | d 7} n  | | sf | | d d r | d 7} q q Wt  | | ƒ d
 k S(   Ni    i   i   i   i   i   i   (   i    i    (   i   i   i   i   (   i   i   (   t   min(   R   t   n1t   n2R   t   k(   R   (    s>   lib/python2.7/site-packages/skimage/morphology/_skeletonize.pyt   G2z   s    c            s4   ˆ  |  ƒ } | d s+ | d s+ | d o2 | d S(   Ni   i   i   i    (    (   R   R   (   R   (    s>   lib/python2.7/site-packages/skimage/morphology/_skeletonize.pyt   G3ˆ   s    c            s4   ˆ  |  ƒ } | d s+ | d s+ | d o2 | d S(   Ni   i   i   i   (    (   R   R   (   R   (    s>   lib/python2.7/site-packages/skimage/morphology/_skeletonize.pyt   G3pŒ   s    (   R   R   R    (   R   R   t   g1_lutR   t   g2_lutt   g12_lutR   R   t   g3_lutt   g3p_lutt   g123_lutt	   g123p_lut(    (   R   s>   lib/python2.7/site-packages/skimage/morphology/_skeletonize.pyt   _generate_thin_lutsj   s    	.
.
..

i    t   dtypec   
      C   s1  t  |  d ƒ t j |  d t ƒj t j ƒ } t j d d d g d d d g d d	 d
 g g d t j ƒ} | pv t j } d } t j t j | ƒ } } x† | | k r | | k  r | } xH t	 t
 g D]: } t j | | d d ƒ} t j | | ƒ }	 d | |	 <qÆ Wt j | ƒ } | d 7} q› W| j t j ƒ S(   sð  
    Perform morphological thinning of a binary image.

    Parameters
    ----------
    image : binary (M, N) ndarray
        The image to be thinned.

    max_iter : int, number of iterations, optional
        Regardless of the value of this parameter, the thinned image
        is returned immediately if an iteration produces no change.
        If this parameter is specified it thus sets an upper bound on
        the number of iterations performed.

    Returns
    -------
    out : ndarray of bool
        Thinned image.

    See also
    --------
    skeletonize, skeletonize_3d, medial_axis

    Notes
    -----
    This algorithm [1]_ works by making multiple passes over the image,
    removing pixels matching a set of criteria designed to thin
    connected regions while preserving eight-connected components and
    2 x 2 squares [2]_. In each of the two sub-iterations the algorithm
    correlates the intermediate skeleton image with a neighborhood mask,
    then looks up each neighborhood in a lookup table indicating whether
    the central pixel should be deleted in that sub-iteration.

    References
    ----------
    .. [1] Z. Guo and R. W. Hall, "Parallel thinning with
           two-subiteration algorithms," Comm. ACM, vol. 32, no. 3,
           pp. 359-373, 1989. DOI:10.1145/62065.62074
    .. [2] Lam, L., Seong-Whan Lee, and Ching Y. Suen, "Thinning
           Methodologies-A Comprehensive Survey," IEEE Transactions on
           Pattern Analysis and Machine Intelligence, Vol 14, No. 9,
           p. 879, 1992. DOI:10.1109/34.161346

    Examples
    --------
    >>> square = np.zeros((7, 7), dtype=np.uint8)
    >>> square[1:-1, 2:-2] = 1
    >>> square[0, 1] =  1
    >>> square
    array([[0, 1, 0, 0, 0, 0, 0],
           [0, 0, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 0, 0],
           [0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
    >>> skel = thin(square)
    >>> skel.astype(np.uint8)
    array([[0, 1, 0, 0, 0, 0, 0],
           [0, 0, 1, 0, 0, 0, 0],
           [0, 0, 0, 1, 0, 0, 0],
           [0, 0, 0, 1, 0, 0, 0],
           [0, 0, 0, 1, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
    i   R'   i   i   i   i    i   i    i@   i€   t   modet   constant(   R   R   t
   asanyarrayR   R   R   R   t   inft   sumt   G123_LUTt	   G123P_LUTt   ndit	   correlatet   take(
   R   t   max_itert   skelt   maskt   n_itert	   n_pts_oldt	   n_pts_newt   lutt   Nt   D(    (    s>   lib/python2.7/site-packages/skimage/morphology/_skeletonize.pyt   thin¸   s"    D!c      
   C   sÖ  | d	 k r! |  j t j ƒ } n  |  j t ƒ j ƒ  } t | | <t j d ƒ d
 @j t ƒ } | t j g  t d ƒ D]B } t	 j
 t | ƒ t ƒ d t	 j
 t | d @ƒ t ƒ d k ^ qs ƒ t j g  t d ƒ D]! } t j t | ƒ ƒ d k  ^ qÎ ƒ B@} t	 j | ƒ } | r| j ƒ  } n  t j g  t d ƒ D] } d t j t | ƒ ƒ ^ q1ƒ }	 t | |	 ƒ }
 t j d |  j d … d |  j d … f \ } } | j ƒ  } | | } t j | | d t j ƒ} t j | | d t j ƒ} t j | t j ƒ } t j j d ƒ } | j t j | j ƒ  ƒ ƒ } t j | |
 | | f ƒ } t j | d t j ƒ} t j | d t j ƒ} t | | | | | ƒ | j t ƒ } | d	 k	 r¾|  | | | <n  | rÎ| | f S| Sd	 S(   s’	  
    Compute the medial axis transform of a binary image

    Parameters
    ----------
    image : binary ndarray, shape (M, N)
        The image of the shape to be skeletonized.
    mask : binary ndarray, shape (M, N), optional
        If a mask is given, only those elements in `image` with a true
        value in `mask` are used for computing the medial axis.
    return_distance : bool, optional
        If true, the distance transform is returned as well as the skeleton.

    Returns
    -------
    out : ndarray of bools
        Medial axis transform of the image
    dist : ndarray of ints, optional
        Distance transform of the image (only returned if `return_distance`
        is True)

    See also
    --------
    skeletonize

    Notes
    -----
    This algorithm computes the medial axis transform of an image
    as the ridges of its distance transform.

    The different steps of the algorithm are as follows
     * A lookup table is used, that assigns 0 or 1 to each configuration of
       the 3x3 binary square, whether the central pixel should be removed
       or kept. We want a point to be removed if it has more than one neighbor
       and if removing it does not change the number of connected components.

     * The distance transform to the background is computed, as well as
       the cornerness of the pixel.

     * The foreground (value of 1) points are ordered by
       the distance transform, then the cornerness.

     * A cython function is called to reduce the image to its skeleton. It
       processes pixels in the order determined at the previous step, and
       removes or maintains a pixel according to the lookup table. Because
       of the ordering, it is possible to process all pixels in only one
       pass.

    Examples
    --------
    >>> square = np.zeros((7, 7), dtype=np.uint8)
    >>> square[1:-1, 2:-2] = 1
    >>> square
    array([[0, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 0, 0],
           [0, 0, 1, 1, 1, 0, 0],
           [0, 0, 0, 0, 0, 0, 0]], dtype=uint8)
    >>> medial_axis(square).astype(np.uint8)
    array([[0, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 0, 1, 0, 0],
           [0, 0, 0, 1, 0, 0, 0],
           [0, 0, 0, 1, 0, 0, 0],
           [0, 0, 0, 1, 0, 0, 0],
           [0, 0, 1, 0, 1, 0, 0],
           [0, 0, 0, 0, 0, 0, 0]], dtype=uint8)

    i   i   i   i   i   i	   i    R'   Ni   i   iïÿÿÿ(   t   NoneR   R   R   t   copyt   Falset   arangeR   R    R/   t   labelt   _pattern_oft   _eight_connectR,   t   distance_transform_edtt   _table_lookupt   mgridt   shapet   ascontiguousarrayt   intpR   t   randomt   RandomStatet   permutationt   lexsortt   int32R   (   R   R4   t   return_distancet   masked_imaget   center_is_foregroundt   indext   tablet   distancet   store_distancet   cornerness_tablet   corner_scoreR   t   jt   resultt	   generatort
   tiebreakert   order(    (    s>   lib/python2.7/site-packages/skimage/morphology/_skeletonize.pyt   medial_axis!  sF    H	R?
	23
	
c         C   sX   t  j |  d
 @|  d @|  d @g |  d @|  d @|  d @g |  d @|  d @|  d @g g t ƒ S(   sZ   
    Return the pattern represented by an index value
    Byte decomposition of index
    i   i    i   i   i   i   i   i   i   i   i   i   i   i   i    i@   i€   i   (   R   R   R   (   RQ   (    (    s>   lib/python2.7/site-packages/skimage/morphology/_skeletonize.pyRA   ¶  s    c         C   s’  |  j  d d k  s& |  j  d d k  ri|  j t ƒ }  t j |  j  t ƒ } | d d … d d … f c |  d d … d d … f d 7<| d d … d d … f c |  d d … d d … f d 7<| d d … d d … f c |  d d … d d … f d 7<| d d … d d … f c |  d d … d d … f d 7<| d d … d d … f c |  d d … d d … f d 7<| d d … d d … f c |  d d … d d … f d 7<| d d … d d … f c |  d d … d d … f d 7<| d d … d d … f c |  d d … d d … f d 7<| d d … d d … f c |  d d … d d … f d 7<n t t j |  t j ƒ ƒ } | | }  |  S(   s±  
    Perform a morphological transform on an image, directed by its
    neighbors

    Parameters
    ----------
    image : ndarray
        A binary image
    table : ndarray
        A 512-element table giving the transform of each pixel given
        the values of that pixel and its 8-connected neighbors.
    border_value : bool
        The value of pixels beyond the border of the image.

    Returns
    -------
    result : ndarray of same shape as `image`
        Transformed image

    Notes
    -----
    The pixels are numbered like this::


      0 1 2
      3 4 5
      6 7 8

    The index at a pixel is the sum of 2**<pixel-number> for pixels
    that evaluate to true.
    i    i   i   Niÿÿÿÿi   i   i   i   i   i   i   i   i   i   i   i    i@   i€   i   (	   RF   R   R   R   t   zerost   intR   RG   R   (   R   RR   t   indexer(    (    s>   lib/python2.7/site-packages/skimage/morphology/_skeletonize.pyRD   À  s    $&<<<<<<<<?
(   t   __doc__t	   six.movesR    t   numpyR   t   scipyR   R/   t   _skeletonize_cyR   R   R   t   _shared.utilsR   R   R&   R   R   R-   R.   R<   R;   t   generate_binary_structureRB   R>   R\   RA   RD   (    (    (    s>   lib/python2.7/site-packages/skimage/morphology/_skeletonize.pyt   <module>   sP   	X	/?999999999999-?999999999999-f•	
