o
    :ήc!                     @   s   d Z ddlZddlZddlmZ ddlmZm	Z	 ddl
mZ dZd	d
 eD ZdZedd
 eD  dd Zdd Ze	dddddddddZe	dddddddddZdS )z#Miscellaneous morphology functions.    N)ndimage   )warn
remove_arg   )_default_footprint)erosiondilationopeningclosingc                 C   s   i | ]}|d | qS )grey_ .0xr   r   >/tmp/pip-target-vg8gfxp4/lib/python/skimage/morphology/misc.py
<dictcomp>   s    r   )binary_erosionbinary_dilationbinary_openingbinary_closingblack_tophatwhite_tophatc                 C   s   i | ]}||qS r   r   r   r   r   r   r      s    c                    s   t  d fdd	}|S )a  Decorator to add a default footprint 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 footprint of same dimension
        as the input image with connectivity 1.

    Nc                    s,   |d u r	t | j} | g|R d|i|S )N	footprint)r   ndim)imager   argskwargsfuncr   r   func_out#   s   
z#default_footprint.<locals>.func_out)N)	functoolswraps)r   r    r   r   r   default_footprint   s   r#   c                 C   s0   | j tkst| j tjstd| j  d S d S )Nz7Only bool or integer image types are supported. Got %s.)dtypeboolnp
issubdtypeinteger	TypeError)arr   r   r   _check_dtype_supported,   s
   r+   in_placez1.0z Please use out argument instead.)changed_versionhelp_msg@   Foutc          
      C   s   t |  |dur
d}|r| }n|du r|  }n| |dd< |dkr$|S |jtkrAt| j|}tj| tj	d}tj
| ||d n|}z	t| }W n tyW   tdw t|dkrg|jtkrgtd ||k }|| }	d||	< |S )	aC  Remove objects smaller than the specified size.

    Expects ar to be an array with labeled objects, and removes objects
    smaller than min_size. If `ar` is bool, the image is first labeled.
    This leads to potentially different behavior for bool and 0-and-1
    arrays.

    Parameters
    ----------
    ar : ndarray (arbitrary shape, int or bool type)
        The array containing the objects of interest. If the array type is
        int, the ints must be non-negative.
    min_size : int, optional (default: 64)
        The smallest allowable object size.
    connectivity : int, {1, 2, ..., ar.ndim}, optional (default: 1)
        The connectivity defining the neighborhood of a pixel. Used during
        labelling if `ar` is bool.
    in_place : bool, optional (default: False)
        If ``True``, remove the objects in the input array itself.
        Otherwise, make a copy. Deprecated since version 0.19. Please
        use `out` instead.
    out : ndarray
        Array of the same shape as `ar`, into which the output is
        placed. By default, a new array is created.

    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]])
    >>> 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]])
    >>> d = morphology.remove_small_objects(a, 6, out=a)
    >>> d is a
    True

    NFr   )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%   ndigenerate_binary_structurer   r&   
zeros_likeint32labelbincountravel
ValueErrorlenr   )
r*   min_sizeconnectivityr,   r1   r   ccscomponent_sizes	too_smalltoo_small_maskr   r   r   remove_small_objects3   s4   ?

rC   c                C   s   t |  | jtkrtdt |dur|jtkrtdd}|r"| }n|du r-| jtdd}tj| |d t	||||d}tj||d |S )aN	  Remove contiguous 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. Deprecated since version 0.19.
        Please use `out` instead.
    out : ndarray
        Array of the same shape as `ar` and bool dtype, into which the
        output is placed. By default, a new array is created.

    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]])
    >>> 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]])
    >>> d = morphology.remove_small_holes(a, 2, out=a)
    >>> 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out dtype must be boolFT)r3   r0   )
r+   r$   r%   r   UserWarningr)   astyper&   logical_notrC   )r*   area_thresholdr>   r,   r1   r   r   r   remove_small_holes   s"   B

rH   )r/   r   F)__doc__numpyr&   r!   scipyr   r4   _shared.utilsr   r   
footprintsr   funcsskimage2ndimageupdater#   r+   rC   rH   r   r   r   r   <module>   s0    f