U
    ?h                     @   s(   d dl ZddlmZ dddddZdS )    N   )label)outc                   s\  t  fdd| jD r(|dkr(td|dkr8|  }|dk	rd|j d|j }|j|jkrht|f|jtkrztd| }n~tj|td} d	 }t	|}t	| d}	d
d |jD }
t
|jD ]8}||
|< d|t|
< |	|
|< d|t|
< t	d|
|< qt|ddd\} }t| | }t|d	 }t||}|| d | j}|||< |S )a  Clear objects connected to the label image border.

    Parameters
    ----------
    labels : (M[, N[, ..., P]]) array of int or bool
        Imaging data labels.
    buffer_size : int, optional
        The width of the border examined.  By default, only objects
        that touch the outside of the image are removed.
    bgval : float or int, optional
        Cleared objects are set to this value.
    mask : ndarray of bool, same shape as `image`, optional.
        Image data mask. Objects in labels image overlapping with
        False pixels of mask will be removed. If defined, the
        argument buffer_size will be ignored.
    out : ndarray
        Array of the same shape as `labels`, into which the
        output is placed. By default, a new array is created.

    Returns
    -------
    out : (M[, N[, ..., P]]) array
        Imaging data labels with cleared borders

    Examples
    --------
    >>> import numpy as np
    >>> from skimage.segmentation import clear_border
    >>> labels = np.array([[0, 0, 0, 0, 0, 0, 0, 1, 0],
    ...                    [1, 1, 0, 0, 1, 0, 0, 1, 0],
    ...                    [1, 1, 0, 1, 0, 1, 0, 0, 0],
    ...                    [0, 0, 0, 1, 1, 1, 1, 0, 0],
    ...                    [0, 1, 1, 1, 1, 1, 1, 1, 0],
    ...                    [0, 0, 0, 0, 0, 0, 0, 0, 0]])
    >>> clear_border(labels)
    array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 1, 0, 0, 0, 0],
           [0, 0, 0, 1, 0, 1, 0, 0, 0],
           [0, 0, 0, 1, 1, 1, 1, 0, 0],
           [0, 1, 1, 1, 1, 1, 1, 1, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0]])
    >>> mask = np.array([[0, 0, 1, 1, 1, 1, 1, 1, 1],
    ...                  [0, 0, 1, 1, 1, 1, 1, 1, 1],
    ...                  [1, 1, 1, 1, 1, 1, 1, 1, 1],
    ...                  [1, 1, 1, 1, 1, 1, 1, 1, 1],
    ...                  [1, 1, 1, 1, 1, 1, 1, 1, 1],
    ...                  [1, 1, 1, 1, 1, 1, 1, 1, 1]]).astype(bool)
    >>> clear_border(labels, mask=mask)
    array([[0, 0, 0, 0, 0, 0, 0, 1, 0],
           [0, 0, 0, 0, 1, 0, 0, 1, 0],
           [0, 0, 0, 1, 0, 1, 0, 0, 0],
           [0, 0, 0, 1, 1, 1, 1, 0, 0],
           [0, 1, 1, 1, 1, 1, 1, 1, 0],
           [0, 0, 0, 0, 0, 0, 0, 0, 0]])

    c                 3   s   | ]} |kV  qd S N ).0sbuffer_sizer   T/var/www/html/venv/lib/python3.8/site-packages/skimage/segmentation/_clear_border.py	<genexpr>@   s     zclear_border.<locals>.<genexpr>Nz/buffer size may not be greater than labels sizez3labels and mask should have the same shape but are z and zmask should be of type bool.)dtype   c                 S   s   g | ]}t d qS r   )slice)r   _r   r   r   
<listcomp>U   s     z clear_border.<locals>.<listcomp>Tr   )
backgroundZ
return_num)anyshape
ValueErrorcopyr   bool	TypeErrornpZ
zeros_liker   rangendimtupler   uniqueZarangeZin1dZreshape)labelsr
   Zbgvalmaskr   err_msgZbordersextZslstartZslendZslicesdnumberZborders_indicesindicesZ
label_maskr   r	   r   clear_border   s:    : 
r&   )r   r   N)numpyr   measurer   r&   r   r   r   r   <module>   s   