ó
 ‰\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 Z e d „  e Dƒ ƒ Z d Z e j e d „  e Dƒ ƒ ƒ d „  Z d „  Z d d e d „ Z d d e d d „ Z d S(   s#   Miscellaneous morphology functions.iÿÿÿÿN(   t   ndimagei   (   t   warni   (   t   _default_selemt   erosiont   dilationt   openingt   closingc         c   s   |  ] } | d  | f Vq d S(   t   grey_N(    (   t   .0t   x(    (    s6   lib/python2.7/site-packages/skimage/morphology/misc.pys	   <genexpr>   s    t   binary_erosiont   binary_dilationt   binary_openingt   binary_closingt   black_tophatt   white_tophatc         c   s   |  ] } | | f Vq d  S(   N(    (   R   R	   (    (    s6   lib/python2.7/site-packages/skimage/morphology/misc.pys	   <genexpr>   s    c            s%   t  j ˆ  ƒ d ‡  f d † ƒ } | S(   sŸ  Decorator to add a default structuring element to morphology functions.

    Parameters
    ----------
    func : function
        A morphology function such as erosion, dilation, opening, closing,
        white_tophat, or black_tophat.

    Returns
    -------
    func_out : function
        The function, using a default structuring element of same dimension
        as the input image with connectivity 1.

    c            s4   | d  k r t |  j ƒ } n  ˆ  |  d | | | ŽS(   Nt   selem(   t   NoneR   t   ndim(   t   imageR   t   argst   kwargs(   t   func(    s6   lib/python2.7/site-packages/skimage/morphology/misc.pyt   func_out#   s    N(   t	   functoolst   wrapsR   (   R   R   (    (   R   s6   lib/python2.7/site-packages/skimage/morphology/misc.pyt   default_selem   s    c         C   sA   |  j  t k p$ t j |  j  t j ƒ s= t d |  j  ƒ ‚ n  d  S(   Ns7   Only bool or integer image types are supported. Got %s.(   t   dtypet   boolt   npt
   issubdtypet   integert	   TypeError(   t   ar(    (    s6   lib/python2.7/site-packages/skimage/morphology/misc.pyt   _check_dtype_supported,   s    'i@   c   
      C   s  t  |  ƒ | r |  } n |  j ƒ  } | d k r5 | S| j t k rŠ t j |  j | ƒ } t j |  d t j	 ƒ} t j
 |  | d | ƒn | } y t j | j ƒ  ƒ } Wn t k
 rÈ t d ƒ ‚ n Xt | ƒ d k r÷ | j t k r÷ t d ƒ n  | | k  } | | }	 d | |	 <| S(   sú  Remove connected components smaller than the specified size.

    Parameters
    ----------
    ar : ndarray (arbitrary shape, int or bool type)
        The array containing the connected components of interest. If the array
        type is int, it is assumed that it contains already-labeled objects.
        The ints must be non-negative.
    min_size : int, optional (default: 64)
        The smallest allowable connected component size.
    connectivity : int, {1, 2, ..., ar.ndim}, optional (default: 1)
        The connectivity defining the neighborhood of a pixel.
    in_place : bool, optional (default: False)
        If `True`, remove the connected components in the input array itself.
        Otherwise, make a copy.

    Raises
    ------
    TypeError
        If the input array is of an invalid type, such as float or string.
    ValueError
        If the input array contains negative values.

    Returns
    -------
    out : ndarray, same shape and type as input `ar`
        The input array with small connected components removed.

    Examples
    --------
    >>> from skimage import morphology
    >>> a = np.array([[0, 0, 0, 1, 0],
    ...               [1, 1, 1, 0, 0],
    ...               [1, 1, 1, 0, 1]], bool)
    >>> b = morphology.remove_small_objects(a, 6)
    >>> b
    array([[False, False, False, False, False],
           [ True,  True,  True, False, False],
           [ True,  True,  True, False, False]], dtype=bool)
    >>> c = morphology.remove_small_objects(a, 7, connectivity=2)
    >>> c
    array([[False, False, False,  True, False],
           [ True,  True,  True, False, False],
           [ True,  True,  True, False, False]], dtype=bool)
    >>> d = morphology.remove_small_objects(a, 6, in_place=True)
    >>> d is a
    True

    i    R   t   outputs{   Negative value labels are not supported. Try relabeling the input with `scipy.ndimage.label` or `skimage.morphology.label`.i   s[   Only one label was provided to `remove_small_objects`. Did you mean to use a boolean array?(   R"   t   copyR   R   t   ndit   generate_binary_structureR   R   t
   zeros_liket   int32t   labelt   bincountt   ravelt
   ValueErrort   lenR   (
   R!   t   min_sizet   connectivityt   in_placet   outR   t   ccst   component_sizest	   too_smallt   too_small_mask(    (    s6   lib/python2.7/site-packages/skimage/morphology/misc.pyt   remove_small_objects3   s*    3
	!

c         C   sÔ   t  |  ƒ |  j t k r) t d t ƒ n  | d k	 rL t d d ƒ | } n  | r[ |  } n |  j ƒ  } | r‚ t j | | ƒ } n t j | ƒ } t	 | | | | ƒ } | rÁ t j | | ƒ } n t j | ƒ } | S(   s˜  Remove continguous holes smaller than the specified size.

    Parameters
    ----------
    ar : ndarray (arbitrary shape, int or bool type)
        The array containing the connected components of interest.
    area_threshold : int, optional (default: 64)
        The maximum area, in pixels, of a contiguous hole that will be filled.
        Replaces `min_size`.
    connectivity : int, {1, 2, ..., ar.ndim}, optional (default: 1)
        The connectivity defining the neighborhood of a pixel.
    in_place : bool, optional (default: False)
        If `True`, remove the connected components in the input array itself.
        Otherwise, make a copy.


    Raises
    ------
    TypeError
        If the input array is of an invalid type, such as float or string.
    ValueError
        If the input array contains negative values.

    Returns
    -------
    out : ndarray, same shape and type as input `ar`
        The input array with small holes within connected components removed.

    Examples
    --------
    >>> from skimage import morphology
    >>> a = np.array([[1, 1, 1, 1, 1, 0],
    ...               [1, 1, 1, 0, 1, 0],
    ...               [1, 0, 0, 1, 1, 0],
    ...               [1, 1, 1, 1, 1, 0]], bool)
    >>> b = morphology.remove_small_holes(a, 2)
    >>> b
    array([[ True,  True,  True,  True,  True, False],
           [ True,  True,  True,  True,  True, False],
           [ True, False, False,  True,  True, False],
           [ True,  True,  True,  True,  True, False]], dtype=bool)
    >>> c = morphology.remove_small_holes(a, 2, connectivity=2)
    >>> c
    array([[ True,  True,  True,  True,  True, False],
           [ True,  True,  True, False,  True, False],
           [ True, False, False,  True,  True, False],
           [ True,  True,  True,  True,  True, False]], dtype=bool)
    >>> d = morphology.remove_small_holes(a, 2, in_place=True)
    >>> d is a
    True

    Notes
    -----
    If the array type is int, it is assumed that it contains already-labeled
    objects. The labels are not kept in the output image (this function always
    outputs a bool image). It is suggested that labeling is completed after
    using this function.

    s\   Any labeled images will be returned as a boolean array. Did you mean to use a boolean array?s;   the min_size argument is deprecated and will be removed in s!   0.16. Use area_threshold instead.N(
   R"   R   R   R   t   UserWarningR   R$   R   t   logical_notR6   (   R!   t   area_thresholdR/   R0   R.   R1   (    (    s6   lib/python2.7/site-packages/skimage/morphology/misc.pyt   remove_small_holes‰   s&    =

		(   R   R   R   R   (   R
   R   R   R   R   R   (   t   __doc__t   numpyR   R   t   scipyR    R%   t   _shared.utilsR   R   R   t   funcst   dictt   skimage2ndimaget   updateR   R"   t   FalseR6   R   R:   (    (    (    s6   lib/python2.7/site-packages/skimage/morphology/misc.pyt   <module>   s    		V	