B
    \0                 @   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	d
 eD ZdZeedd
 eD  dd Zdd ZdddZdddZdS )z#Miscellaneous morphology functions.    N)ndimage   )warn   )_default_selem)ZerosionZdilationZopeningclosingc             c   s   | ]}|d | fV  qdS )Zgrey_N ).0xr   r   6lib/python3.7/site-packages/skimage/morphology/misc.py	<genexpr>   s    r   )Zbinary_erosionZbinary_dilationZbinary_openingZbinary_closingZblack_tophatZwhite_tophatc             c   s   | ]}||fV  qd S )Nr   )r	   r
   r   r   r   r      s    c                s   t  d fdd	}|S )a  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.

    Nc                s*   |d krt | j} | f|d|i|S )Nselem)r   ndim)Zimager   argskwargs)funcr   r   func_out#   s    
zdefault_selem.<locals>.func_out)N)	functoolswraps)r   r   r   )r   r   default_selem   s    r   c             C   s,   | j tks(t| j tjs(td| j  d S )Nz7Only bool or integer image types are supported. Got %s.)dtypeboolnpZ
issubdtypeZinteger	TypeError)arr   r   r   _check_dtype_supported,   s    r   @   Fc       
      C   s   t |  |r| }n|  }|dkr&|S |jtkr`t| j|}tj| tj	d}tj
| ||d n|}yt| }W n tk
r   tdY nX t|dkr|jtkrtd ||k }|| }	d||	< |S )a  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

    r   )r   )outputz{Negative value labels are not supported. Try relabeling the input with `scipy.ndimage.label` or `skimage.morphology.label`.r   z[Only one label was provided to `remove_small_objects`. Did you mean to use a boolean array?)r   copyr   r   ndiZgenerate_binary_structurer   r   Z
zeros_likeZint32ZlabelZbincountZravel
ValueErrorlenr   )
r   min_sizeconnectivityin_placeoutr   ZccsZcomponent_sizesZ	too_smallZtoo_small_maskr   r   r   remove_small_objects3   s*    3
r&   c             C   s   t |  | jtkrtdt |dk	r0td |}|r:| }n|  }|rTt||}n
t|}t||||}|r~t||}n
t|}|S )a  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.

    z\Any labeled images will be returned as a boolean array. Did you mean to use a boolean array?Nz\the min_size argument is deprecated and will be removed in 0.16. Use area_threshold instead.)	r   r   r   r   UserWarningr   r   Zlogical_notr&   )r   Zarea_thresholdr#   r$   r"   r%   r   r   r   remove_small_holes   s$    =


r(   )r   r   F)r   r   FN)__doc__Znumpyr   r   Zscipyr   r   Z_shared.utilsr   r   r   ZfuncsdictZskimage2ndimageupdater   r   r&   r(   r   r   r   r   <module>   s   
V 