+
    åûjŠL  ã                   ó¦   € ^ RI t ^ RIHtHtHtHt ^ RIt^ RIHtH	t	H
t
 ^ RIHtHtHt ^RIHtHtHt ^RIHt R R ltRR	 R
 llt ! R R4      tR# )é    N)ÚOptionalÚSequenceÚTupleÚUnion)ÚKORNIA_CHECKÚKORNIA_CHECK_LAFÚKORNIA_CHECK_SHAPE)Úget_laf_centerÚget_laf_orientationÚget_laf_scale)ÚAdalamConfigÚ	_no_matchÚadalam_core)Údist_matrixc                ó$   € V ^8„  d   QhR\         /# )é   Úreturn)r   )Úformats   "ÚXC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\feature\adalam\adalam.pyÚ__annotate__r   "   s   € ÷ ñ ¤<ñ ó    c                 óV   € \        ^d^^€^^È^R^RR\        P                  ! R4      R7      # )zReturn default Adalam Config.g      ø?TÚcpu)Ú
area_ratioÚsearch_expansionÚransac_itersÚmin_inliersÚmin_confidenceÚ orientation_difference_thresholdÚscale_rate_thresholdÚdetected_scale_rate_thresholdÚrefitÚforce_seed_mnnÚdevice)r   Útorchr$   © r   r   Úget_adalam_default_configr'   "   s9   € äØØØØØØ)+Ø Ø&'ØØÜ�|Š|˜EÓ"ôð r   c                ó¶  € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  R\        \        ,          R\        \        \
        ,          ,          R\        \        \
        ,          ,          R\        \         P                  ,          R	\        \         P                  \         P                  3,          /	# )
r   Údesc1Údesc2Úlafs1Úlafs2ÚconfigÚhw1Úhw2Údmr   )r%   ÚTensorr   r   r   Úintr   )r   s   "r   r   r   3   s¯   € ÷ mñ mÜ�<‰<ðmä�<‰<ðmô �<‰<ðmô �<‰<ð	mô
 ”\Õ"ðmô 
”(œ3•-Õ	 ðmô 
”(œ3•-Õ	 ðmô 	”—‘Õðmô Œ5�<‰<œŸ™Ð%Õ&ñmr   c                óÔ  € \        V RR.4       \        VRR.4       \        V4       \        V4       \        4       pV P                  VR&   Ve\   VP	                  4        FG  w  ršW˜9  d9   \
        P                  ! RV	 R\        VP                  4       4       R2^R7       KC  W¨V	&   KI  	  \        V4      pVP                  \        V4      P                  R
^4      \        V4      P                  R
^4      V VVV\        V4      P                  R
4      \        V4      P                  R
4      \        V4      P                  R
4      \        V4      P                  R
4      RVR	7      w  rÍWÜ3# )a„  Match descriptors by nearest neighbour, filtered by local affine model support (AdaLAM).

``dists, idxs = match_adalam(desc1, desc2, lafs1, lafs2, hw1=(H1, W1), hw2=(H2, W2))`` takes two descriptor sets
``(B1, D)`` and ``(B2, D)``, their LAFs ``(1, B1, 2, 3)`` and ``(1, B2, 2, 3)`` and, optionally, the image sizes
in the units of the LAF centres, pixels for LAFs in pixels. It matches every row of ``desc1`` to its nearest
neighbour in ``desc2`` and returns the matches that the local RANSACs around confident seed matches accept, each
match once: the inverse RANSAC confidences ``(B3, 1)``, float32, and the index pairs ``(B3, 2)``. The local
models read the LAF orientations and scales as well as the centres, and need no global model of the scene, see
:cite:`AdaLAM2020` for more details. To keep the matches that one global model explains instead, pass the
matches of :func:`~kornia.feature.match_snn` or :func:`~kornia.feature.match_smnn` to
:class:`~kornia.geometry.ransac.RANSAC`.

Convention:
    - See the Convention block on :func:`~kornia.feature.match_nn`. ``dists`` is the inverse of the confidence of
      the most confident local RANSAC that accepted the match (overlapping neighbourhoods often accept a match
      several times), in float32 whatever the descriptor dtype, so at most ``1 / min_confidence`` (``1 / 200`` by
      default). If no seed survives, for example with very few keypoints, the matches of a ratio test on squared
      distances, ``ratio <= 0.95``, are returned with that ratio instead.
    - The putative matches are the nearest neighbours in the distance matrix ``dm``, scored by the ratio of the
      two smallest distances; with ``force_seed_mnn`` only mutual nearest neighbours can be seeds. ``dm`` holds
      plain distances, like the ``dm`` of :func:`~kornia.feature.match_nn` and the other matchers: AdaLAM
      squares it internally (its thresholds are tuned for squared distances), so ``dm=torch.cdist(desc1, desc2)``
      gives the matches of ``dm=None``, where the squared L2 distances between the descriptors are used.
    - The LAFs give each keypoint's centre, orientation in degrees and scale. Orientations and scales are read
      only through how their change between the two images varies from match to match: adding an angle to the
      orientations of one image or both, negating both, or scaling the LAFs of one image or both alike changes
      nothing.
    - ``hw1`` and ``hw2`` enter only through the area of their image, which sets the neighbourhood radius in that
      image, so ``(H, W)`` and ``(W, H)`` give the same matches. Without them, the extent of each image's
      keypoints is used.
    - ``config`` is merged into the defaults of ``kornia.feature.adalam.get_adalam_default_config()``; an
      unknown key warns and is ignored. The computation runs on the device of ``desc1`` unless ``config`` sets
      ``"device"``.
    - Local affine consensus counts distinct source and target locations, reserving three samples for the
      seed and minimal fitting pair. Repeated locations use the first observation in descriptor-score order
      as evidence; a later conflicting match cannot replace it. Equal residuals do not imply duplicate keypoints.
      The full accepted radius is returned, including repeated observations tied at its boundary and mixtures
      of exact and noisy matches. Confidence uses a
      conservative residual-area floor of ``2**-24`` in normalized coordinates, remaining finite for exact fits.
    - Unlike the reference implementation, which merges nearly equal residuals into float16 bins, the
      consensus radius is the largest at which both the cumulative inlier density and the density over the
      last ``min_inliers`` samples reach ``min_confidence``.

Args:
    desc1: Batch of descriptors of a shape ``(B1, D)``.
    desc2: Batch of descriptors of a shape ``(B2, D)``.
    lafs1: LAFs of a shape ``(1, B1, 2, 3)``.
    lafs2: LAFs of a shape ``(1, B2, 2, 3)``.
    config: dict with AdaLAM config, merged into the defaults; see the Convention block.
    hw1: size of the first image, ``(H, W)`` or ``(W, H)``.
    hw2: size of the second image, ``(H, W)`` or ``(W, H)``.
    dm: torch.Tensor containing the non-negative distances (not squared) from each descriptor in desc1
      to each descriptor in desc2, shape of ``(B1, B2)``. Smaller is better.

Return:
    - Inverse local RANSAC confidence of each match, float32, shape of ``(B3, 1)``; see the Convention
      block.
    - Long torch.Tensor indexes of matching descriptors in desc1 and desc2. Shape: ``(B3, 2)``,
      where 0 <= B3 <= B1.

See Also:
    - :func:`match_fginn`: a ratio test that reads the LAF centres of desc2 only, with no local models.
    - :class:`GeometryAwareDescriptorMatcher`: the module form, with ``match_mode="adalam"`` and no image sizes.
    - :class:`~kornia.geometry.ransac.RANSAC`: one global model, such as a homography or a fundamental matrix, for
      all the matches.

ÚBÚDIMr$   z=Custom configuration contains a key which is not recognized (z). Known configurations are Ú.)Ú
stacklevelT)Úreturn_distr0   éÿÿÿÿ)r	   r   r'   r$   ÚitemsÚwarningsÚwarnÚlistÚkeysÚAdalamFilterÚmatch_and_filterr
   Úreshaper   r   )r)   r*   r+   r,   r-   r.   r/   r0   Úconfig_ÚkeyÚvalÚadalam_objectÚidxsÚqualitys   &&&&&&&&      r   Úmatch_adalamrH   3   s]  € ôZ �u˜s E˜lÔ+Ü�u˜s E˜lÔ+Ü�UÔÜ�UÔÜ'Ó)€GØŸ™€GˆHÑØÒØŸ™ž‰HˆCØÔ!Ü—’ØSÐTWÐSXð Y0Ü04°W·\±\³^Ó0DÐ/EÀQðHà õñ
 à�C‹Lñ 'ô ! Ó)€MØ!×2Ñ2Ü�uÓ×%Ñ% b¨!Ó,Ü�uÓ×%Ñ% b¨!Ó,ØØØØÜ˜EÓ"×*Ñ*¨2Ó.Ü˜EÓ"×*Ñ*¨2Ó.Ü�eÓ×$Ñ$ RÓ(Ü�eÓ×$Ñ$ RÓ(ØØð 3ó �M€Dð ˆ=Ðr   c                   óf   a € ] tR t^£t o RtR
V 3R lR lltRV 3R lR lltRV 3R lR lltR	tV t	R# )r?   a	  Implement the AdaLAM (Adaptive Locally-Affine Matching) filter for outlier rejection.

This class wraps the AdaLAM algorithm to filter feature matches based on
local affine consistency.

Args:
    custom_config: Optional configuration object for AdaLAM parameters.
Nc                ó4   <€ V ^8„  d   QhRS[ S[,          RR/# )r   Úcustom_configr   N)r   r   )r   Ú__classdict__s   "€r   r   ÚAdalamFilter.__annotate__­   s    ø€ ÷ 
6ñ 
6¡h©|Õ&<ð 
6Èñ 
6r   c                ó<   € Ve	   Wn         R# \        4       V n         R# )a  Wrap the method AdaLAM for outlier filtering.

init args:
    custom_config: dictionary overriding the default configuration. Missing parameters are kept as default.
                   See documentation of DEFAULT_CONFIG for specific explanations on the accepted parameters.
N)r-   r'   )ÚselfrK   s   &&r   Ú__init__ÚAdalamFilter.__init__­   s   € ð Ò$Ø'ŽKä3Ó5ˆDŽKr   c                ó  <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  RS[ P                  RS[S[ P                  ,          RS[S[S[,          ,          RS[S[S[,          ,          RS[S[ P                  ,          R	S[S[ P                  ,          R
S[S[ P                  ,          RS[S[ P                  ,          RS[RS[S[S[ P                  S[ P                  3,          S[ P                  3,          /# )r   Úk1Úk2Úputative_matchesÚscoresÚmnnÚim1shapeÚim2shapeÚo1Úo2Ús1Ús2r8   r   ©r%   r1   r   r   r2   Úboolr   r   )r   rL   s   "€r   r   rM   ¹   s  ø€ ÷ Gñ Gá�L‰LðGñ �L‰LðGñ  Ÿ,™,ð	Gñ
 —‘ðGñ ‘e—l‘lÕ#ðGñ ™8¡C�=Õ)ðGñ ™8¡C�=Õ)ðGñ ‘U—\‘\Õ"ðGñ ‘U—\‘\Õ"ðGñ ‘U—\‘\Õ"ðGñ ‘U—\‘\Õ"ðGñ ðGñ 
‰u‘U—\‘\¡5§<¡<Ð/Õ0±%·,±,Ð>Õ	?ñGr   c                ó¾   € \         P                  ! 4       ;_uu_ 4        \        VVVVVVVVV	V
VV P                  VR7      uuRRR4       #   + '       g   i     R# ; i)a	  Call the core functionality of AdaLAM, i.e. just outlier filtering.

No sanity check is performed on the inputs.

Args:
    k1: keypoint locations in the source image, in pixel coordinates.
        Expected a float32 torch.Tensor with shape (num_keypoints_in_source_image, 2).
    k2: keypoint locations in the destination image, in pixel coordinates.
        Expected a float32 torch.Tensor with shape (num_keypoints_in_destination_image, 2).
    putative_matches: Initial set of putative matches to be filtered.
                      The current implementation assumes that these are unfiltered nearest neighbor matches,
                      so it requires this to be a list of indices a_i such that the source keypoint i is
                      associated to the destination keypoint a_i. For now to use AdaLAM on different inputs a
                      workaround on the input format is required.
                      Expected a long torch.Tensor with shape (num_keypoints_in_source_image,).
    scores: Confidence scores on the putative_matches. Usually holds Lowe's ratio scores.
    mnn: A mask indicating which putative matches are also mutual nearest neighbors. See documentation on
         'force_seed_mnn' in the DEFAULT_CONFIG. If None, it disables the mutual nearest neighbor filtering on
         seed point selection. Expected a bool torch.Tensor with shape (num_keypoints_in_source_image,)
    im1shape: Shape of the source image. If None, it is inferred from keypoints max and min, at the cost of
              wasted runtime. So please provide it. Expected a tuple or list with (width, height) or
              (height, width) of source image
    im2shape: Shape of the destination image. If None, it is inferred from keypoints max and min, at the cost
              of wasted runtime. So please provide it. Expected a tuple or list with (width, height) or
              (height, width) of destination image
    o1: keypoint orientations in degrees. They can be None if 'orientation_difference_threshold' in config
           is set to None. See documentation on 'orientation_difference_threshold' in the DEFAULT_CONFIG.
           Expected a float32 torch.Tensor with shape (num_keypoints_in_source/destination_image,)
    o2: same as o1 but for destination.
    s1: keypoint scales. They can be None if 'scale_rate_threshold' in config is set to None.
           See documentation on 'scale_rate_threshold' in the DEFAULT_CONFIG.
           Expected a float32 torch.Tensor with shape (num_keypoints_in_source/destination_image,)
    s2: same as s1 but for destination.
    return_dist: if True, inverse confidence value is also outputted.

Returns:
    Filtered putative matches.
    A long torch.Tensor with shape (num_filtered_matches, 2) with indices of corresponding
    keypoints in k1 and k2.

)Úfnn12Úscores1rW   rX   rY   rZ   r[   r\   r]   r-   r8   N)r%   Úno_gradr   r-   )rO   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r8   s   &&&&&&&&&&&&&r   Úfilter_matchesÚAdalamFilter.filter_matches¹   sO   € ôp �]Š]�_�_ÜØØØ&ØØØ!Ø!ØØØØØ—{‘{Ø'ô÷ �_�_‹_ús   ž"AÁA	c                ó  <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  RS[ P                  RS[S[S[,          ,          RS[S[S[,          ,          RS[S[ P                  ,          RS[S[ P                  ,          R	S[S[ P                  ,          R
S[S[ P                  ,          RS[RS[S[ P                  ,          RS[S[S[ P                  S[ P                  3,          S[ P                  3,          /# )r   rS   rT   Úd1Úd2rX   rY   rZ   r[   r\   r]   r8   r0   r   r^   )r   rL   s   "€r   r   rM     s  ø€ ÷ q
ñ q
á�L‰Lðq
ñ �L‰Lðq
ñ �L‰Lð	q
ñ
 �L‰Lðq
ñ ™8¡C�=Õ)ðq
ñ ™8¡C�=Õ)ðq
ñ ‘U—\‘\Õ"ðq
ñ ‘U—\‘\Õ"ðq
ñ ‘U—\‘\Õ"ðq
ñ ‘U—\‘\Õ"ðq
ñ ðq
ñ ‘U—\‘\Õ"ðq
ñ 
‰u‘U—\‘\¡5§<¡<Ð/Õ0±%·,±,Ð>Õ	?ñq
r   c                ó  € V	e   V
f!   V P                   R,          e   \        R4      hVe   Vf!   V P                   R,          e   \        R4      h\        P                  ! WP                   R,          \        P                  R7      p\        P                  ! W P                   R,          \        P                  R7      p\        P                  ! W0P                   R,          \        P                  R7      p\        P                  ! W@P                   R,          \        P                  R7      pVep   \        P                  ! WpP                   R,          \        P                  R7      p\        P                  ! W€P                   R,          \        P                  R7      pMYxppV	ep   \        P                  ! W�P                   R,          \        P                  R7      p\        P                  ! W P                   R,          \        P                  R7      pMYšppVed   \        \        VP                  4      \        V4      \        V4      38H  R\        V4      \        V4      3 R	\        VP                  4       24       \        V4      ^8:  g   \        V4      ^8:  d   \        V4      w  ppV'       d   VV3# V# Vf   \        VVR
R7      pMF\        P                  ! WÀP                   R,          \        P                  R7      P                  ^4      p\        P                  ! V^^R
R7      w  ppVR,          pVR,          VR,          P                  R4      ,          pV P                   R,          '       dt   VP                  4       P                  4       P!                  ^R7      pVV,          \        P"                  ! VP                  ^ ,          V P                   R,          R7      8H  pMRpV P%                  WÞVVVWVVVVVV4      # )aã	  Match and filter with AdaLAM.

This function:

    - performs some elementary sanity check on the inputs;
    - wraps input arrays into torch tensors and loads to GPU if necessary;
    - extracts nearest neighbors;
    - finds mutual nearest neighbors if required;
    - finally calls AdaLAM filtering.

Args:
    k1: keypoint locations in the source image, in pixel coordinates.
        Expected an array with shape (num_keypoints_in_source_image, 2).
    k2: keypoint locations in the destination image, in pixel coordinates.
        Expected an array with shape (num_keypoints_in_destination_image, 2).
    d1: descriptors in the source image.
        Expected an array with shape (num_keypoints_in_source_image, descriptor_size).
    d2: descriptors in the destination image.
        Expected an array with shape (num_keypoints_in_destination_image, descriptor_size).
    im1shape: Shape of the source image. If None, it is inferred from keypoints max and min, at the cost of
              wasted runtime. So please provide it. Expected a tuple or list with (width, height) or
              (height, width) of source image
    im2shape: Shape of the destination image. If None, it is inferred from keypoints max and min, at the cost
              of wasted runtime. So please provide it. Expected a tuple or list with (width, height) or
              (height, width) of destination image
    o1: keypoint orientations in degrees. They can be None if 'orientation_difference_threshold' in config
           is set to None. See documentation on 'orientation_difference_threshold' in the DEFAULT_CONFIG.
           Expected an array with shape (num_keypoints_in_source/destination_image,)
    o2: Same as o1 for destination.
    s1: keypoint scales. They can be None if 'scale_rate_threshold' in config is set to None.
           See documentation on 'scale_rate_threshold' in the DEFAULT_CONFIG.
           Expected an array with shape (num_keypoints_in_source/destination_image,)
    s2: Same as s1 for destination.
    return_dist: if True, inverse confidence value is also outputted.
    dm: non-negative distances (not squared) from each descriptor in d1 to each descriptor in d2, shape
        (num_keypoints_in_source_image, num_keypoints_in_destination_image); smaller is better. They are
        squared internally, like the L2 distances used when dm is None.

Returns:
    Filtered putative matches.
    A long torch.Tensor with shape (num_filtered_matches, 2) with indices of corresponding
    keypoints in k1 and k2.

Nr    z¿Current configuration considers keypoint scales for filtering, but scales have not been provided.
Please either provide scales or set 'scale_rate_threshold' to None to disable scale filteringr   zäCurrent configuration considers keypoint orientations for filtering, but orientations have not been provided.
Please either provide orientations or set 'orientation_difference_threshold' to None to disable orientations filteringr$   )r$   Údtypezdm must have shape z, got F)Úis_normalized)ÚkÚdimÚlargestgü©ñÒMbP?r#   )rm   )r$   )ºNNNr   )ro   é   )r-   ÚAttributeErrorr%   Ú	as_tensorÚfloat32r   ÚtupleÚshapeÚlenr   r   ÚpowÚtopkÚ
clamp_min_ÚtÚ
contiguousÚargminÚarangerd   )rO   rS   rT   rg   rh   rX   rY   rZ   r[   r\   r]   r8   r0   Ú_k1Ú_k2Ú_d1Ú_d2Ú_o1Ú_o2Ú_s1Ú_s2ÚdistsrF   ÚdistmatÚdd12Únn12rU   rV   Únn21rW   s   &&&&&&&&&&&&&                 r   r@   ÚAdalamFilter.match_and_filter  sñ  € ðv ŠJ˜"š*¨$¯+©+Ð6LÕ*MÒ*YÜ ðpóð ð ŠJ˜"š*¨$¯+©+Ð6XÕ*YÒ*eÜ ðIóð ô �oŠo˜b¯©°XÕ)>ÄeÇmÁmÔTˆÜ�oŠo˜b¯©°XÕ)>ÄeÇmÁmÔTˆÜ�oŠo˜b¯©°XÕ)>ÄeÇmÁmÔTˆÜ�oŠo˜b¯©°XÕ)>ÄeÇmÁmÔTˆØŠ>Ü—/’/ "¯[©[¸Õ-BÌ%Ï-É-ÔXˆCÜ—/’/ "¯[©[¸Õ-BÌ%Ï-É-ÔX‰Cà�ˆCØŠ>Ü—/’/ "¯[©[¸Õ-BÌ%Ï-É-ÔXˆCÜ—/’/ "¯[©[¸Õ-BÌ%Ï-É-ÔX‰Cà�ˆCàŠ>ÜÜ�b—h‘h“¤C¨£H¬c°#«hÐ#7Ñ7Ø%¤s¨3£x´°S³Ð&:Ð%;¸6Ä%ÈÏÉÃ/ÐARÐSôô
 �‹H˜ŒMœs 3›x¨1œ}Ü# C›.‰KˆE�4ßØ˜U�{Ð"ØˆKàŠ:Ü! # s¸%Ô@‰Gä—o’o b·±¸XÕ1FÌeÏmÉmÔ\×`Ñ`ÐabÓcˆGÜ—Z’Z ¨1°!¸UÔC‰
ˆˆdà �:ÐØ�d•˜d 4�j×3Ñ3°DÓ9Õ9ˆà�;‰;Ð'×(Ô(Ø—9‘9“;×)Ñ)Ó+×2Ñ2°qÐ2Ó9ˆDØÐ'Õ(¬E¯LªL¸¿¹À1½ÈdÏkÉkÐZbÕNcÔ,dÑd‰CàˆCà×"Ñ"ØÐ&¨°°XÈÈcÐSVÐX[Ð]hó
ð 	
r   )r-   )N)NNNNNNNF)NNNNNNFN)
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rP   rd   r@   Ú__static_attributes__Ú__classdictcell__)rL   s   @r   r?   r?   £   s3   ø‡ € ñ÷
6ò 
6÷Gò G÷Rq
÷ q
ð q
r   r?   )NNNN)r;   Útypingr   r   r   r   r%   Úkornia.core.checkr   r   r	   Úkornia.feature.lafr
   r   r   Úcorer   r   r   Úutilsr   r'   rH   r?   r&   r   r   Ú<module>r˜      s@   ðó, ß 3Ó 3ã ç PÑ Pß QÑ Qç 6Ñ 6Ý õ÷"m÷`P
ó P
r   