o
    oi
                     @   sB   d dl Z d dlm  mZ d dlmZ d	dededefddZdS )
    N)Tensord   imagenum_iterationsreturnc           	      C   s
  t | tstdt|  t |tr|dk rtdt| jdk s)| jd dkr1td| j | jdd \}}| d	d||}|dk}|j\}}}}t	j
d|| | d | j| jd
d	d||f}d|| < t|D ]}tj|dddd}t	||}qn|| S )a  Compute the Connected-component labelling (CCL) algorithm.

    .. image:: https://github.com/kornia/data/raw/main/cells_segmented.png

    The implementation is an adaptation of the following repository:

    https://gist.github.com/efirdc/5d8bd66859e574c683a504a4690ae8bc

    .. warning::
        This is an experimental API subject to changes and optimization improvements.

    .. note::
       See a working example `here <https://kornia.github.io/tutorials/nbs/connected_components.html>`__.

    Args:
        image: the binarized input image with shape :math:`(*, 1, H, W)`.
          The image must be in floating point with range [0, 1].
        num_iterations: the number of iterations to make the algorithm to converge.

    Return:
        The labels image with the same shape of the input image.

    Example:
        >>> img = torch.rand(2, 1, 4, 5)
        >>> img_labels = connected_components(img, num_iterations=100)

    z&Input imagetype is not a Tensor. Got:    z0Input num_iterations must be a positive integer.   z*Input image shape must be (*,1,H,W). Got: N)devicedtyper   )kernel_sizestridepadding)
isinstancer   	TypeErrortypeintlenshape
ValueErrorviewtorcharanger   r   rangeF
max_pool2dmulview_as)	r   r   HW
image_viewmaskB_out r'   W/home/ubuntu/.local/lib/python3.10/site-packages/kornia/contrib/connected_components.pyconnected_components   s    
0

r)   )r   )	r   torch.nn.functionalnn
functionalr   kornia.corer   r   r)   r'   r'   r'   r(   <module>   s   