+
    ÿäûjˆ,  ã                   ó2   € ^ RI t R R ltR R ltR R ltR# )é    Nc                óx   € V ^8„  d   QhR\         P                  R\         P                  R\         P                  /# )é   ÚsourceÚtemplateÚreturn©ÚtorchÚTensor)Úformats   "Ú]C:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\contrib\histogram_matching.pyÚ__annotate__r      s7   € ÷ KDñ KDœuŸ|™|ð KD´u·|±|ð KDÌÏÉñ KDó    c                ó  € V P                   pV P                  \        P                  8X  d   \        P                  M\        P                  pV P                  4       '       d   V P                  MTpVP                  4       ^ 8X  d   \        R4      hV P                  4       ^ 8X  d   V P                  V4      # V P                  4       p VP                  4       p\        P                  ! V RRR7      w  rVp\        P                  ! VRR7      w  r‰\        P                  ! V^ R7      P                  V4      p
WªR,          ,          p
\        P                  ! V	^ R7      P                  V4      pW»R,          ,          pVP                  V4      p\        P                  ! VP                  ^4      V.4      p\        P                  ! VR,          V.4      p\        W«V4      pWÆ,          P                  V4      P                  V4      # )a|  Remap the values of an image so that its histogram matches the histogram of a template.

``out = histogram_matching(source, template)`` maps each value of ``source`` to the ``template`` value at the same
quantile and returns a tensor with the shape of ``source``. One histogram covers all of ``source``, batch and
channels together: to match each image or channel on its own, call the function on each slice. The result is not
differentiable.

The implementation follows
`this answer <https://stackoverflow.com/questions/32655686/histogram-matching-of-two-images-in-python-2-x>`_; see
also `histogram matching <https://en.wikipedia.org/wiki/Histogram_matching>`_.

Convention:
    - This is ``skimage.exposure.match_histograms(source, template, channel_axis=None)``, which needs both inputs to
      have the same number of dimensions (flatten them otherwise).
    - A source value maps to the template value at its quantile, interpolated linearly between template values
      with :func:`interp`. The result stays inside the template's range once that range is rounded to the output
      dtype, so with a ``float64`` template and a ``float32`` source it can pass the unrounded ends by that
      rounding. It can also fall between template values: a ``uint8`` template gives fractional values, as in
      skimage.
    - A floating source keeps its dtype; an integer or ``bool`` source returns ``float32``. The quantiles are
      computed in ``float32`` (``float64`` for a ``float64`` source).
    - With a non-empty ``source``, no gradient reaches it and a backward pass into ``template`` raises.
    - An empty ``source`` with a non-empty ``template`` returns an empty tensor, still attached to its autograd
      graph.

Args:
    source: Image to transform, of any shape.
    template: Template image, of any shape.

Returns:
    The transformed output image, with the shape of the source image.

Raises:
    ValueError: if ``template`` is empty, even when ``source`` is empty too.

See Also:
    - :func:`interp`: the linear interpolation that maps the quantiles.
    - :func:`~kornia.enhance.equalize`: flattens the histogram of each channel, with no template.

z.histogram_matching needs a non-empty template.T)Úreturn_inverseÚreturn_counts)r   )Údim:Né   Néÿÿÿÿ)ÚshapeÚdtyper	   Úfloat64Úfloat32Úis_floating_pointÚnumelÚ
ValueErrorÚtoÚravelÚuniqueÚcumsumÚcatÚ	new_zerosÚinterpÚreshape)r   r   ÚoldshapeÚcompute_dtypeÚ	out_dtypeÚ_Úbin_idxÚs_countsÚt_valuesÚt_countsÚs_quantilesÚt_quantilesÚinterp_t_valuess   &&           r   Úhistogram_matchingr/      sƒ  € ðR �|‰|€HØ%+§\¡\´U·]±]Ô%B”E—M’MÌÏÉ€MØ &× 8Ñ 8× :Ò :�—’À€IØ‡~�~Ó˜1ÔÜÐIÓJÐJØ‡|�|ƒ~˜ÔØ�y‰y˜Ó#Ð#Ø�\‰\‹^€FØ�~‰~Ó€Hô !Ÿ<š<¨¸tÐSWÔXÑ€A�ÜŸš h¸dÔCÑ€Hô —,’,˜x¨QÔ/×2Ñ2°=ÓA€KØ¨B¥Õ/€KÜ—,’,˜x¨QÔ/×2Ñ2°=ÓA€KØ¨B¥Õ/€KØ�{‰{˜=Ó)€Hô —)’)˜[×2Ñ2°1Ó5°{ÐCÓD€KÜ�yŠy˜( 2�,¨Ð1Ó2€HÜ˜[°xÓ@€Oð Õ#×+Ñ+¨HÓ5×8Ñ8¸ÓCÐCr   c                ó˜   € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  /# ©r   ÚxÚxpÚfpr   r   )r   s   "r   r   r   c   s8   € ÷ $#ñ $#Œe�l‰lð $#¤§¡ð $#´%·,±,ð $#Ä5Ç<Á<ñ $#r   c                óÄ   € VP                  4       '       d@   \        P                  ! \        WVP                  4      \        WVP
                  4      4      # \        WV4      # )aí  Interpolate samples ``(xp, fp)`` linearly at query points, extrapolating the end segments.

``y = interp(x, xp, fp)`` evaluates the piecewise-linear function through the 1-D samples ``xp`` (increasing) and
``fp`` at every element of ``x`` and returns a tensor with the shape of ``x``. Outside ``[xp[0], xp[-1]]`` it
extends the first and last segments, where :func:`numpy.interp` clamps to ``fp[0]`` and ``fp[-1]``. It is
differentiable in ``x``, ``xp`` and ``fp``.

Convention:
    - SciPy's ``interp1d(xp, fp, fill_value="extrapolate")`` makes the same extrapolation choice. At a knot the
      result is that knot's ``fp``. ``xp`` must be increasing, which is not checked.
    - The output dtype is the promoted dtype of ``x``, ``xp`` and ``fp``, or the default float dtype when all three
      are integer. A complex ``fp`` interpolates its real and imaginary parts separately.
    - A ``float16`` or ``bfloat16`` result is computed in ``float32`` and cast to the output dtype once, so
      coordinate differences above 65504 do not overflow the weight and a segment whose ``fp`` difference overflows
      the output dtype stays finite. Inside a segment the result is within about half an ulp of the segment's
      larger end value, and the knots are exact. ``float32`` and ``float64`` results are computed in their own
      dtype. An integer ``x`` or ``xp`` mixed with floating coordinates is converted to the dtype the result is
      computed in, so in ``float32`` it is exact only up to :math:`2^{24}`: larger integer knots can merge and
      larger integer queries are rounded.

Args:
    x: the :math:`x`-coordinates at which to evaluate the interpolated values, of any shape.
    xp: the :math:`x`-coordinates of the data points, 1-D and increasing.
    fp: the :math:`y`-coordinates of the data points, 1-D with the length of ``xp``.

Returns:
    the interpolated values, with the shape of ``x``.

See Also:
    - :func:`histogram_matching`: maps image values through this interpolation of template quantiles.

)Ú
is_complexr	   ÚcomplexÚ_interp_realÚrealÚimag)r2   r3   r4   s   &&&r   r"   r"   c   sD   € ðB 
‡}�}‡‚ä�}Š}œ\¨!°·±Ó9¼<ÈÈrÏwÉwÓ;WÓXÐXÜ˜˜rÓ"Ð"r   c                ó˜   € V ^8„  d   QhR\         P                  R\         P                  R\         P                  R\         P                  /# r1   r   )r   s   "r   r   r   Š   s>   € ÷ 7Zñ 7Z”E—L‘Lð 7Z¤e§l¡lð 7Z¼¿¹ð 7ZÌÏÉñ 7Zr   c                 óŒ	  € V P                  4       ;'       g    VP                  4       p\        P                  ! \        P                  ! V P                  VP                  4      VP                  4      pV'       gC   VP                  4       '       g-   \        P                  ! ^ VP
                  R7      P                  p\        P                  ! V\        P                  4      pV'       d   TM\        P                  pV P                  V4      VP                  V4      VP                  V4      r!p \        P                  ! \        P                  ! WRR7      ^\        V4      ^,
          4      pW^,
          ,          W,          W'^,
          ,          W',          3w  r‰r«W˜,
          pV^ 8H  p\        P                  ! V\        P                  ! V4      V4      P                  V4      pW,
          P                  V4      V,          pWº,
          p\        P                  ! V4      \        P                  ! V
4      ,          \        P                  ! V4      ,          p\        P                   ! V4      p\        P                  ! VVV
4      \        P                  ! VVV4      ppVV,
          p\        P"                  ! VVV4      pVVV,          ,           pVV^V,
          ,          ,
          p\        P                  ! \        P$                  ! V4      VV4      p\        P                  ! \        P$                  ! V4      VV4      p\        P                  ! \        P$                  ! V4      W«8H  ,          V
V4      p\        P                  ! W8H  \        P                  ! \        P$                  ! V4      V
V4      \        P                  ! W	8H  \        P                  ! \        P$                  ! V4      VV4      V4      4      p\        P                  ! VW©V ,
          P                  V4      V,          ,          W¾,          ,           V4      p\        P                  ! V\        P                  ! W8  W«4      V4      p\        P                  ! \        P$                  ! V 4      \        P&                  ! V\)        R4      4      V4      P                  V4      # )r   )ÚdeviceT)ÚrightÚnan)r   r	   Úpromote_typesr   Úemptyr=   r   Úint64r   ÚclipÚsearchsortedÚlenÚwhereÚ	ones_likeÚisinfÚisfiniteÚ
zeros_likeÚlerpÚisnanÚ	full_likeÚfloat)r2   r3   r4   Úfloat_coordsr   ÚcdtypeÚcoord_dtypeÚiÚx0Úx1Úf0Úf1ÚdxÚ
zero_widthÚwÚdÚoverflowÚzeroÚf0sÚf1sÚdsÚoutÚaÚbs   &&&                     r   r8   r8   Š   s-  € ð ×&Ñ&Ó(×BÐB¨B×,@Ñ,@Ó,B€LÜ×Ò¤× 3Ò 3°A·G±G¸R¿X¹XÓ FÈÏÉÓQ€Eß˜B×0Ñ0×2Ò2ô —’˜A b§i¡iÔ0×6Ñ6ˆô × Ò  ¬¯©Ó6€Fß(‘&¬e¯k©k€KØ—‘�[Ó! 2§5¡5¨Ó#5°r·u±u¸V³}ˆ2€AÜ�
Š
”5×%Ò% b°4Ô8¸!¼SÀ»WÀq½[ÓI€AØ˜A�•Y ¥ r¨a­%¥y°"µ%Ð7�N€BˆBØ	�€BØ�q‘€JÜ	�Š�Z¤§¢°Ó!4°bÓ	9×	<Ñ	<¸VÓ	D€BØ	
��‰�FÓ˜bÕ €AØ
�€Aô �{Š{˜1‹~¤§¢¨rÓ 2Õ2´U·^²^ÀBÓ5GÕG€HÜ×Ò˜AÓ€DÜ�{Š{˜8 T¨2Ó.´·²¸HÀdÈBÓ0Oˆ€CØ	ˆs�€Bô �*Š*�S˜#˜qÓ
!€CØˆa�"�f�€AØˆb�A˜•E�lÕ€Aä
�+Š+”e—k’k #Ó&¨¨3Ó
/€CÜ
�+Š+”e—k’k #Ó&¨¨3Ó
/€CÜ
�+Š+”e—k’k #Ó&¨"©(Õ3°R¸Ó
=€Cô �+Š+Ø	‰Ü�Š”E—K’K “N B¨Ó*Ü�Š�A‘GœUŸ[š[¬¯ª°Q«¸¸QÓ?ÀÓEó€Cô �+Š+�h ¨A¥v§k¡k°&Ó&9¸BÕ&>Õ ?À"Å&Õ HÈ#Ó
N€Cô �+Š+�j¤%§+¢+¨a©f°bÓ"=¸sÓ
C€Cä�;Š;”u—{’{ 1“~¤u§¢°s¼EÀ%»LÓ'IÈ3ÓO×RÑRÐSXÓYÐYr   )r	   r/   r"   r8   © r   r   Ú<module>rd      s   ðó$ õKDõ\$#÷N7Zr   