def connected_components_gpu(image: torch.Tensor, num_iterations: int = 1000) -> torch.Tensor:
    """Perform connected component labeling on GPU.

    Labels connected regions in a binary image and remaps the labels sequentially
    from 0 to N, where N is the number of unique components. Uses the GPU for
    faster processing of large images.

    Args:
        image (torch.Tensor): Binary input image tensor of shape ``(B, 1, H, W)``
            where ``B`` is batch size, ``H`` is height and ``W`` is width.
            Values should be binary (0 or 1).
        num_iterations (int, optional): Number of iterations for the connected
            components algorithm. Higher values may be needed for complex regions.
            Defaults to 1000.

    Returns:
        torch.Tensor: Integer tensor of same shape as input, containing labeled
            components from 0 to N. Background (zero) pixels in the input remain
            ``0``, while connected regions are labeled with integers from ``1``
            to ``N``.

    Example:
        >>> import torch
        >>> from anomalib.utils.cv import connected_components_gpu
        >>> # Create a binary mask with a 2x2 square anomaly
        >>> mask = torch.zeros(1, 1, 4, 4)
        >>> mask[0, 0, 1:3, 1:3] = 1
        >>> labels = connected_components_gpu(mask)
        >>> print(labels.unique())  # Should show tensor([0, 1])
        >>> print(labels[0, 0])  # Show the labeled components
        tensor([[0, 0, 0, 0],
                [0, 1, 1, 0],
                [0, 1, 1, 0],
                [0, 0, 0, 0]])
    """
    components = connected_components(image, num_iterations=num_iterations)

    # remap component values from 0 to N
    labels = components.unique()
    for new_label, old_label in enumerate(labels):
        components[components == old_label] = new_label

    return components.int()
Perform connected component labeling on GPU.

    Labels connected regions in a binary image and remaps the labels sequentially
    from 0 to N, where N is the number of unique components. Uses the GPU for
    faster processing of large images.

    Args:
        image (torch.Tensor): Binary input image tensor of shape ``(B, 1, H, W)``
            where ``B`` is batch size, ``H`` is height and ``W`` is width.
            Values should be binary (0 or 1).
        num_iterations (int, optional): Number of iterations for the connected
            components algorithm. Higher values may be needed for complex regions.
            Defaults to 1000.

    Returns:
        torch.Tensor: Integer tensor of same shape as input, containing labeled
            components from 0 to N. Background (zero) pixels in the input remain
            ``0``, while connected regions are labeled with integers from ``1``
            to ``N``.

    Example:
        >>> import torch
        >>> from anomalib.utils.cv import connected_components_gpu
        >>> # Create a binary mask with a 2x2 square anomaly
        >>> mask = torch.zeros(1, 1, 4, 4)
        >>> mask[0, 0, 1:3, 1:3] = 1
        >>> labels = connected_components_gpu(mask)
        >>> print(labels.unique())  # Should show tensor([0, 1])
        >>> print(labels[0, 0])  # Show the labeled components
        tensor([[0, 0, 0, 0],
                [0, 1, 1, 0],
                [0, 1, 1, 0],
                [0, 0, 0, 0]])
    
src/anomalib/utils/cv/connected_components.py