
    3j                         S SK r S\ R                  S\ R                  S\ R                  4S jrS\ R                  S\ R                  S\ R                  S\ R                  4S	 jrg)
    Nsourcetemplatereturnc                    U R                   nU R                  5       n UR                  5       n[        R                  " U SSS9u  p4n[        R                  " USS9u  pg[        R                  " USU R
                  S9nXS   -  n[        R                  " USU R
                  S9n	XS   -  n	[        XU5      n
X   R                  U5      $ )a  Adjust the pixel values of an image to match its histogram towards a target image.

`Histogram matching <https://en.wikipedia.org/wiki/Histogram_matching>`_ is the transformation
of an image so that its histogram matches a specified histogram. In this implementation, the
histogram is computed over the flattened image array. Code referred to
`here <https://stackoverflow.com/questions/32655686/histogram-matching-of-two-images-in-python-2-x>`_.

Args:
    source: Image to transform.
    template: Template image. It can have different dimensions to source.

Returns:
    The transformed output image as the same shape as the source image.

Note:
    This function does not matches histograms element-wisely if input a batched tensor.

T)return_inversereturn_counts)r   r   )dimdtype)shaperaveltorchuniquecumsumr
   interpreshape)r   r   oldshape_bin_idxs_countst_valuest_countss_quantilest_quantilesinterp_t_valuess              [/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/contrib/histogram_matching.pyhistogram_matchingr      s    & ||H\\^F~~H !<<tSWXAhdCH ,,xQfllCKB/K,,xQfllCKB/K [x@O#++H55    xxpfpc                     [         R                  " [         R                  " XSS9S[        U5      S-
  5      nX#S-
     X   U -
  -  X#   XUS-
     -
  -  -   X   XS-
     -
  -  $ )a  One-dimensional linear interpolation for monotonically increasing sample points.

Returns the one-dimensional piecewise linear interpolant to a function with
given discrete data points :math:`(xp, fp)`, evaluated at :math:`x`.

This is confirmed to be a correct implementation.
See https://github.com/pytorch/pytorch/issues/1552#issuecomment-979998307

Args:
    x: the :math:`x`-coordinates at which to evaluate the interpolated
        values.
    xp: the :math:`x`-coordinates of the data points, must be increasing.
    fp: the :math:`y`-coordinates of the data points, same length as `xp`.

Returns:
    the interpolated values, same size as `x`.

T)right   )r   clipsearchsortedlen)r   r    r!   is       r   r   r   @   sk    & 	

5%%b48!SWq[IA1uI#beqa!e9}&=="%"QRU)BSTTr   )r   Tensorr   r    r   r   <module>r+      sg   $ (6u|| (6u|| (6 (6VUell U U%,, U5<< Ur   