
    3j9                        S SK JrJr  S SKrS SKJr  S SKJr  S SKJrJ	r	  S\
S\4S jr   SS	\R                  S
\\R                  \
4   S\S\\R                     S\R                  4
S jjr SS	\R                  S\S\\R                     S\R                  4S jjr SS	\R                  S\S\\R                     S\R                  4S jjrS	\R                  S\R                  4S jrS S	\R                  S\
S\
S\R                  4S jjr " S S\R(                  5      r " S S\R(                  5      r " S S\R(                  5      r " S S\R(                  5      r " S S\R(                  5      rg)!    )OptionalUnionN)nnKORNIA_CHECK_SHAPE)gaussian_blur2dspatial_gradientsigmareturnc                 D    [        SU -  S-   5      nUS-  S:X  a  US-  nU$ )Ng       @      ?   r      )int)r
   ksizes     R/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/feature/responses.py_get_kernel_sizer      s1    	E!C'(E qyA~
L    inputk
grads_modesigmasc                 `   [        U / SQ5        Ub  [        U[        R                  5      (       d  [	        S[        U5       35      e[        UR                  5      S:X  a$  UR                  S5      U R                  S5      :w  a  [        SUR                   35      e[        X5      nUSS2SS2S4   nUSS2SS2S4   n[        US-  SS	5      n[        US-  SS	5      n[        XV-  SS	5      n	Xx-  X-  -
  n
Xx-   nXUS-  -  -
  nUb%  XR                  S
5      R                  SSSS5      -  nU$ )u  Compute the Harris cornerness function.

.. image:: _static/img/harris_response.png

Function does not do any normalization or nms. The response map is computed according the following formulation:

.. math::
    R = max(0, det(M) - k \cdot trace(M)^2)

torch.where:

.. math::
    M = \sum_{(x,y) \in W}
    \begin{bmatrix}
        I^{2}_x & I_x I_y \\
        I_x I_y & I^{2}_y \\
    \end{bmatrix}

and :math:`k` is an empirically determined constant
:math:`k ∈ [ 0.04 , 0.06 ]`

Args:
    input: input image with shape :math:`(B, C, H, W)`.
    k: the Harris detector free parameter.
    grads_mode: can be ``'sobel'`` for standalone use or ``'diff'`` for use on Gaussian pyramid.
    sigmas: coefficients to be multiplied by multichannel response. Should be shape of :math:`(B)`
      It is necessary for performing non-maxima-suppression across different scale pyramid levels.
      See `vlfeat <https://github.com/vlfeat/vlfeat/blob/master/vl/covdet.c#L874>`_.

Return:
    the response map per channel with shape :math:`(B, C, H, W)`.

Example:
    >>> input = torch.tensor([[[
    ...    [0., 0., 0., 0., 0., 0., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 0., 0., 0., 0., 0., 0.],
    ... ]]])  # 1x1x7x7
    >>> # compute the response map
    harris_response(input, 0.04)
    tensor([[[[0.0012, 0.0039, 0.0020, 0.0000, 0.0020, 0.0039, 0.0012],
              [0.0039, 0.0065, 0.0040, 0.0000, 0.0040, 0.0065, 0.0039],
              [0.0020, 0.0040, 0.0029, 0.0000, 0.0029, 0.0040, 0.0020],
              [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
              [0.0020, 0.0040, 0.0029, 0.0000, 0.0029, 0.0040, 0.0020],
              [0.0039, 0.0065, 0.0040, 0.0000, 0.0040, 0.0065, 0.0039],
              [0.0012, 0.0039, 0.0020, 0.0000, 0.0020, 0.0039, 0.0012]]]])

BCHWN'sigmas type is not a torch.Tensor. Got r   r   9Invalid sigmas shape, we expect B == input.size(0). Got: r      r"   r   r      )r   
isinstancetorchTensor	TypeErrortypelenshapesize
ValueErrorr	   r   powview)r   r   r   r   	gradientsdxdydx2dy2dxydet_mtrace_mscoress                r   harris_responser:   '   s5   x u23&%,,//Ed6l^TUUFLL!Q&FKKNejjm,KXY_YeYeXfghh.uAI Aq)B Aq)B (AvzBC'AvzBC'DC)ci/EIG !
#33F**Q-,,RAq99Mr   c                 (   [        U / SQ5        [        X5      nUSS2SS2S4   nUSS2SS2S4   n[        US-  SS5      n[        US-  SS5      n[        XE-  SS5      nXg-  X-  -
  n	Xg-   n
SU
[        R                  " U
S-  S	U	-  -
  R                  5       5      -   -  nSU
[        R                  " U
S-  S	U	-  -
  R                  5       5      -
  -  n[        R                  " X5      nUb%  XR                  S	5      R                  S
SSS5      -  nU$ )a  Compute the Shi-Tomasi cornerness function.

.. image:: _static/img/gftt_response.png

Function does not do any normalization or nms. The response map is computed according the following formulation:

.. math::
    R = min(eig(M))

torch.where:

.. math::
    M = \sum_{(x,y) \in W}
    \begin{bmatrix}
        I^{2}_x & I_x I_y \\
        I_x I_y & I^{2}_y \\
    \end{bmatrix}

Args:
    input: input image with shape :math:`(B, C, H, W)`.
    grads_mode: can be ``'sobel'`` for standalone use or ``'diff'`` for use on Gaussian pyramid.
    sigmas: coefficients to be multiplied by multichannel response. Should be shape of :math:`(B)`
      It is necessary for performing non-maxima-suppression across different scale pyramid levels.
      See `vlfeat <https://github.com/vlfeat/vlfeat/blob/master/vl/covdet.c#L874>`_.

Return:
    the response map per channel with shape :math:`(B, C, H, W)`.

Example:
    >>> input = torch.tensor([[[
    ...    [0., 0., 0., 0., 0., 0., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 0., 0., 0., 0., 0., 0.],
    ... ]]])  # 1x1x7x7
    >>> # compute the response map
    gftt_response(input)
    tensor([[[[0.0155, 0.0334, 0.0194, 0.0000, 0.0194, 0.0334, 0.0155],
              [0.0334, 0.0575, 0.0339, 0.0000, 0.0339, 0.0575, 0.0334],
              [0.0194, 0.0339, 0.0497, 0.0000, 0.0497, 0.0339, 0.0194],
              [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
              [0.0194, 0.0339, 0.0497, 0.0000, 0.0497, 0.0339, 0.0194],
              [0.0334, 0.0575, 0.0339, 0.0000, 0.0339, 0.0575, 0.0334],
              [0.0155, 0.0334, 0.0194, 0.0000, 0.0194, 0.0334, 0.0155]]]])

r   Nr   r   r   r!   r#   g      ?r$   r%   )	r   r	   r   r'   sqrtabsminr/   r0   )r   r   r   r1   r2   r3   r4   r5   r6   r7   r8   e1e2r9   s                 r   gftt_responserA      s   j u23.uAI Aq)B Aq)B'AvzBC'AvzBC'DC)ci/EIGg

GQJU4J3O3O3Q(RRSBg

GQJU4J3O3O3Q(RRSB 99R,F**Q-,,RAq99Mr   c                    [        U / SQ5        Ub  [        U[        R                  5      (       d  [	        S[        U5       35      e[        UR                  5      S:X  a$  UR                  S5      U R                  S5      :w  a  [        SUR                   35      e[        XS5      nUSS2SS2S4   nUSS2SS2S4   nUSS2SS2S4   nXF-  US-  -
  nUb%  XrR                  S5      R                  S	SSS5      -  nU$ )
as  Compute the absolute of determinant of the Hessian matrix.

.. image:: _static/img/hessian_response.png

Function does not do any normalization or nms. The response map is computed according the following formulation:

.. math::
    R = det(H)

torch.where:

.. math::
    M = \sum_{(x,y) \in W}
    \begin{bmatrix}
        I_{xx} & I_{xy} \\
        I_{xy} & I_{yy} \\
    \end{bmatrix}

Args:
    input: input image with shape :math:`(B, C, H, W)`.
    grads_mode: can be ``'sobel'`` for standalone use or ``'diff'`` for use on Gaussian pyramid.
    sigmas: coefficients to be multiplied by multichannel response. Should be shape of :math:`(B)`
      It is necessary for performing non-maxima-suppression across different scale pyramid levels.
      See `vlfeat <https://github.com/vlfeat/vlfeat/blob/master/vl/covdet.c#L874>`_.

Return:
    the response map per channel with shape :math:`(B, C, H, W)`.

Shape:
   - Input: :math:`(B, C, H, W)`
   - Output: :math:`(B, C, H, W)`

Examples:
    >>> input = torch.tensor([[[
    ...    [0., 0., 0., 0., 0., 0., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 1., 1., 1., 1., 1., 0.],
    ...    [0., 0., 0., 0., 0., 0., 0.],
    ... ]]])  # 1x1x7x7
    >>> # compute the response map
    hessian_response(input)
    tensor([[[[0.0155, 0.0334, 0.0194, 0.0000, 0.0194, 0.0334, 0.0155],
              [0.0334, 0.0575, 0.0339, 0.0000, 0.0339, 0.0575, 0.0334],
              [0.0194, 0.0339, 0.0497, 0.0000, 0.0497, 0.0339, 0.0194],
              [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
              [0.0194, 0.0339, 0.0497, 0.0000, 0.0497, 0.0339, 0.0194],
              [0.0334, 0.0575, 0.0339, 0.0000, 0.0339, 0.0575, 0.0334],
              [0.0155, 0.0334, 0.0194, 0.0000, 0.0194, 0.0334, 0.0155]]]])

r   Nr   r   r   r    r   r$   r%   )r   r&   r'   r(   r)   r*   r+   r,   r-   r.   r	   r/   r0   )r   r   r   r1   dxxr6   dyyr9   s           r   hessian_responserE      s    r u23&%,,//Ed6l^TUUFLL!Q&FKKNejjm,KXY_YeYeXfghh.u!DI!!Q'*C!!Q'*C!!Q'*C9sAv-F**Q-,,RAq99Mr   c                 X    [        U / SQ5        U SS2SS2SS24   U SS2SS2SS24   -
  $ )zCompute the Difference-of-Gaussian response.

Args:
    input: a given the gaussian 5d torch.Tensor :math:`(B, C, D, H, W)`.

Return:
    the response map per channel with shape :math:`(B, C, D-1, H, W)`.

)r   r   Lr   r   Nr   r%   r   )r   s    r   dog_responserH     s5     u78Aqr?U1a"9---r   sigma1sigma2c                     [        U / SQ5        [        U5      n[        U5      n[        XU4X45      n[        XU4X"45      nXe-
  $ )a9  Compute the Difference-of-Gaussian response.

.. image:: _static/img/dog_response_single.png

Args:
    input: a given the gaussian 4d torch.Tensor :math:`(B, C, H, W)`.
    sigma1: lower gaussian sigma
    sigma2: bigger gaussian sigma

Return:
    the response map per channel with shape :math:`(B, C, H, W)`.

r   )r   r   r   )r   rI   rJ   ks1ks2g1g2s          r   dog_response_singlerP   ,  sP     u23
6
"C
6
"C	c
V,<	=B	c
V,<	=B7Nr   c                      ^  \ rS rSrSrS
U 4S jjrS\4S jrSS\R                  S\
\R                     S\R                  4S jjrS	rU =r$ )BlobDoGiB  zqnn.Module that calculates Difference-of-Gaussians blobs.

See
:func: `~kornia.feature.dog_response` for details.
r   c                 "   > [         TU ]  5         g N)super__init__)self	__class__s    r   rV   BlobDoG.__init__I  s    r   c                 .    U R                   R                  $ rT   )rX   __name__rW   s    r   __repr__BlobDoG.__repr__L  s    ~~&&&r   r   r   c                     [        U5      $ rT   )rH   rW   r   r   s      r   forwardBlobDoG.forwardO  s    E""r    )r   NrT   )r[   
__module____qualname____firstlineno____doc__rV   strr]   r'   r(   r   ra   __static_attributes____classcell__rX   s   @r   rR   rR   B  sL    '# '#U\\ #8ELL3I #UZUaUa # #r   rR   c                      ^  \ rS rSrSrSS\S\SS4U 4S jjjrS\4S jrSS	\	R                  S
\\	R                     S\	R                  4S jjrSrU =r$ )BlobDoGSingleiS  znn.Module that calculates Difference-of-Gaussians blobs.

.. image:: _static/img/dog_response_single.png

See :func:`~kornia.feature.dog_response_single` for details.
rI   rJ   r   Nc                 :   > [         TU ]  5         Xl        X l        g rT   )rU   rV   rI   rJ   )rW   rI   rJ   rX   s      r   rV   BlobDoGSingle.__init__[  s    r   c                 h    U R                   R                   SU R                   SU R                   S3$ )Nz	, sigma1=z	, sigma2=))rX   r[   rI   rJ   r\   s    r   r]   BlobDoGSingle.__repr__`  s/    ..))*)DKK=	$++VWXXr   r   r   c                 B    [        XR                  U R                  5      $ rT   )rP   rI   rJ   r`   s      r   ra   BlobDoGSingle.forwardc  s    "5++t{{CCr   )rI   rJ   r   g?rT   )r[   rd   re   rf   rg   floatrV   rh   r]   r'   r(   r   ra   ri   rj   rk   s   @r   rm   rm   S  sm    u E D  
Y# YDU\\ D8ELL3I DUZUaUa D Dr   rm   c                      ^  \ rS rSr% Sr\R                  \S'   SS\\	\R                  4   S\
SS4U 4S jjjrS\
4S jrSS	\R                  S
\\R                     S\R                  4S jjrSrU =r$ )CornerHarrisig  znn.Module that calculates Harris corners.

.. image:: _static/img/harris_response.png

See :func:`~kornia.feature.harris_response` for details.
r   r   r   Nc                    > [         TU ]  5         [        U[        5      (       a'  U R	                  S[
        R                  " U5      5        OU R	                  SU5        X l        g )Nr   )rU   rV   r&   rv   register_bufferr'   tensorr   )rW   r   r   rX   s      r   rV   CornerHarris.__init__q  sH    a  ell1o6  a()r   c                 h    U R                   R                   SU R                   SU R                   S3$ )Nz(k=z, grads_mode=rq   )rX   r[   r   r   r\   s    r   r]   CornerHarris.__repr__y  s0    ..))*#dffX]4??BSSTUUr   r   r   c                 D    [        XR                  U R                  U5      $ rT   )r:   r   r   r`   s      r   ra   CornerHarris.forward|  s    uffdoovFFr   r   sobelrT   )r[   rd   re   rf   rg   r'   r(   __annotations__r   rv   rh   rV   r]   r   ra   ri   rj   rk   s   @r   rx   rx   g  s     ||O*%u|| 34 *# *TX * *V# VGU\\ G8ELL3I GUZUaUa G Gr   rx   c                      ^  \ rS rSrSrSS\SS4U 4S jjjrS\4S jrSS\R                  S	\
\R                     S\R                  4S
 jjrSrU =r$ )
CornerGFTTi  znn.Module that calculates Shi-Tomasi corners.

.. image:: _static/img/gftt_response.png

See :func:`~kornia.feature.gftt_response` for details.
r   r   Nc                 .   > [         TU ]  5         Xl        g rT   rU   rV   r   rW   r   rX   s     r   rV   CornerGFTT.__init__      )r   c                 N    U R                   R                   SU R                   S3$ Nz(grads_mode=rq   rX   r[   r   r\   s    r   r]   CornerGFTT.__repr__  %    ..))*,t6GqIIr   r   r   c                 .    [        XR                  U5      $ rT   )rA   r   r`   s      r   ra   CornerGFTT.forward  s    UOOV<<r   r   r   rT   r[   rd   re   rf   rg   rh   rV   r]   r'   r(   r   ra   ri   rj   rk   s   @r   r   r     sa    *3 *T * *J# J=U\\ =8ELL3I =UZUaUa = =r   r   c                      ^  \ rS rSrSrSS\SS4U 4S jjjrS\4S jrSS\R                  S	\
\R                     S\R                  4S
 jjrSrU =r$ )BlobHessiani  znn.Module that calculates Hessian blobs.

.. image:: _static/img/hessian_response.png

See :func:`~kornia.feature.hessian_response` for details.
r   r   Nc                 .   > [         TU ]  5         Xl        g rT   r   r   s     r   rV   BlobHessian.__init__  r   r   c                 N    U R                   R                   SU R                   S3$ r   r   r\   s    r   r]   BlobHessian.__repr__  r   r   r   r   c                 .    [        XR                  U5      $ rT   )rE   r   r`   s      r   ra   BlobHessian.forward  s    ??r   r   r   rT   r   rk   s   @r   r   r     sf    *3 *T * *J# J@U\\ @8ELL3I @UZUaUa @ @r   r   )g{Gz?r   N)r   Nru   )typingr   r   r'   r   kornia.core.checkr   kornia.filtersr   r	   rv   r   r   r(   rh   r:   rA   rE   rH   rP   ModulerR   rm   rx   r   r   rc   r   r   <module>r      s  $ #   0 <	E 	c 	 %)%)	W<<WU\\5 !W W U\\"	W
 \\Wv VZJ<<J%(J<DU\\<RJ
\\J\ VZL<<L%(L<DU\\<RL
\\L^. . .u|| U % Z_ZfZf ,#bii #"DBII D(G299 G2= =&@")) @r   