
    3j"                         S SK Jr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  S SKJr  S SKJr  S SKJr   " S	 S
\R&                  5      rg)    )DictOptionalTupleN)nn)DescriptorMatcherGFTTAffNetHardNetLocalFeatureMatcherLoFTR)LocalFeature)transform_points)RANSAC)warp_perspectivec                   2  ^  \ rS rSrSr    SS\\   S\\R                     S\\R                     S\	SS4
U 4S	 jjjr
\S\R                  4S
 j5       r\S\R                  4S j5       r\R                  " 5       S\R                   SS4S j5       rSS jrS\\R                   \4   4S jrS\R                   S\\R                   \4   4S jrS\R                   S\\R                   \4   4S jrS\R                   S\\R                   \4   4S jrSrU =r$ )HomographyTracker   a  Perform local-feature-based tracking of the target planar object in the sequence of the frames.

Args:
    initial_matcher: image matching module, e.g. :class:`~kornia.feature.LocalFeatureMatcher`
                      or :class:`~kornia.feature.LoFTR`. Default: :class:`~kornia.feature.GFTTAffNetHardNet`.
    fast_matcher: fast image matching module, e.g. :class:`~kornia.feature.LocalFeatureMatcher`
                      or :class:`~kornia.feature.LoFTR`. Default: :class:`~kornia.feature.DescriptorMatcher`.
    ransac: homography estimation module. Default: :class:`~kornia.geometry.RANSAC`.
    minimum_inliers_num: threshold for number inliers for matching to be successful.

Ninitial_matcherfast_matcherransacminimum_inliers_numreturnc                 h  > [         TU ]  5         U=(       d    [        [        S5      [	        SS5      5      U l        U=(       d    [        S5      U l        U=(       d    [        SSSSSS	9U l	        X@l
        U   0 U l        0 U l        S U l        S
U l        S
U l        S
U l        U R#                  5         g )Ni  smnngffffff?outdoor
homographyg      @i   
   )inl_th
batch_sizemax_itermax_lo_itersr   )super__init__r	   r   r   r   r
   r   r   r   r   target_initial_representationtarget_fast_representationprevious_homographyinliers_numkeypoints0_numkeypoints1_numreset_tracking)selfr   r   r   r   	__class__s        X/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/tracking/planar_tracker.pyr!   HomographyTracker.__init__+   s     	.  
 1$ 79J6SW9XY 	 )<E),<o|CD[]ln o#6  	FH*CE';?  !#$#$    c                 .    U R                   R                  $ )zReturn the device used by the current target image tensor.

Returns:
    The ``torch.device`` where ``self.target`` is allocated.
)targetdevicer)   s    r+   r0   HomographyTracker.deviceF   s     {{!!!r-   c                 .    U R                   R                  $ )ztReturn the data type used by the current target image tensor.

Returns:
    The ``torch.dtype`` of ``self.target``.
)r/   dtyper1   s    r+   r4   HomographyTracker.dtypeO   s     {{   r-   r/   c                    Xl         0 U l        0 U l        [        U R                  S5      (       aS  [        U R                  R                  [        R                  5      (       a   U R                  R                  U5      U l        [        U R                  S5      (       aU  [        U R                  R                  [        R                  5      (       a!  U R                  R                  U5      U l        ggg)aB  Register a new target image and refresh cached matcher features.

Args:
    target: Reference target image tensor used for subsequent matching.

Returns:
    None.

The method clears previously cached features and precomputes new
feature representations when the configured matchers expose an
``extract_features`` method.
extract_featuresN)
r/   r"   r#   hasattrr   
isinstancer7   r   Moduler   )r)   r/   s     r+   
set_targetHomographyTracker.set_targetX   s     -/**,'4'');<<  11299B
 B
 261E1E1V1VW]1^D.4$$&899jIZIZIkIkmomvmv>w>w.2.?.?.P.PQW.XD+ ?x9r-   c                     SU l         g)zTReset temporal tracking state from previously processed frames.

Returns:
    None.
N)r$   r1   s    r+   r(    HomographyTracker.reset_trackingp   s     $( r-   c                     SU l         SU l        SU l        [        R                  " SSU R
                  U R                  S9S4$ )zReturn a failed-match response and clear current match statistics.

Returns:
    A tuple ``(H, is_valid)`` where ``H`` is an empty ``3 x 3`` tensor
    on the tracker device and dtype, and ``is_valid`` is ``False``.
r      )r0   r4   F)r%   r&   r'   torchemptyr0   r4   r1   s    r+   no_matchHomographyTracker.no_matchx   s>     {{1a4::FMMr-   xc                 T   U R                   US.nU R                  R                  5        H  u  p4XBU S3'   M     U R                  U5      nUS   US   S:H     nUS   US   S:H     n[	        U5      U l        [	        U5      U l        U R
                  U R                  :  a  U R                  5       $ U R                  Xg5      u  pU	R                  5       R                  5       U l        U R                  U R                  :  a  U R                  5       $ UR                  5       U l        US4$ )a  Estimate a homography from the initial target frame to frame ``x``.

Args:
    x: Current frame tensor to match against the stored target image.

Returns:
    A tuple ``(H, is_valid)`` where ``H`` is the estimated homography
    matrix and ``is_valid`` indicates whether enough inliers were found.

The method updates keypoint counters, inlier statistics, and stores the
estimated homography as ``previous_homography`` on success.
image0image10
keypoints0batch_indexesr   
keypoints1T)r/   r"   itemsr   lenr&   r'   r   rC   r   sumitemr%   cloner$   )
r)   rE   
input_dictkv
match_dictrK   rM   Hinlierss
             r+   match_initialHomographyTracker.match_initial   s    :>PQ.R
66<<>DA"#!Aw ? /3.B.B:.N
-j.IQ.NO
-j.IQ.NO
!*o!*o!9!99==?"[[8
";;=--/d666==?"#$779 $wr-   c                    U R                   b  U R                   R                  5       S   nWSS2SS2SS24   S-  USS2SS2SS24'   USS2SS2S4==   S-  ss'   [        R                  " U5      nU R                  R
                  SS u  pE[        XXE45      nU R                  US.nU R                  R                  5        H  u  pXU S3'   M     U R                  U5      n
U
S   U
S	   S:H     nU
S
   U
S	   S:H     n[        X,5      n[        U5      U l        [        U5      U l        U R                  U R                  :  a   U R                  5         U R!                  5       $ U R#                  X5      u  pUR%                  5       R'                  5       U l        U R(                  U R                  :  a   U R                  5         U R!                  5       $ UR                  5       U l         US4$ )a  Track the target in frame ``x`` using the previous homography prior.

Args:
    x: Current frame tensor to align with the target image.

Returns:
    A tuple ``(H, is_valid)`` where ``H`` is the updated homography and
    ``is_valid`` indicates whether tracking remained reliable.

The frame is first prewarped by the inverse of the previous homography,
then matched with ``fast_matcher`` and verified using RANSAC.
Nr      g?g      $@rG   rJ   rK   rL   rM   T)r$   rR   rA   inverser/   shaper   r#   rN   r   r   rO   r&   r'   r   r(   rC   r   rP   rQ   r%   )r)   rE   HwarpHinvhwframe_warpedrS   rT   rU   rV   rK   rM   rW   rX   s                  r+   track_next_frame"HomographyTracker.track_next_frame   s    ##/,,224T:E"1ac1Q3;/#5a1acka1aiD }}U#{{  $'!89=P\.]
3399;DA"#!Aw < &&z2
-j.IQ.NO
-j.IQ.NO
%e8
!*o!*o!9!99!==?"[[8
";;=--/d666!==?"#$779 $wr-   c                 `    U R                   b  U R                  U5      $ U R                  U5      $ )zRun one tracking step on frame ``x``.

Args:
    x: Current frame tensor.

Returns:
    A tuple ``(H, is_valid)`` from ``track_next_frame`` when previous
    state exists, otherwise from ``match_initial``.
)r$   rd   rY   )r)   rE   s     r+   forwardHomographyTracker.forward   s2     ##/((++!!!$$r-   )r   r   r%   r&   r'   r   r$   r   r/   r#   r"   )NNNr   )r   N)__name__
__module____qualname____firstlineno____doc__r   r   r   r:   intr!   propertyrA   r0   r4   no_gradTensorr;   r(   r   boolrC   rY   rd   rg   __static_attributes____classcell__)r*   s   @r+   r   r      sl   
 37,0&*#%!,/ ryy) #	
 ! 
 6 " " " !u{{ ! ! ]]_Y Y$ Y Y.(
N%d 23 
N#u|| #ellD6H0I #J-%,, -5t9K3L -^% %%d0B*C % %r-   r   )typingr   r   r   rA   r   kornia.featurer   r   r	   r
   kornia.feature.integratedr   kornia.geometry.linalgr   kornia.geometry.ransacr   kornia.geometry.transformr   r:   r    r-   r+   <module>r|      s7   $ ) (   [ [ 2 3 ) 6F%		 F%r-   