ó
 ‰\c           @   s  d  Z  d d l Z d d l Z d d l m Z d d l m Z d d l	 m
 Z
 d d	 d
 d d d g Z d „  Z d „  Z d „  Z e d d e e d „ ƒ Z e d d e e d „ ƒ Z e e d d d „ ƒ ƒ Z e e d d d „ ƒ ƒ Z e d d d „ ƒ Z e d d d „ ƒ Z d S(   s$   
Grayscale morphological operations
iÿÿÿÿN(   t   ndimagei   (   t   default_selemi   (   t   cropt   erosiont   dilationt   openingt   closingt   white_tophatt   black_tophatc         C   sò   |  j  d k r |  S|  j \ } } | d d k r t j d | f |  j ƒ } | rk t j |  | f ƒ }  n t j | |  f ƒ }  | d 7} n  | d d k rî t j | d f |  j ƒ } | rÖ t j |  | f ƒ }  qî t j | |  f ƒ }  n  |  S(   sÉ  Shift the binary image `selem` in the left and/or up.

    This only affects 2D structuring elements with even number of rows
    or columns.

    Parameters
    ----------
    selem : 2D array, shape (M, N)
        The input structuring element.
    shift_x, shift_y : bool
        Whether to move `selem` along each axis.

    Returns
    -------
    out : 2D array, shape (M + int(shift_x), N + int(shift_y))
        The shifted structuring element.
    i   i    i   (   t   ndimt   shapet   npt   zerost   dtypet   vstackt   hstack(   t   selemt   shift_xt   shift_yt   mt   nt	   extra_rowt	   extra_col(    (    s6   lib/python2.7/site-packages/skimage/morphology/grey.pyt   _shift_selem   s    c         C   s$   |  t  d d d ƒ f |  j } | S(   sÌ  Change the order of the values in `selem`.

    This is a patch for the *weird* footprint inversion in
    `ndi.grey_morphology` [1]_.

    Parameters
    ----------
    selem : array
        The input structuring element.

    Returns
    -------
    inverted : array, same shape and type as `selem`
        The structuring element, in opposite order.

    Examples
    --------
    >>> selem = np.array([[0, 0, 0], [0, 1, 1], [0, 1, 1]], np.uint8)
    >>> _invert_selem(selem)
    array([[1, 1, 0],
           [1, 1, 0],
           [0, 0, 0]], dtype=uint8)

    References
    ----------
    .. [1] https://github.com/scipy/scipy/blob/ec20ababa400e39ac3ffc9148c01ef86d5349332/scipy/ndimage/morphology.py#L1285
    iÿÿÿÿN(   t   slicet   NoneR	   (   R   t   inverted(    (    s6   lib/python2.7/site-packages/skimage/morphology/grey.pyt   _invert_selem4   s     c            s%   t  j ˆ  ƒ d ‡  f d † ƒ } | S(   sú  Pad input images for certain morphological operations.

    Parameters
    ----------
    func : callable
        A morphological function, either opening or closing, that
        supports eccentric structuring elements. Its parameters must
        include at least `image`, `selem`, and `out`.

    Returns
    -------
    func_out : callable
        The same function, but correctly padding the input image before
        applying the input function.

    See Also
    --------
    opening, closing.
    c   
         sï   g  } t  } | d  k r* t j |  ƒ } n  xN | j D]C } | d d k r] | d } t } n d } | j | f d ƒ q4 W| r« t j |  | d d ƒ}  t j |  ƒ }	 n | }	 ˆ  |  | d |	 | | Ž}	 | rå t |	 | ƒ | (n |	 } | S(   Ni   i    i   t   modet   edget   out(	   t   FalseR   R   t
   empty_likeR
   t   Truet   appendt   padR   (
   t   imageR   R   t   argst   kwargst
   pad_widthst   paddingt   axis_lent   axis_pad_widtht   out_temp(   t   func(    s6   lib/python2.7/site-packages/skimage/morphology/grey.pyt   func_outh   s&    
	N(   t	   functoolst   wrapsR   (   R,   R-   (    (   R,   s6   lib/python2.7/site-packages/skimage/morphology/grey.pyt   pad_for_eccentric_selemsT   s    c         C   s\   t  j | ƒ } t | | | ƒ } | d k r? t  j |  ƒ } n  t j |  d | d | ƒ| S(   sì  Return greyscale morphological erosion of an image.

    Morphological erosion sets a pixel at (i,j) to the minimum over all pixels
    in the neighborhood centered at (i,j). Erosion shrinks bright regions and
    enlarges dark regions.

    Parameters
    ----------
    image : ndarray
        Image array.
    selem : ndarray, optional
        The neighborhood expressed as an array of 1's and 0's.
        If None, use cross-shaped structuring element (connectivity=1).
    out : ndarrays, optional
        The array to store the result of the morphology. If None is
        passed, a new array will be allocated.
    shift_x, shift_y : bool, optional
        shift structuring element about center point. This only affects
        eccentric structuring elements (i.e. selem with even numbered sides).

    Returns
    -------
    eroded : array, same shape as `image`
        The result of the morphological erosion.

    Notes
    -----
    For ``uint8`` (and ``uint16`` up to a certain bit-depth) data, the
    lower algorithm complexity makes the `skimage.filters.rank.minimum`
    function more efficient for larger images and structuring elements.

    Examples
    --------
    >>> # Erosion shrinks bright regions
    >>> import numpy as np
    >>> from skimage.morphology import square
    >>> bright_square = np.array([[0, 0, 0, 0, 0],
    ...                           [0, 1, 1, 1, 0],
    ...                           [0, 1, 1, 1, 0],
    ...                           [0, 1, 1, 1, 0],
    ...                           [0, 0, 0, 0, 0]], dtype=np.uint8)
    >>> erosion(bright_square, square(3))
    array([[0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0],
           [0, 0, 1, 0, 0],
           [0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0]], dtype=uint8)

    t	   footprintt   outputN(   R   t   arrayR   R   R    t   ndit   grey_erosion(   R$   R   R   R   R   (    (    s6   lib/python2.7/site-packages/skimage/morphology/grey.pyR   ‚   s    3c         C   sh   t  j | ƒ } t | | | ƒ } t | ƒ } | d k rK t  j |  ƒ } n  t j |  d | d | ƒ| S(   sý  Return greyscale morphological dilation of an image.

    Morphological dilation sets a pixel at (i,j) to the maximum over all pixels
    in the neighborhood centered at (i,j). Dilation enlarges bright regions
    and shrinks dark regions.

    Parameters
    ----------

    image : ndarray
        Image array.
    selem : ndarray, optional
        The neighborhood expressed as a 2-D array of 1's and 0's.
        If None, use cross-shaped structuring element (connectivity=1).
    out : ndarray, optional
        The array to store the result of the morphology. If None, is
        passed, a new array will be allocated.
    shift_x, shift_y : bool, optional
        shift structuring element about center point. This only affects
        eccentric structuring elements (i.e. selem with even numbered sides).

    Returns
    -------
    dilated : uint8 array, same shape and type as `image`
        The result of the morphological dilation.

    Notes
    -----
    For `uint8` (and `uint16` up to a certain bit-depth) data, the lower
    algorithm complexity makes the `skimage.filters.rank.maximum` function more
    efficient for larger images and structuring elements.

    Examples
    --------
    >>> # Dilation enlarges bright regions
    >>> import numpy as np
    >>> from skimage.morphology import square
    >>> bright_pixel = np.array([[0, 0, 0, 0, 0],
    ...                          [0, 0, 0, 0, 0],
    ...                          [0, 0, 1, 0, 0],
    ...                          [0, 0, 0, 0, 0],
    ...                          [0, 0, 0, 0, 0]], dtype=np.uint8)
    >>> dilation(bright_pixel, square(3))
    array([[0, 0, 0, 0, 0],
           [0, 1, 1, 1, 0],
           [0, 1, 1, 1, 0],
           [0, 1, 1, 1, 0],
           [0, 0, 0, 0, 0]], dtype=uint8)

    R1   R2   N(   R   R3   R   R   R   R    R4   t   grey_dilation(   R$   R   R   R   R   (    (    s6   lib/python2.7/site-packages/skimage/morphology/grey.pyR   ½   s    4c      	   C   s4   t  |  | ƒ } t | | d | d t d t ƒ} | S(   s¶  Return greyscale morphological opening of an image.

    The morphological opening on an image is defined as an erosion followed by
    a dilation. Opening can remove small bright spots (i.e. "salt") and connect
    small dark cracks. This tends to "open" up (dark) gaps between (bright)
    features.

    Parameters
    ----------
    image : ndarray
        Image array.
    selem : ndarray, optional
        The neighborhood expressed as an array of 1's and 0's.
        If None, use cross-shaped structuring element (connectivity=1).
    out : ndarray, optional
        The array to store the result of the morphology. If None
        is passed, a new array will be allocated.

    Returns
    -------
    opening : array, same shape and type as `image`
        The result of the morphological opening.

    Examples
    --------
    >>> # Open up gap between two bright regions (but also shrink regions)
    >>> import numpy as np
    >>> from skimage.morphology import square
    >>> bad_connection = np.array([[1, 0, 0, 0, 1],
    ...                            [1, 1, 0, 1, 1],
    ...                            [1, 1, 1, 1, 1],
    ...                            [1, 1, 0, 1, 1],
    ...                            [1, 0, 0, 0, 1]], dtype=np.uint8)
    >>> opening(bad_connection, square(3))
    array([[0, 0, 0, 0, 0],
           [1, 1, 0, 1, 1],
           [1, 1, 0, 1, 1],
           [1, 1, 0, 1, 1],
           [0, 0, 0, 0, 0]], dtype=uint8)

    R   R   R   (   R   R   R!   (   R$   R   R   t   eroded(    (    s6   lib/python2.7/site-packages/skimage/morphology/grey.pyR   ÿ   s    ,!c      	   C   s4   t  |  | ƒ } t | | d | d t d t ƒ} | S(   sŒ  Return greyscale morphological closing of an image.

    The morphological closing on an image is defined as a dilation followed by
    an erosion. Closing can remove small dark spots (i.e. "pepper") and connect
    small bright cracks. This tends to "close" up (dark) gaps between (bright)
    features.

    Parameters
    ----------
    image : ndarray
        Image array.
    selem : ndarray, optional
        The neighborhood expressed as an array of 1's and 0's.
        If None, use cross-shaped structuring element (connectivity=1).
    out : ndarray, optional
        The array to store the result of the morphology. If None,
        is passed, a new array will be allocated.

    Returns
    -------
    closing : array, same shape and type as `image`
        The result of the morphological closing.

    Examples
    --------
    >>> # Close a gap between two bright lines
    >>> import numpy as np
    >>> from skimage.morphology import square
    >>> broken_line = np.array([[0, 0, 0, 0, 0],
    ...                         [0, 0, 0, 0, 0],
    ...                         [1, 1, 0, 1, 1],
    ...                         [0, 0, 0, 0, 0],
    ...                         [0, 0, 0, 0, 0]], dtype=np.uint8)
    >>> closing(broken_line, square(3))
    array([[0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0],
           [1, 1, 1, 1, 1],
           [0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0]], dtype=uint8)

    R   R   R   (   R   R   R!   (   R$   R   R   t   dilated(    (    s6   lib/python2.7/site-packages/skimage/morphology/grey.pyR   1  s    ,!c         C   s*  t  j | ƒ } | |  k ri t |  | ƒ } t  j | j t  j ƒ r[ t  j | | d | ƒn
 | | 8} | S| d k r‡ t  j |  ƒ } n  t	 |  t  j
 ƒ rÃ |  j t  j k rÃ |  j d t  j ƒ } n |  } t	 | t  j
 ƒ r| j t  j k r| j d t  j ƒ } n | } t j | d | d | ƒ} | S(   sb  Return white top hat of an image.

    The white top hat of an image is defined as the image minus its
    morphological opening. This operation returns the bright spots of the image
    that are smaller than the structuring element.

    Parameters
    ----------
    image : ndarray
        Image array.
    selem : ndarray, optional
        The neighborhood expressed as an array of 1's and 0's.
        If None, use cross-shaped structuring element (connectivity=1).
    out : ndarray, optional
        The array to store the result of the morphology. If None
        is passed, a new array will be allocated.

    Returns
    -------
    out : array, same shape and type as `image`
        The result of the morphological white top hat.

    Examples
    --------
    >>> # Subtract grey background from bright peak
    >>> import numpy as np
    >>> from skimage.morphology import square
    >>> bright_on_grey = np.array([[2, 3, 3, 3, 2],
    ...                            [3, 4, 5, 4, 3],
    ...                            [3, 5, 9, 5, 3],
    ...                            [3, 4, 5, 4, 3],
    ...                            [2, 3, 3, 3, 2]], dtype=np.uint8)
    >>> white_tophat(bright_on_grey, square(3))
    array([[0, 0, 0, 0, 0],
           [0, 0, 1, 0, 0],
           [0, 1, 5, 1, 0],
           [0, 0, 1, 0, 0],
           [0, 0, 0, 0, 0]], dtype=uint8)

    R   R   R1   R2   N(   R   R3   R   t
   issubdtypeR   t   bool_t   logical_xorR   R    t
   isinstancet   ndarrayt   boolt   viewt   uint8R4   R   (   R$   R   R   t   openedt   image_t   out_(    (    s6   lib/python2.7/site-packages/skimage/morphology/grey.pyR   c  s"    *
$$c         C   su   | |  k r |  j  ƒ  } n |  } t |  | d | ƒ} t j | j t j ƒ rg t j | | d | ƒn
 | | 8} | S(   sÇ  Return black top hat of an image.

    The black top hat of an image is defined as its morphological closing minus
    the original image. This operation returns the dark spots of the image that
    are smaller than the structuring element. Note that dark spots in the
    original image are bright spots after the black top hat.

    Parameters
    ----------
    image : ndarray
        Image array.
    selem : ndarray, optional
        The neighborhood expressed as a 2-D array of 1's and 0's.
        If None, use cross-shaped structuring element (connectivity=1).
    out : ndarray, optional
        The array to store the result of the morphology. If None
        is passed, a new array will be allocated.

    Returns
    -------
    out : array, same shape and type as `image`
        The result of the morphological black top hat.

    Examples
    --------
    >>> # Change dark peak to bright peak and subtract background
    >>> import numpy as np
    >>> from skimage.morphology import square
    >>> dark_on_grey = np.array([[7, 6, 6, 6, 7],
    ...                          [6, 5, 4, 5, 6],
    ...                          [6, 4, 0, 4, 6],
    ...                          [6, 5, 4, 5, 6],
    ...                          [7, 6, 6, 6, 7]], dtype=np.uint8)
    >>> black_tophat(dark_on_grey, square(3))
    array([[0, 0, 0, 0, 0],
           [0, 0, 1, 0, 0],
           [0, 1, 5, 1, 0],
           [0, 0, 1, 0, 0],
           [0, 0, 0, 0, 0]], dtype=uint8)

    R   (   t   copyR   R   R9   R   R:   R;   (   R$   R   R   t   original(    (    s6   lib/python2.7/site-packages/skimage/morphology/grey.pyR   ¥  s    +
(   t   __doc__R.   t   numpyR   t   scipyR    R4   t   miscR   t   utilR   t   __all__R   R   R0   R   R   R   R   R   R   R   R   (    (    (    s6   lib/python2.7/site-packages/skimage/morphology/grey.pyt   <module>   s0   		&	 	.:A00A