
    3j$                        S SK Jr  S SKrS SKrS SKJr  S SKJrJrJrJ	r	  S SK
JrJr  S SKJrJr  SS jrSS jrSSS	 jjr " S
 S\R&                  5      rg)    )annotationsN)nn)KORNIA_CHECKKORNIA_CHECK_IS_TENSORKORNIA_CHECK_SHAPEKORNIA_CHECK_TYPE)filter2dfilter3d)create_meshgridcreate_meshgrid3dc                .   U R                   nU R                  nUS-  n[        R                  " [	        U R
                  S   U R
                  S   5      U-  5      n[        XSX45      nXu-
  n[        R                  " US   US   5      n[        R                  " U* U-  5      R                  S5      n	[        R                  " U 5      n
U R                  5       n[        R                  " U5      n[        U5       H  n[        XSS9nU* [        R                   " U5      -  n[        R"                  " US	S	S	S
9nUS:  nUR%                  5       (       d    U
$ X-  nU
UU-   UR'                  U
R                  S9-  -   n
[        R(                  " XU5      nM     U
$ )N      F)r   .r   )r   .   r   	replicateborder_type        nanposinfneginfdtype)devicer   mathceilmaxshaper   torchhypotexp	unsqueeze
zeros_likeclone	ones_likeranger	   log
nan_to_numanytowhereimagekernel_sizehr   r   k_halfn_itersgriddistkerneloutboundarysignal_onesicdtmaskoffsets                    [/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/contrib/distance_transform.py_distance_transform_2d_implr>      sa   \\FKKEAFiiEKKNEKKN;fDEG;UFJD=D;;tIY8DYYuqy!++A.F


5
!C{{}H//(+K7^x[Ab599S>!sCDQwxxzz J	 jVc\TWW399W%===;;t(;  J    c                   U R                   nU R                  nUS-  n[        R                  " [	        U R
                  SS  5      U-  5      n[        XUSX45      nXu-
  n[        R                  " US   SSS9n[        R                  " U* U-  5      R                  S5      n	[        R                  " U 5      n
U R                  5       n[        R                  " U5      n[        U5       H  n[        XSS9nU* [        R                   " U5      -  n[        R"                  " USSSS	9nUS:  nUR%                  5       (       d    U
$ X-  nU
UU-   UR'                  U
R                  S
9-  -   n
[        R(                  " XU5      nM     U
$ )Nr   Fr   )pdimr   r   r   r   r   )r   r   r   r   r   r   r   r    normr"   r#   r$   r%   r&   r'   r
   r(   r)   r*   r+   r,   r-   s                    r=   _distance_transform_3d_implrE   >   sY   \\FKKEAFiiEKKO,v56G[{E6YD=D::d1g+DYYuqy!++A.F


5
!C{{}H//(+K7^x[Ab599S>!sCDQwxxzz J	 jVc\TWW399W%===;;t(;  Jr?   c                   [        U 5        [        U R                  5       S5        [        U R                  S;   SU R                   35        U R                  S:X  a  [        U / SQ5        O[        U / SQ5        [        U[        S5        [        US-  S	:g  =(       a    US
:  S5        [        US	:  SU 35        U R                  S:X  a  [        XU5      $ [        XU5      $ )as  Approximates the Euclidean distance transform of images/volumes using cascaded convolution operations.

The value at each pixel/voxel represents the distance to the nearest non-zero element.
It uses the method described in :cite:`pham2021dtlayer`.
The transformation is applied independently across the channel dimension.

Args:
    image: Image or volume with shape :math:`(B,C,H,W)` or :math:`(B,C,D,H,W)`.
    kernel_size: size of the convolution kernel. Must be an odd number.
    h: value that influence the approximation of the min function.

Returns:
    tensor with the same shape as input.

Example:
    >>> # 2D example:
    >>> tensor = torch.zeros(1, 1, 5, 5)
    >>> tensor[:,:, 1, 2] = 1
    >>> dt = distance_transform(tensor)
    >>> # 3D example:
    >>> volume = torch.zeros(1, 1, 5, 5, 5)
    >>> volume[:, :, 2, 2, 2] = 1
    >>> dt = distance_transform(volume)

z%image must be a floating point tensor)      z:Invalid image shape, we expect BxCxHxW or BxCxDxHxW. Got: rG   )BCHW)rI   rJ   DrK   rL   zkernel_size must be an intr   r   r   z'kernel_size must be an odd integer >= 3z h must be a positive float, got )
r   r   is_floating_pointndimr   r   r   intr>   rE   )r.   r/   r0   s      r=   distance_transformrQ   ]   s    6 5!((*,STv%)cdidodocp'qrzzQ5"675";< k3(DEqA%:+*:<efQ:1#>?zzQ*5qAA&u1==r?   c                  >   ^  \ rS rSrSrSSU 4S jjjrSS jrSrU =r$ )	DistanceTransform   zModule that approximates the Euclidean distance transform of images/volumes using convolutions.

Args:
    kernel_size: size of the convolution kernel.
    h: value that influence the approximation of the min function.

c                :   > [         TU ]  5         Xl        X l        g )N)super__init__r/   r0   )selfr/   r0   	__class__s      r=   rW   DistanceTransform.__init__   s    &r?   c                    UR                   S   S:  a$  UR                   SS nUR                  " SS/UQ76 nOUn[        X0R                  U R                  5      R                  U5      $ )a  Compute the distance transform while preserving the original tensor layout.

The underlying functional implementation expects a single-channel tensor.
For multi-channel inputs, channels are temporarily folded into the batch
dimension, processed independently, and then reshaped back.

Args:
    image: Input tensor with shape :math:`(B, C, H, W)` for 2D data or
        :math:`(B, C, D, H, W)` for 3D data, where:
        - ``B`` is batch size,
        - ``C`` is the number of channels,
        - ``D`` is depth (only for 3D volumes),
        - ``H`` is height,
        - ``W`` is width.

Returns:
    A distance-transform tensor with the same shape as ``image``.
    Each channel is processed independently.
r   r   NrA   )r   reshaperQ   r/   r0   view_as)rX   r.   spatial_dimsimage_ins       r=   forwardDistanceTransform.forward   s_    * ;;q>A ;;qr?L}}R:\:HH!(,<,<dffEMMeTTr?   )r0   r/   r   gffffff?)r/   rP   r0   floatreturnNone)r.   torch.Tensorrd   rf   )	__name__
__module____qualname____firstlineno____doc__rW   r`   __static_attributes____classcell__)rY   s   @r=   rS   rS      s     
U Ur?   rS   )r.   rf   r/   rP   r0   rc   rd   rf   rb   )
__future__r   r   r    r   kornia.core.checkr   r   r   r   kornia.filtersr	   r
   kornia.geometry.gridr   r   r>   rE   rQ   ModulerS    r?   r=   <module>rt      sB   $ #    i i - C@>->`+U		 +Ur?   