+
    ÿäûj:  ã                   ó˜   € ^ RI HtHtHt ^ RIt^ RIHt ^ RIHt ^ RIH	t	H
t
 ^ RIHt ^ RIHt ^ RIHt  ! R	 R
]P"                  4      tR# )é    )ÚDictÚOptionalÚTupleN)Únn)Úrgb_to_grayscale)ÚLocalFeatureMatcherÚLoFTR)Úfind_homography_dlt_iterated)ÚRANSAC)Úwarp_perspectivec                   óâ   a a€ ] tR t^t oRtRV3R lV 3R llltV3R lR ltRR/V3R lR	 lltV3R
 lR ltV3R lR lt	V3R lR lt
V3R lR ltRV3R lR lltV3R lR ltRtVtV ;t# )ÚImageStitcheraÒ  Stitch overlapping RGB images, ordered left to right, into one panorama by matched homographies.

``stitcher = ImageStitcher(KF.LoFTR(pretrained="outdoor"))`` builds the stitcher.
``panorama = stitcher(img_left, img_right, ...)`` takes RGB images ``(B, 3, H, W)`` of one height, ordered left to
right, and returns one ``(B, 3, H, W_pano)`` panorama in the first image's pixel frame. Each next image is matched
to the panorama so far, and warped onto it with the homography of those matches.

Convention:
    - ``matcher`` is a :class:`~kornia.feature.LoFTR` or a :class:`~kornia.feature.LocalFeatureMatcher`, which gets
      grayscale copies of the images; any other matcher raises ``NotImplementedError``.
    - The panorama has the first image's height: content that maps left of its first column, or above or below
      its rows, is lost. Every sample needs 4 matches, or a ``RuntimeError`` names the sample.
    - ``"naive"`` blending keeps the warped image wherever it covers a pixel completely, so in an overlap the right
      image replaces the left one; a pixel it covers only in part keeps the panorama's value.
    - ``W_pano`` is at most the sum of the widths: the result drops the empty trailing columns, those after the
      last column that any sample covers; an uncovered column before it, the first one included, is kept. A batch
      shares one crop, so it is cropped to its widest sample and a narrower sample keeps zero columns on its
      right.
    - ``estimator="ransac"`` fits the inliers that :class:`~kornia.geometry.ransac.RANSAC` finds for
      ``"homography"`` (``inl_th=2.0``). ``"vanilla"`` fits every match with
      :func:`~kornia.geometry.homography.find_homography_dlt_iterated`, which down-weights matches by their
      transfer error after the first fit.

Args:
    matcher: image feature matching module, :class:`~kornia.feature.LoFTR` or
        :class:`~kornia.feature.LocalFeatureMatcher`.
    estimator: method to compute homography, ``"ransac"`` or ``"vanilla"``; another value raises
        ``NotImplementedError``.
    blending_method: method to blend two images together.
        Only "naive" is currently supported.

.. code-block:: python

    import torch
    import kornia.feature as KF
    from kornia.contrib import ImageStitcher

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    stitcher = ImageStitcher(KF.LoFTR(pretrained="outdoor"), estimator="ransac").to(device)
    # img_left and img_right: (1, 3, H, W) RGB images of one height on `device`
    with torch.inference_mode():
        panorama = stitcher(img_left, img_right)

See :doc:`/applications/image_stitching` for a complete example.

See Also:
    - :class:`~kornia.geometry.transform.image_registrator.ImageRegistrator`: aligns one image to another by
      optimising a homography on the pixels, without matches and without a panorama.
    - :class:`~kornia.tracking.HomographyTracker`: returns a planar target's homography in each video frame,
      starting from the previous frame's, and builds no panorama.
    - :func:`~kornia.geometry.transform.warp_perspective`: warps one image by a homography you already have.
    - :class:`~kornia.geometry.ransac.RANSAC`: the robust homography estimator of ``estimator="ransac"``.

c                óD   <€ V ^8„  d   QhRS[ P                  RS[RS[RR/# )é   ÚmatcherÚ	estimatorÚblending_methodÚreturnN)r   ÚModuleÚstr)ÚformatÚ__classdict__s   "€ÚZC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\contrib\image_stitching.pyÚ__annotate__ÚImageStitcher.__annotate__V   s/   ø€ ÷ /ñ /¡§	¡	ð /±cð /ÑWZð /Ðimñ /ó    c                ó¦   <€ \         SV `  4        Wn        W n        W0n        VR9  d   \        RV R24      hVR 8X  d   \        R4      V n        R# R# )ÚransacúUnsupported estimator ú$. Use `ransac` or `vanilla` instead.Ú
homographyN)r   Úvanilla)ÚsuperÚ__init__r   r   r   ÚNotImplementedErrorr   r   )Úselfr   r   r   Ú	__class__s   &&&&€r   r$   ÚImageStitcher.__init__V   sV   ø€ Ü‰ÑÔØŒØ"ŒØ.ÔØÐ1Ô1Ü%Ð(>¸y¸kÐImÐ&nÓoÐoØ˜Ô Ü  Ó.ˆDŽKñ !r   c                óh   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  /# )r   Ú
keypoints1Ú
keypoints2r   ©ÚtorchÚTensor)r   r   s   "€r   r   r   `   s1   ø€ ÷ ñ ©u¯|©|ð ÉÏÉð ÑZ_×ZfÑZfñ r   c                ó$  € V P                   R8X  d9   \        VR,          VR,          \        P                  ! VR,          4      4      pV# V P                   R8X  d   V P	                  W!4      w  r4VR,          pV# \        RV P                    R24      h)zÑEstimate homography by the matched keypoints.

Args:
    keypoints1: matched keypoint set from an image, shaped as ``(N, 2)``.
    keypoints2: matched keypoint set from the other image, shaped as ``(N, 2)``.

r"   Nr   r   r    )NºNNNr   )r   r
   r-   Ú	ones_liker   r%   )r&   r*   r+   ÚhomoÚ_s   &&&  r   Ú_estimate_homographyÚ"ImageStitcher._estimate_homography`   s‹   € ð �>‰>˜YÔ&Ü/Ø˜4Õ  *¨TÕ"2´E·O²OÀJÈzÕDZÓ4[óˆDð ˆð �^‰^˜xÔ'Ø—k‘k *Ó9‰GˆDØ˜•:ˆDð ˆô &Ð(>¸t¿~¹~Ð>NÐNrÐ&sÓtÐtr   Ú
batch_sizeNc                ó~   <€ V ^8„  d   QhRS[ P                  RS[S[,          RS[ P                  RS[ P                  /# )r   Úargsr6   Úkwargsr   )r-   r.   r   Úint)r   r   s   "€r   r   r   s   s=   ø€ ÷ " ñ " Ù—\‘\ð" Ù/7¹­}ð" ÙOTÏ|É|ð" á	�‰ñ" r   c               óV  € VR,          VR,          VR,          repVfP   VP                  4       ^ 8X  d   \        R4      h\        VP                  4       P	                  4       4      ^,           pMTpWf^ 8  Wg8¬  ,          ,          pVP                  4       ^ 8”  dW   \        V^ ,          P	                  4       4      p	V	^ 8  d   \        RV	 R24      hV^8X  d   RMV R2p
\        RV	 R	V
 R
24      h\        V4       Uu. uF  q¶V8H  NK	  	  pp\        V4       FE  w  r½\        VP                  4       P	                  4       4      pV^8  g   K5  \        RV RV R24      h	  V Uu. uF   qÐP                  WM,          W],          4      NK"  	  pp\        P                  ! V4      # u upi u upi )aø  Compute the corresponding homography of every sample.

Args:
    *args: ignored.
    batch_size: number of samples. Defaults to one more than the largest batch index of the matches.
    **kwargs: the matcher output, with ``keypoints0``, ``keypoints1`` and ``batch_indexes``.

Raises:
    RuntimeError: if a sample has fewer than 4 matches (the error names the sample), if a batch index is outside
        the batch (the error names the index), or if there are no matches and ``batch_size`` is not given.
Ú
keypoints0r*   Úbatch_indexesz6Compute homography failed. No matched keypoints found.z'Compute homography failed: batch index z is negative.z1 samplez samplesz is outside the batch of Ú.z%Compute homography failed for sample z: z matches, a homography needs 4.)ÚnumelÚRuntimeErrorr:   ÚmaxÚitemÚrangeÚ	enumerateÚsumr4   r-   Úcat)r&   r6   r8   r9   Úkp1Úkp2ÚidxÚnÚoutsideÚbadÚsamplesÚiÚ
selectionsÚselÚn_iÚhomoss   &$*,            r   Úestimate_transformÚ ImageStitcher.estimate_transforms   s‹  € ð ˜|Õ,¨f°\Õ.BÀFÈ?ÕD[�#ˆØÒØ�y‰y‹{˜aÔÜ"Ð#[Ó\Ð\Ü�C—G‘G“I—N‘NÓ$Ó%¨Õ)‰AàˆAØ˜Q‘w 3¡8Õ,Õ-ˆØ�=‰=‹?˜QÔÜ�g˜a•j—o‘oÓ'Ó(ˆCØ�QŒwÜ"Ð%LÈSÈEÐQ^Ð#_Ó`Ð`Ø$%¨¤F‘j°1°#°X°ˆGÜÐ!HÈÈÐMfÐgnÐfoÐopÐqÓrÐrÜ(-¨a¬Ó1© 1˜Q”h©ˆ
Ð1Ü 
Ö+‰FˆAÜ�c—g‘g“i—n‘nÓ&Ó'ˆCØ�QŽwÜ"Ð%JÈ1È#ÈRÐPSÈuÐTsÐ#tÓuÐuñ ,ñ KUÓUÉ*À3×*Ñ*¨3­8°SµXÖ>É*ˆÐUÜ�yŠy˜ÓÐùò 2ùò
 Vs   Ã<F!Å#&F&c                ó‚   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  RS[ P                  /# )r   Úsrc_imgÚdst_imgÚmaskr   r,   )r   r   s   "€r   r   r   —   s<   ø€ ÷ ñ ¡5§<¡<ð ¹%¿,¹,ð ÉeÏlÉlð Ñ_d×_kÑ_kñ r   c                óŽ   € V P                   R8X  d   \        P                  ! V^8H  W4      pV# \        RV P                    R24      h)zBlend two images together.ÚnaivezUnsupported blending method z. Use `naive`.)r   r-   Úwherer%   )r&   rV   rW   rX   Úouts   &&&& r   Úblend_imageÚImageStitcher.blend_image—   sL   € ð ×Ñ 7Ô*Ü—+’+˜d a™i¨Ó:ˆCð ˆ
ô &Ð(DÀT×EYÑEYÐDZÐZhÐ&iÓjÐjr   c                ó~   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[S[S[ P                  3,          /# )r   Úimage_1Úimage_2r   )r-   r.   r   r   )r   r   s   "€r   r   r       s;   ø€ ÷ 
ñ 
¡%§,¡,ð 
¹¿¹ð 
É$ÉsÑTY×T`ÑT`ÐO`ÕJañ 
r   c                ó´   € \        V P                  \        \        34      '       d   R\	        V4      R\	        V4      /pV# \        RV P                   R24      h)z(Preprocess input to the required format.Úimage0Úimage1zThe preprocessor for z has not been implemented.)Ú
isinstancer   r	   r   r   r%   )r&   r`   ra   Ú
input_dicts   &&& r   Ú
preprocessÚImageStitcher.preprocess    s\   € ô �d—l‘l¤UÔ,?Ð$@×AÒAàÔ*¨7Ó3ØÔ*¨7Ó3ðˆJð Ðô &Ð(=¸d¿l¹l¸^ÐKeÐ&fÓgÐgr   c                óh   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  /# )r   ÚimagerX   r   r,   )r   r   s   "€r   r   r   ¬   s.   ø€ ÷ #ñ #¡§¡ð #±U·\±\ð #ÁeÇlÁlñ #r   c                ó  € VP                  R4      p\        VP                  4       P                  ^ 4      P	                  ^ 4      P                  4       P                  4       P                  4       4      pV^ 8X  d   V# VRRV) 13,          # )aK  Crop redundant empty columns from a stitched panorama.

Args:
    image: Stitched image tensor, shaped ``(B, C, H, W_pano)``, where ``W_pano`` is the
        panorama width after concatenation/warping.
    mask: Validity mask aligned with ``image`` where non-zero values mark
        pixels covered by at least one input view.

Returns:
    The stitched image without its empty trailing columns, those after the last column that has a valid pixel
    in any sample of the mask. Uncovered columns before it are kept, and a mask without any valid pixel returns
    the original ``image``.
.N)r   é   )rE   r:   ÚboolÚanyÚflipÚlongÚargmaxrB   )r&   rj   rX   Úmask_Úindexs   &&&  r   ÚpostprocessÚImageStitcher.postprocess¬   st   € ð —‘˜Ó ˆÜ�E—J‘J“L×$Ñ$ QÓ'×,Ñ,¨QÓ/×4Ñ4Ó6×=Ñ=Ó?×DÑDÓFÓGˆØ�AŒ:ØˆLØ�S˜'˜E˜6˜'�\Õ"Ð"r   c                óz   <€ V ^8„  d   QhRS[ S[S[P                  3,          RS[ S[S[P                  3,          /# )r   Údatar   )r   r   r-   r.   )r   r   s   "€r   r   r   Á   s7   ø€ ÷ "ñ "™t¡C©¯©Ð$5Õ6ð "¹4ÁÁUÇ\Á\Ð@QÕ;Rñ "r   c                ó$   € V P                  V4      # )aR  Run the configured feature matcher on preprocessed inputs.

Args:
    data: Matcher input dictionary, usually containing ``image0`` and
        ``image1`` tensors.

Returns:
    A correspondence dictionary produced by ``self.matcher``.
    Typical entries are matched keypoints and batch indices used later
    for homography estimation.
)r   )r&   rw   s   &&r   Ú
on_matcherÚImageStitcher.on_matcherÁ   s   € ð �|‰|˜DÓ!Ð!r   c                óæ   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[S[ P                  ,          RS[S[ P                  ,          RS[S[ P                  S[ P                  3,          /# )r   Úimages_leftÚimages_rightÚ	mask_leftÚ
mask_rightr   )r-   r.   r   r   )r   r   s   "€r   r   r   Ï   sp   ø€ ÷ ;mñ ;má—\‘\ð;mñ —l‘lð;mñ ™EŸL™LÕ)ð	;mñ
 ™UŸ\™\Õ*ð;mñ 
‰u�|‰|™UŸ\™\Ð)Õ	*ñ;mr   c           
     ó¢  € V P                  W4      pVP                  R	,          VP                  R
,          VP                  R
,          ,           3pV P                  V4      pV P                  ! RRVP                  ^ ,          /VB p\        P
                  ! V\        P                  ! V4      .R
4      p	Vf   \        P                  ! V4      pVf   \        P                  ! V4      pVP                  ^,          p
\        P                  ! VP                  \        P                  4      p\        P                  ! VP                  P                  RR7      ;_uu_ 4        \        \        P
                  ! VP                  V4      VP                  V4      .^4      VP                  V4      V4      pRRR4       XRRV
13,          P                  VP                  4      p\!        RR\        P"                  ! V4      P$                  ,          \!        V4      ,          4      pVRV
R13,          RV,
          8¬  P                  VP                  4      p\        P
                  ! V\        P                  ! V4      .R
4      pV P'                  WÙV4      VV,           P)                  4       P                  VP                  4      3#   + '       g   i     EL; i)aÚ  Stitch two images into a shared panorama frame.

Args:
    images_left: Reference image tensor, shaped ``(B, C, H, W_l)``.
    images_right: Image tensor to be warped onto the reference frame,
        shaped ``(B, C, H, W_r)``.
    mask_left: Optional validity mask for ``images_left`` with the same
        shape as ``images_left``.
    mask_right: Optional validity mask for ``images_right`` with the same
        shape as ``images_right``.

Returns:
    A tuple ``(stitched_image, stitched_mask)``:
    - ``stitched_image`` is the blended panorama tensor with width
      ``W_l + W_r`` before final cropping.
    - ``stitched_mask`` is a mask tensor indicating which panorama
      pixels are valid after warping and blending.
r6   NF)Údevice_typeÚenabledr0   g-Cëâ6?g      @g      ð?éþÿÿÿéÿÿÿÿ© )rg   Úshapery   rS   r-   rF   Ú
zeros_liker1   Úpromote_typesÚdtypeÚfloat32ÚautocastÚdeviceÚtyper   ÚtorA   ÚfinfoÚepsr]   rm   )r&   r|   r}   r~   r   rf   Ú	out_shapeÚcorrespondencesr2   rW   Ún_chÚworkÚwarpedrV   ÚtolÚsrc_maskÚdst_masks   &&&&&            r   Ústitch_pairÚImageStitcher.stitch_pairÏ   s+  € ð4 —_‘_ [Ó?ˆ
Ø ×&Ñ& rÕ*¨K×,=Ñ,=¸bÕ,AÀL×DVÑDVÐWYÕDZÕ,ZÐ[ˆ	ØŸ/™/¨*Ó5ˆØ×&Ò&ÑZ°+×2CÑ2CÀAÕ2FÐZÈ/ÑZˆÜ—)’)˜[¬%×*:Ò*:¸<Ó*HÐIÈ2ÓNˆð ÒÜŸš¨Ó4ˆIØÒÜŸš¨Ó6ˆJð ×!Ñ! !Õ$ˆÜ×"Ò" <×#5Ñ#5´u·}±}ÓEˆÜ�^Š^¨×(;Ñ(;×(@Ñ(@È%×PÖPÜ%Ü—	’	˜<Ÿ?™?¨4Ó0°*·-±-ÀÓ2EÐFÈÓJÈDÏGÉGÐTXËMÐ[dóˆF÷ Qð ˜˜E˜T˜E˜Õ"×%Ñ% l×&8Ñ&8Ó9ˆô �$˜œeŸkšk¨$Ó/×3Ñ3Õ3´c¸)³nÕDÓEˆØ˜1˜d™e˜8Õ$¨¨c­	Ñ1×5Ñ5°l×6HÑ6HÓIˆÜ—9’9˜i¬×)9Ò)9¸*Ó)EÐFÈÓKˆØ×Ñ °(Ó;¸hÈÕ>Q×=WÑ=WÓ=Y×=\Ñ=\Ð]e×]kÑ]kÓ=lÐlÐl÷ Q×PÐPús   ÅAJ=Ê=K	c                óN   <€ V ^8„  d   QhRS[ P                  RS[ P                  /# )r   Úimgsr   r,   )r   r   s   "€r   r   r     s#   ø€ ÷ 4ñ 4™UŸ\™\ð 4©e¯l©lñ 4r   c                óê   € V^ ,          p\         P                  ! V4      p\        \        V4      ^,
          4       F%  pV P	                  W!V^,           ,          V4      w  r#K'  	  V P                  W#4      # )a„  Iteratively stitch a sequence of overlapping images.

Args:
    *imgs: Ordered image tensors from left to right. Each tensor is expected
        to share a common height and overlap with the next image.
        Each tensor is RGB, ``(B, 3, H, W)``.

Returns:
    A single panorama tensor obtained by stitching all provided images
    in order and cropping it as :meth:`postprocess` does.
)r-   r1   rC   Úlenr™   rt   )r&   rœ   Úimg_outr~   rN   s   &*   r   ÚforwardÚImageStitcher.forward  sb   € ð �q•'ˆÜ—O’O GÓ,ˆ	Ü”s˜4“y 1•}Ö%ˆAØ!%×!1Ñ!1°'ÀÀAÅ½;È	Ó!RÑˆG‘Yñ &à×Ñ Ó3Ð3r   )r   r   r   r   )r   rZ   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r$   r4   rS   r]   rg   rt   ry   r™   r    Ú__static_attributes__Ú__classdictcell__Ú__classcell__)r'   r   s   @@r   r   r      sv   ù‡ € ñ5÷n/õ /÷ð ð&" Ø?C÷" ò " ÷Hð ÷
ð 
÷#ð #÷*"ð "÷;mò ;m÷z4÷ 4ð 4r   r   )Útypingr   r   r   r-   r   Úkornia.colorr   Úkornia.featurer   r	   Úkornia.geometry.homographyr
   Úkornia.geometry.ransacr   Úkornia.geometry.transformr   r   r   r…   r   r   Ú<module>r°      s4   ð÷$ )Ñ (ã Ý å )ß 5Ý CÝ )Ý 6ô~4�B—I‘Iö ~4r   