a
    d                     @   s6   d dl Z d dlm  mZ de jee jdddZdS )    Nd   )imagenum_iterationsreturnc           	      C   s  t | tjstdt|  t |tr0|dk r8tdt| jdk sT| jd dkrdtd| j | jdd \}}| 	d	d||}|dk}|j\}}}}tj
|| | | j| jd
	d	d||f}d|| < t|D ]}tj|dddd| ||< q|| S )a  Computes 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-tutorials.readthedocs.io/en/latest/
       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 torch.Tensor. Got:    z0Input num_iterations must be a positive integer.   z*Input image shape must be (*,1,H,W). Got: N)devicedtyper   )Zkernel_sizeZstridepadding)
isinstancetorchTensor	TypeErrortypeintlenshape
ValueErrorviewZaranger   r   rangeFZ
max_pool2dZview_as)	r   r   HWZ
image_viewmaskB_out r    l/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/contrib/connected_components.pyconnected_components   s    *
r"   )r   )r   Ztorch.nn.functionalnnZ
functionalr   r   r   r"   r    r    r    r!   <module>   s   