
    3jk@                     <   S SK r S SKJrJ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  S SKJr  \\R&                  \R&                  4   r SS\R&                  S	\R&                  S
\R&                  S\S\S\R&                  4S jjr SS\R&                  S	\R&                  S
\R&                  S\S\S\R&                  4S jjr SS\R&                  S\R&                  S
\R&                  S\S\R&                  4
S jjr S S\R&                  S\R&                  S\\R&                     S\S\R&                  4
S jjr S!S\R&                  S\R&                  S\R&                  S\S\S\R&                  4S jjrS\R&                  S\R&                  S\R&                  4S jr S"S\R&                  S\R&                  S\\R&                     S\R&                  4S jjr S#S\R&                  S\R&                  S\R&                  S\S\S\R&                  4S jjr g)$    N)OptionalTuple)KORNIA_CHECK_SHAPE)_extract_device_dtype_torch_svd_castsafe_inverse_with_masksafe_solve_with_mask)convert_points_from_homogeneousconvert_points_to_homogeneous)normalize_points)transform_pointspts1pts2Hsquaredepsreturnc                    [        U/ SQ5        U R                  S   S:X  a  [        U 5      nUS   nUS   nOU SSS2S4   nU SSS2S	4   nUR                  S   S:X  a  [        U5      nUS   n	US   n
OUSSS2S4   n	USSS2S	4   n
US
   S   nUS   S   nUS   S   nUS   S   nUS   S   nUS   S   nUS   S   nUS   S   nUS   S   nX-  X-  -   U-   nX-  X-  -   U-   nUU-  UU-  -   U-   nUUU-   -  nUUU-   -  nUU	-
  R                  S5      UU
-
  R                  S5      -   nU(       a  U$ UU-   R	                  5       $ )a9  Return transfer error in image 2 for correspondences given the homography matrix.

Args:
    pts1: correspondences from the left images with shape
      (B, N, 2 or 3). If they are homogeneous, converted automatically.
    pts2: correspondences from the right images with shape
      (B, N, 2 or 3). If they are homogeneous, converted automatically.
    H: Homographies with shape :math:`(B, 3, 3)`.
    squared: if True (default), the squared distance is returned.
    eps: Small constant for safe sqrt.

Returns:
    the computed distance with shape :math:`(B, N)`.

B3r      .r   .   .Nr   r   ).r   r   ).N).r   r   ).r      ).r   r   ).r   r   ).r   r   ).r   r   ).r   r   ).r   r   r   )r   shaper
   powsqrt)r   r   r   r   r   x1y1x1y1u2v2u2v2h00h01h02h10h11h12h20h21h22x_numy_numw_denu1in2v1in2err2s                             T/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/geometry/homography.pyoneway_transfer_errorr7       s   $ q/*zz"~.t4&\&\#q!)_#q!)_zz"~.t4&\&\#q!)_#q!)_ I,y
!C
I,y
!C
I,y
!C
I,y
!C
I,y
!C
I,y
!C
I,y
!C
I,y
!C
I,y
!C Hsx#%EHsx#%E"HsRx#%EUS[!EUS[!E BJA%"*!1!1!!44D3J    c                 :   [        U/ SQ5        U R                  S5      S:X  a  [        U 5      n UR                  S5      S:X  a  [        U5      n[        R                  " U R
                  5      R                  n[        U5      u  pg[        XUSU5      n[        XUSU5      n	UR                  SS5      R                  U5      n
X-   U
R                  UR
                  5      -  XZ) R                  UR
                  5      -  -   nU(       a  U$ X-   R                  5       $ )a8  Return Symmetric transfer error for correspondences given the homography matrix.

Args:
    pts1: correspondences from the left images with shape
      (B, N, 2 or 3). If they are homogeneous, converted automatically.
    pts2: correspondences from the right images with shape
      (B, N, 2 or 3). If they are homogeneous, converted automatically.
    H: Homographies with shape :math:`(B, 3, 3)`.
    squared: if True (default), the squared distance is returned.
    eps: Small constant for safe sqrt.

Returns:
    the computed distance with shape :math:`(B, N)`.

r   r   r   Tr   )r   sizer
   torchfinfodtypemaxr   r7   view	expand_astor    )r   r   r   r   r   max_numH_invgood_Htherebackgood_H_reshapeouts               r6   symmetric_transfer_errorrI   a   s    $ q/*yy}.t4yy}.t4kk$**%))G +1-ME/AtSIE.t5$LD#);;r1#5#?#?#FN<>,,U[[9
9GFZFZ[`[f[fFg<g
gC
Ir8   ls1ls2c                    [        U/ SQ5        [        U / SQ5        [        U/ SQ5        U R                  SS u  pE[        R                  " U SSS9u  pg[        R                  " USSS9u  p[	        U5      n
[	        U	5      n[        R
                  R                  XSS9n[	        [        X&5      5      n[	        [        X'5      5      nXR                  SS	5      -  R                  XE5      R                  5       nXR                  SS	5      -  R                  XE5      R                  5       nS
UU-   -  nU(       a  US-  nU$ )a  Return transfer error in image 2 for line segment correspondences given the homography matrix.

Line segment end points are reprojected into image 2, and point-to-line error is calculated w.r.t. line,
induced by line segment in image 2. See :cite:`homolines2001` for details.

Args:
    ls1: line segment correspondences from the left images with shape
      (B, N, 2, 2).
    ls2: line segment correspondences from the right images with shape
      (B, N, 2, 2).
    H: Homographies with shape :math:`(B, 3, 3)`.
    squared: if True (default is False), the squared distance is returned.

Returns:
    the computed distance with shape :math:`(B, N)`.

r   r   N2rO   Nr   dimchunksr   rQ   r   g      ?)r   r   r;   chunkr   linalgcrossr   	transposer?   abs)rJ   rK   r   r   r   rN   ps1pe1ps2pe2ps2_hpe2_hln2ps1_in2pe1_in2er_st1er_end1errors                     r6   #line_segment_transfer_error_one_wayrf      s   ( q/*s01s0199Ra=DA{{3Aa0HC{{3Aa0HC)#.E)#.E
,,

Uq

1C+,<Q,DEG+,<Q,DEG%%b"--33A9==?F&&r2..44Q:>>@G6G#$EqLr8   points1points2weightssolverc                 P   U R                   UR                   :w  a  [        U R                   5      eU R                   S   S:  a  [        U R                   5      e[        U / SQ5        [        U/ SQ5        [        X/5      u  pESn[	        U 5      u  px[	        U5      u  p[
        R                  " USSS9u  p[
        R                  " U	SSS9u  p[
        R                  " U5      [
        R                  " U5      nn[
        R                  " UUUU* U* U* X-  X-  U/	SS9n[
        R                  " XUUUUU* U-  U* U-  U* /	SS9n[
        R                  " UU4SS9R                  UR                   S	   SUR                   S   5      nUc  UR                  SS5      U-  nO[        UR                   5      S:X  a  UR                   U R                   S
S :X  d  [        UR                   5      eUR                  SSS9R                  S5      nUR                  SS5      U-  U-  nUS:X  a'   [        U5      u    nnUS   R-                  SSS5      nO`US:X  aT  [
        R.                  " UR                   S	   UR                   S   XES9n[1        UU5      u  n  nUR                  SSS5      nO[2        e[5        U
5      S	   UU-  -  nUUSSS
2SS
24   U-   -  nU$ ! [          aD    ["        R$                  " S[&        SS9  [
        R(                  " UR+                  S	5      SS4XES9s $ f = f)a%  Compute the homography matrix using the DLT formulation.

The linear system is solved by using the Weighted Least Squares Solution for the 4 Points algorithm.

Args:
    points1: A set of points in the first image with a tensor shape :math:`(B, N, 2)`.
    points2: A set of points in the second image with a tensor shape :math:`(B, N, 2)`.
    weights: Tensor containing the weights per point correspondence with a shape of :math:`(B, N)`.
    solver: variants: svd, lu.


Returns:
    the computed homography matrix with shape :math:`(B, 3, 3)`.

r      )r   rN   rO   :0yE>r   r   rP   rS   r   NrT   svdSVD did not converge
stacklevelr   devicer=   .r   lu.)r   AssertionErrorr   r   r   r;   rU   	ones_like
zeros_likecatreshaperX   lenrepeat_interleave	unsqueezer   RuntimeErrorwarningswarnRuntimeWarningemptyr:   r?   onesr	   NotImplementedErrorr   )rg   rh   ri   rj   rs   r=   r   points1_norm
transform1points2_norm
transform2r"   r#   x2y2r   zerosaxayAw_full_Vr   r   solH_norms                              r6   find_homography_dltr      s   $ }}%W]]++}}Q!W]]++w0w0)7*<=MFC/8L/8L[[2a8FB[[2a8FB//"%u'7'7';%D 
E5%"rcD5"'27BOUW	XB	BD%sRx"rB3OUW	XB		2r(#++BHHQKRXXb\JAKKB!# GMM"a'GMMW]]2A=N,N //**1!*4>>qA[[R 6)Q.	Y%a(GAq! gJOOB1%	4JJqwwqz1771:fJ(A.	QKKAq!!!z*1-Z@A!CbcM"S()FM  	YMM0.QO;; 1 1! 4a;FXX	Ys   )K AL%$L%soft_inl_thn_iterc                     [        XU5      n[        US-
  5       H<  n[        XUS5      n[        R                  " U* SUS-  -  -  5      n[        XU5      nM>     U$ )a  Compute the homography matrix using the iteratively-reweighted least squares (IRWLS).

The linear system is solved by using the Reweighted Least Squares Solution for the 4 Points algorithm.

Args:
    points1: A set of points in the first image with a tensor shape :math:`(B, N, 2)`.
    points2: A set of points in the second image with a tensor shape :math:`(B, N, 2)`.
    weights: Tensor containing the weights per point correspondence with a shape of :math:`(B, N)`.
      Used for the first iteration of the IRWLS.
    soft_inl_th: Soft inlier threshold used for weight calculation.
    n_iter: number of iterations.

Returns:
    the computed homography matrix with shape :math:`(B, 3, 3)`.

r   F       @r   )r   rangerI   r;   exp)	rg   rh   ri   r   r   r   r   errorsweights_news	            r6   find_homography_dlt_iteratedr      sb    & *'GDA6A:7!US$)IIvgQ9O.P$Q+>  Hr8   c                    U R                   UR                   :w  a  [        U R                   5      e[        R                  " / SQU R                  S9n[        R                  " / SQU R                  S9n[        R                  " / SQU R                  S9nU SS2U4   U SS2U4   U SS2U4   pvnUSS2U4   USS2U4   USS2U4   pnS[        R
                  S[        R
                  S[        R
                  S	[        R
                  4S
 jn[        R                  " U" XVU5      5      n[        R                  " U" XU
5      5      nX:H  R                  SS9nU$ )a  Implement oriented constraint check from :cite:`Marquez-Neila2015`.

Analogous to https://github.com/opencv/opencv/blob/4.x/modules/calib3d/src/usac/degeneracy.cpp#L88

Args:
    points1: A set of points in the first image with a tensor shape :math:`(B, 4, 2)`.
    points2: A set of points in the second image with a tensor shape :math:`(B, 4, 2)`.

Returns:
    Mask with the minimal sample is good for homography estimation :math:`(B)`.

)r   r   r   r   )rs   )r   r   r   r   )r   r   r   r   Nabcr   c                 @    X-
  nX -
  nUS   US   -  US   US   -  -
  $ )Nr   r    )r   r   r   abacs        r6   _orient/sample_is_valid_for_homography.<locals>._orient(  s5    UU&zBvJ&Fbj)@@@r8   r   rS   )r   rv   r;   tensorrs   Tensorsignall)rg   rh   idx_iJKp1_ip1_jp1_kp2_ip2_jp2_kr   	left_sign
right_signsample_is_valids                  r6   sample_is_valid_for_homographyr     s)    }}%W]]++LLgnn=E\'..9A\'..9A q%x('!Q$-ADq%x('!Q$-ADA5<< AELL AU\\ Aell A
 

74t45IGD56J !.333:Or8   c                    [        U R                  5      S:X  a  U S   n [        UR                  5      S:X  a  US   n[        U / SQ5        [        U/ SQ5        U R                  SS u  p4[        X/5      u  pVU R	                  USU-  S5      nUR	                  USU-  S5      n[        U5      u  p[        U5      u  p[        R                  " U	SSS9u  p[        R                  " USSS9u  nn[        R                  " USSS9u  nn[        R                  " USSS9u  nn[        R                  " USSS9u  nn[        R                  " USSS9u  nnUU-
  nUU-
  nUU-  UU-  -
  nSn[        R                  " UU-  UU-  UUU-  UU-  UUU-  UU-  U/	SS	9n[        R                  " UU-  UU-  UUU-  UU-  UUU-  UU-  U/	SS	9n[        R                  " UU4SS	9R	                  UR                  S
   SUR                  S   5      nUc  UR                  SS5      U-  nO[        UR                  5      S:X  a  UR                  U R                  SS :X  d  [        UR                  5      e[        R                  " UR                  SS	9R                  SSS5      R	                  UR                  S
   S5      5      nUR                  SS5      U-  U-  n [        U5      u    n n!U!S   R+                  SSS5      n"[-        U5      S
   U"U
-  -  n"U"U"SSS2SS24   U-   -  n#U#$ ! [         aD    [         R"                  " S[$        SS9  [        R&                  " U	R)                  S
5      SS4XVS9s $ f = f)a`  Compute the homography matrix using the DLT formulation for line correspondences.

See :cite:`homolines2001` for details.

The linear system is solved by using the Weighted Least Squares Solution for the 4 Line correspondences algorithm.

Args:
    ls1: A set of line segments in the first image with a tensor shape :math:`(B, N, 2, 2)`.
    ls2: A set of line segments in the second image with a tensor shape :math:`(B, N, 2, 2)`.
    weights: Tensor containing the weights per point correspondence with a shape of :math:`(B, N)`.

Returns:
    the computed homography matrix with shape :math:`(B, 3, 3)`.

r   NrM   r   r   rP   r   rm   rS   r   rT   ro   rp   rr   rt   .)r{   r   r   r   rz   r   r;   rU   ry   rX   rv   
diag_embedr}   repeatr   r~   r   r   r   r   r:   r?   r   )$rJ   rK   ri   BSrN   rs   r=   rg   rh   r   r   r   r   lst1le1lst2le2xs1ys1xs2ys2xe1ye1xe2ye2r   r   Cr   r   r   w_diagr   r   r   r   s$                                       r6   find_homography_lines_dltr   5  sn   $ 399~$i
399~$is01s01IIbqMEB)3*5MFkk"a!eQ'Gkk"a!eQ'G/8L/8LLa:IDLa:ID#{{4R2HC{{4R2HC{{3Bq1HC{{3Bq1HCc	Ac	Ac	C#IAC 
AGQWaS!c'1a#gq3wPQRXZ	[B	AGQWaS!c'1a#gq3wPQRXZ	[B		2r(#++BHHQKRXXb\JAKKB!# W]]#q(w}}		"1/M //!!'"3"3"3";"B"B1a"K"S"ST[TaTabcTdfh"ijKKB&(1,U!!$1a
 	
'
Aq!Az*1-Z@A!CbcM"S()FM  U,nK{{L--a0!Q7TTUs   L' 'AM54M5c                     [        XU5      n[        US-
  5       H<  n[        XUS5      n[        R                  " U* SUS-  -  -  5      n[        XU5      nM>     U$ )a  Compute the homography matrix using the iteratively-reweighted least squares (IRWLS) from line segments.

The linear system is solved by using the Reweighted Least Squares Solution for the 4 line segments algorithm.

Args:
    ls1: A set of line segments in the first image with a tensor shape :math:`(B, N, 2, 2)`.
    ls2: A set of line segments in the second image with a tensor shape :math:`(B, N, 2, 2)`.
    weights: Tensor containing the weights per point correspondence with a shape of :math:`(B, N)`.
      Used for the first iteration of the IRWLS.
    soft_inl_th: Soft inlier threshold used for weight calculation.
    n_iter: number of iterations.

Returns:
    the computed homography matrix with shape :math:`(B, 3, 3)`.

r   Fr   r   )r   r   rf   r;   r   )	rJ   rK   ri   r   r   r   r   r   r   s	            r6   "find_homography_lines_dlt_iteratedr   ~  sc    & 0'BA6A:B3QPUV$)IIvgQ9O.P$Q%c<  Hr8   )Trm   )F)Nru   )g      @   )N)g      @r   )!r   typingr   r   r;   kornia.core.checkr   kornia.core.utilsr   r   r   r	   kornia.geometry.conversionsr
   r   kornia.geometry.epipolarr   kornia.geometry.linalgr   r   TupleTensorboolfloatr7   rI   rf   strr   intr   r   r   r   r   r8   r6   <module>r      sq  $  "  0 r r f 5 3ELL%,,./ ae>
,,>#ll>/4||>FJ>X]>
\\>D ae$
,,$#ll$/4||$FJ$X]$
\\$P LQ$	$!LL$-2\\$DH$
\\$P imA\\A$)LLA;CELL;QAbeA
\\AJ rs\\$)LL;@<<V[kn
\\6$ELL $5<< $TYT`T` $P MQF	F!LLF3;ELL3IF
\\FT jk	!LL38<<NScf
\\r8   