+
    ÿäûj],  ã                   ó<   € ^ RI t ^ RIHu Ht RR R lltR R ltR# )é    Nc                ód   € V ^8„  d   QhR\         P                  R\        R\         P                  /# )é   ÚimageÚnum_iterationsÚreturn)ÚtorchÚTensorÚint)Úformats   "Ú_C:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\contrib\connected_components.pyÚ__annotate__r      s.   € ÷ Y<ñ Y<¤§¡ð Y<¼cð Y<ÌEÏLÉLñ Y<ó    c                óŽ  € \        V \        P                  4      '       g   \        R\	        V 4       24      h\        V\
        4      '       d   V^8  d   \        R4      h\        V P                  4      ^8  g   V P                  R,          ^8w  d   \        RV P                   24      hV P                  RR w  r#V P                  4       ^ 8X  d<   \        P                  ! V P                  \        P                  V P                  R7      # V P                  R^W#4      pVP                  ^ ,          pW#,          p\        P                  pVR8”  d;   V P                  P                  R8X  d   \        RV R24      h\        P                  pV^8H  p\        P                   ! ^V^,           V P                  VR	7      P#                  ^^W#4      P%                  V^W#4      p	W˜,          p	\'        V4       F3  p
\(        P*                  ! V	^^^R
7      p	\        P,                  ! W˜4      p	K5  	  V	P/                  \        P                  4      p\        P                   ! WPP                  \        P                  R	7      P#                  R^^^4      V,          pVP1                  V4      P3                  V4      P#                  V P                  4      # )aS	  Label 8-connected foreground components by a fixed number of max-pooling steps.

``labels = connected_components(image, num_iterations)`` takes a ``(*, 1, H, W)`` mask whose pixels equal to 1 are
foreground and returns ``int64`` labels of the same shape, 0 on the background. A component comes out whole only
when every pixel is within ``num_iterations`` 8-connected steps, inside the component, of its last pixel in raster
order; otherwise it is split. For the exact partition at any size, use :func:`connected_components_union_find`.

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

The implementation adapts 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://www.kornia.org/tutorials/nbs/connected_components.html>`__.

Convention:
    - Components are 8-connected: pixels that touch at a corner are connected. :ref:`labeling-method` maps this
      onto SciPy, OpenCV and scikit-image.
    - Foreground is a value exactly equal to 1: 0.5 and 2 are background. Other values and dtypes are accepted.
    - The label of a component is 1 plus the largest raster index of its pixels in the batch flattened to
      ``(N, H, W)``; background is 0. Labels are therefore not contiguous, and an image gets other labels at
      another batch position or transposed.
    - The default 100 already splits a 1 x 102 line, and a filled 120 x 150 rectangle comes out with 1000 labels.
    - The labels carry no gradient. The function has no host synchronization and compiles with
      ``torch.compile(fullgraph=True)``.

Args:
    image: the binary input image with shape ``(*, 1, H, W)``.
    num_iterations: the number of max-pooling steps, at least 1.

Return:
    int64 labels with the shape of the input.

Raises:
    TypeError: if ``image`` is not a tensor or ``num_iterations`` is not a positive integer.
    ValueError: if ``image`` is not ``(*, 1, H, W)``, or, on MPS, has more than :math:`2^{24}` pixels per image.

See Also:
    - :func:`connected_components_union_find`: the exact partition with no iteration budget, at the cost of host
      synchronizations and other label values.

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.ú*Input image shape must be (*,1,H,W). Got: N)ÚdtypeÚdeviceÚmpszconnected_components labels zi pixels in one image; on MPS (no float64) the limit is 2**24 pixels per image. Run it on the CPU instead.)r   r   )Úkernel_sizeÚstrideÚpaddingéýÿÿÿéþÿÿÿéÿÿÿÿi   )Ú
isinstancer   r	   Ú	TypeErrorÚtyper
   ÚlenÚshapeÚ
ValueErrorÚnumelÚzerosÚint64r   ÚreshapeÚfloat32Úfloat64ÚarangeÚviewÚexpandÚrangeÚFÚ
max_pool2dÚmulÚtoÚadd_Úmul_)r   r   ÚHÚWÚ
image_viewÚBÚnum_idsÚid_dtypeÚmaskÚoutÚ_ÚlabelsÚoffsets   &&           r   Úconnected_componentsr;      s*  € ôb �eœUŸ\™\×*Ò*ÜÐFÄtÈEÃ{ÀmÐTÓUÐUä�n¤c×*Ò*¨n¸qÔ.@ÜÐJÓKÐKä
ˆ5�;‰;Ó˜!Ô˜uŸ{™{¨2�°!Ô3ÜÐEÀeÇkÁkÀ]ÐSÓTÐTà�;‰;�r�sÐ�D€Aà‡{�{ƒ}˜ÔÜ�{Š{˜5Ÿ;™;¬e¯k©kÀ%Ç,Á,ÔOÐOØ—‘˜r 1 aÓ+€JØ×Ñ˜Õ€AØ�e€Gô �}‰}€HØ�„Ø�<‰<×Ñ Ô%ÜØ.¨w¨ið 8?ð ?óð ô —=‘=ˆð ˜‰?€DÜ
�,Š,�q˜' A�+¨e¯l©lÀ(Ô
K×
PÑ
PÐQRÐTUÐWXÓ
\×
cÑ
cÐdeÐghÐjkÓ
o€CØ
�*€Cä�>Ö"ˆÜ�lŠl˜3¨A°aÀÔCˆÜ�iŠi˜Ó"Šñ #ð �V‰V”E—K‘KÓ €FÜ�\Š\˜!§L¡L¼¿¹ÔD×IÑIÈ"ÈaÐQRÐTUÓVÐY`Õ`€Fà�;‰;�vÓ×#Ñ# DÓ)×.Ñ.¨u¯{©{Ó;Ð;r   c                óX   € V ^8„  d   QhR\         P                  R\         P                  /# )r   r   r   )r   r	   )r   s   "r   r   r   r   s*   € ÷ wDñ wD¬5¯<©<ð wD¼E¿L¹Lñ wDr   c                ó$  € \        V \        P                  4      '       g   \        R\	        V 4       24      hV P
                  ^8  g   V P                  R,          ^8w  d   \        RV P                   24      hV P                  4       ^ 8X  d'   \        P                  ! V \        P                  R7      # V P                  RR w  rV ^8H  P                  RW4      pVP                  VP                  ^ ,          W^,          ,           W"^,          ,           34      pW4RRV1RV13&   VR,          VR,          reVR,          VR,          r‡\        P                  ! VP                  4       V P                  R7      P                  VP                  4      p	VR,          VR,          ,          VRRRR13,          VRRRR13,          ,          ,          p
VR,          VR,          ,          VRRR1R3,          VRRR1R3,          ,          ,          pVR,          VRRR1RR13,          ,          pVRRRR13,          VRRR1R3,          ,          p\        P                  ! V	R,          P!                  4       V	R,          P!                  4       V	R,          P!                  4       V	RRRR13,          P!                  4       .4      p\        P                  ! \        P"                  ! W©RRRR13,          V	R,          4      P!                  4       \        P"                  ! W¹RRR1R3,          V	R,          4      P!                  4       \        P"                  ! WÉRRR1RR13,          V	R,          4      P!                  4       \        P"                  ! WÙRRR1R3,          V	RRRR13,          4      P!                  4       .4      pV	P!                  4       pV P                  P                  R8X  dh   VP                  4       \        P$                  ! \        P&                  4      P(                  ^,           8:  d    VP+                  \        P&                  4      p VV,          VV,          ppVP-                  4       pVP/                  ^ \        P0                  ! VV4      P3                  4       \        P4                  ! VV4      R	R
7        VVP3                  4       ,          p\        P6                  ! VV4      '       d   MTpK:  \        P6                  ! VV4      '       d   MTpKÌ  VP3                  4       P                  V	P                  4      ^,           P9                  ^R4      P9                  ^R4      pVRRV1RV13,          V,          P                  V P                  4      # )aˆ  Label 8-connected foreground components exactly, by block union-find.

``labels = connected_components_union_find(image)`` takes a ``(*, 1, H, W)`` mask whose pixels equal to 1 are
foreground and returns ``int64`` labels of the same shape and device, 0 on the background. It iterates until the
partition converges, so components of any size and shape come out whole.

The pure PyTorch implementation contracts each 2x2 block, following Allegretti, Bolelli and Grana, *Optimized
Block-Based Algorithms to Label Connected Components on GPUs*, TPDS 2020,
https://doi.org/10.1109/TPDS.2019.2934683. Root hooking with ``scatter_reduce_`` and pointer jumping replace the
paper's custom CUDA union kernels.

Convention:
    :ref:`labeling-method` maps its 8-connectivity onto SciPy, OpenCV and scikit-image, and compares it with
    :func:`connected_components`.

Args:
    image: Binary image of shape ``(*, 1, H, W)``. Boolean, integer and
        floating-point tensors are accepted. Only values equal to 1 are
        foreground, as in :func:`connected_components`.

Returns:
    Foreground labels are the minimum 1-based block
    index in each component, in raster order over flattened batches and
    the :math:`\lceil H/2\rceil \times \lceil W/2\rceil` block grid.
    Labels are deterministic and distinct across images, but not contiguous
    and not equal to the labels of the pooling implementation.

Note:
    This discrete operation is not differentiable. Working memory is linear
    in the number of pixels. Convergence checks synchronize the device with
    the host; CUDA graph capture and ``torch.compile(fullgraph=True)`` are
    not supported. All mask, edge and label computation stays on the input
    device.

Raises:
    TypeError: if ``image`` is not a tensor.
    ValueError: if ``image`` is not ``(*, 1, H, W)``.

See Also:
    - :func:`connected_components`: a fixed number of pooling steps, which compiles with ``fullgraph=True`` and
      splits components longer than its budget.

Example:
    >>> mask = torch.tensor([[[1, 0, 0], [0, 0, 1]]], dtype=torch.bool)
    >>> connected_components_union_find(mask)
    tensor([[[1, 0, 0],
             [0, 0, 2]]])

z(Input image is not a torch.Tensor. Got: r   )r   NºNNN)r   ºé   NNr   Úamin)Úreducer   r   r   )r>   ºNNr   rC   )r>   rC   ºr@   Nr   )r>   rD   rC   )r>   rD   rD   )r>   r>   r?   )r>   r?   r>   )r>   r?   r?   )r   r   r	   r   r   Úndimr   r   r    Ú
zeros_liker"   r#   Ú	new_zerosr&   r   ÚcatÚflattenÚwhereÚiinfoÚint32Úmaxr-   ÚcloneÚscatter_reduce_ÚmaximumÚlongÚminimumÚequalÚrepeat_interleave)r   ÚheightÚwidthr6   ÚpaddedÚtop_leftÚ	top_rightÚbottom_leftÚbottom_rightÚblocksÚleftÚaboveÚ
above_leftÚabove_rightÚsourcesÚtargetsÚparentsÚsource_rootsÚtarget_rootsÚmergedÚ
compressedr9   s   &                     r   Úconnected_components_union_findrh   r   s§  € ôd �eœUŸ\™\×*Ò*ÜÐBÄ4ÈÃ;À-ÐPÓQÐQØ‡z�z�A„~˜Ÿ™ R�¨AÔ-ÜÐEÀeÇkÁkÀ]ÐSÓTÐTØ‡{�{ƒ}˜ÔÜ×Ò ¬U¯[©[Ô9Ð9à—K‘K  Ð$�M€FØ�Q‰J×Ñ  FÓ2€Dà�^‰^˜TŸZ™Z¨�]¨F¸aµZÕ,?ÀÐQRÍÕARÐSÓT€FØ!%ˆ1ˆgˆvˆg�v˜�vÐÑØ  Õ-¨v°lÕ/CˆiØ & |Õ 4°f¸]Õ6K�ô �\Š\˜(Ÿ.™.Ó*°5·<±<Ô@×HÑHÈÏÉÓX€FØ�XÕ ¨XÕ!6Õ6¸9ÀQÈÈ3ÈBÈ3ÀYÕ;OÐR^Ð_`ÐbcÐehÐfhÐehÐ_hÕRiÕ;iÕj€DØ�hÕ )¨HÕ"5Õ5¸+ÀaÈÈ"ÈÈaÀiÕ:PÐS_Ð`aÐcfÐdfÐcfÐhiÐ`iÕSjÕ:jÕk€EØ˜)Õ$ |°A°s¸°s¸C¸R¸C°KÕ'@Õ@€JØ˜A˜r 3 B 3˜JÕ'¨+°a¸¸"¸¸b°jÕ*AÕA€Kä�iŠià�8Õ×$Ñ$Ó&Ø�8Õ×$Ñ$Ó&Ø�9Õ×%Ñ%Ó'Ø�1�b˜#˜2˜#�:Õ×&Ñ&Ó(ð		
ó€Gô �iŠiä�KŠK˜ Q¨¨3¨B¨3 YÕ/°¸Õ1AÓB×JÑJÓLÜ�KŠK˜ a¨¨"¨¨a iÕ0°&¸Õ2BÓC×KÑKÓMÜ�KŠK˜
¨1¨c¨r¨c°3°B°3¨;Õ$7¸À	Õ9JÓK×SÑSÓUÜ�KŠK˜¨A¨s°¨s°B¨JÕ%7¸ÀÀ2ÀsÈÀsÀ
Õ9KÓL×TÑTÓVð		
ó€Gð �n‰nÓ€Gð ‡|�|×Ñ˜EÔ! g§m¡m£o¼¿ºÄUÇ[Á[Ó9Q×9UÑ9UÐXYÕ9YÔ&YØ—*‘*œUŸ[™[Ó)ˆØ
Ø%,¨WÕ%5°w¸wÕ7G�lˆØ—‘“ˆØ×ÑØÜ�MŠM˜,¨Ó5×:Ñ:Ó<Ü�MŠM˜,¨Ó5Øð	 	ô 	
ð Ø §¡£Õ.ˆJÜ�{Š{˜: v×.Ò.ØØŠFÜ�;Š;�v˜w×'Ò'ØØŠð �l‰l‹n×$Ñ$ V§\¡\Ó2°QÕ6×IÑIÈ!ÈRÓP×bÑbÐcdÐfhÓi€FØ�1�g�v�g˜v ˜vÐ%Õ&¨Õ-×6Ñ6°u·{±{ÓCÐCr   )éd   )r   Útorch.nn.functionalÚnnÚ
functionalr*   r;   rh   © r   r   Ú<module>rn      s   ðó$ ß Ð ÷Y<÷xwDr   