Ë
    ÿ[;j"  ã                   ó–   — d dl Zd dlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ g d¢Zd„ Zed	„ «       Zed
dœd„«       Zddœd„Zd„ Zddœd„Zy)é    N)Úentropyé   )Údispatchable)Údtype_range)Ú_supported_float_typeÚcheck_shape_equalityÚwarn)Úmean_squared_errorÚnormalized_root_mseÚpeak_signal_noise_ratioÚnormalized_mutual_informationc                 ó¨   — t        | j                  |j                  f«      }t        j                  | |¬«      } t        j                  ||¬«      }| |fS )zK
    Promote im1, im2 to nearest appropriate floating point precision.
    ©Údtype)r   r   ÚnpÚasarray)Úimage0Úimage1Ú
float_types      úgG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\skimage/metrics/simple_metrics.pyÚ
_as_floatsr      sE   € ô '¨¯©°f·l±lÐ'CÓD€JÜ�Z‰Z˜ jÔ1€FÜ�Z‰Z˜ jÔ1€FØ�6ˆ>Ðó    c                 óŽ   — t        | |«       t        | |«      \  } }t        j                  | |z
  dz  t        j                  ¬«      S )a¶  
    Compute the mean-squared error between two images.

    Parameters
    ----------
    image0, image1 : ndarray
        Images.  Any dimensionality, must have same shape.

    Returns
    -------
    mse : float
        The mean-squared error (MSE) metric.

    Notes
    -----
    .. versionchanged:: 0.16
        This function was renamed from ``skimage.measure.compare_mse`` to
        ``skimage.metrics.mean_squared_error``.

    r   r   )r   r   r   ÚmeanÚfloat64)r   r   s     r   r
   r
      s<   € ô, ˜ Ô(Ü ¨Ó/�N€FˆFÜ�7‰7�F˜V‘O¨Ñ)´·±Ô<Ð<r   Ú	euclidean)Únormalizationc                ó®  — t        | |«       t        | |«      \  } }|j                  «       }|dk(  r<t        j                  t        j
                  | | z  t        j                  ¬«      «      }nH|dk(  r"| j                  «       | j                  «       z
  }n!|dk(  r| j                  «       }nt        d«      ‚t        j                  t        | |«      «      |z  S )a0  
    Compute the normalized root mean-squared error (NRMSE) between two
    images.

    Parameters
    ----------
    image_true : ndarray
        Ground-truth image, same shape as im_test.
    image_test : ndarray
        Test image.
    normalization : {'euclidean', 'min-max', 'mean'}, optional
        Controls the normalization method to use in the denominator of the
        NRMSE.  There is no standard method of normalization across the
        literature [1]_.  The methods available here are as follows:

        - 'euclidean' : normalize by the averaged Euclidean norm of
          ``im_true``::

              NRMSE = RMSE * sqrt(N) / || im_true ||

          where || . || denotes the Frobenius norm and ``N = im_true.size``.
          This result is equivalent to::

              NRMSE = || im_true - im_test || / || im_true ||.

        - 'min-max'   : normalize by the intensity range of ``im_true``.
        - 'mean'      : normalize by the mean of ``im_true``

    Returns
    -------
    nrmse : float
        The NRMSE metric.

    Notes
    -----
    .. versionchanged:: 0.16
        This function was renamed from ``skimage.measure.compare_nrmse`` to
        ``skimage.metrics.normalized_root_mse``.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Root-mean-square_deviation

    r   r   zmin-maxr   zUnsupported norm_type)r   r   Úlowerr   Úsqrtr   r   ÚmaxÚminÚ
ValueErrorr
   )Ú
image_trueÚ
image_testr   Údenoms       r   r   r   5   s¶   € ô\ ˜ ZÔ0Ü'¨
°JÓ?Ñ€J�
ð "×'Ñ'Ó)€MØ˜Ò#Ü—‘œŸ™ ¨jÑ!8ÄÇÁÔLÓM‰Ø	˜)Ò	#Ø—‘Ó  :§>¡>Ó#3Ñ3‰Ø	˜&Ò	 Ø—‘Ó!‰äÐ0Ó1Ð1Ü�7‰7Ô% j°*Ó=Ó>ÀÑFÐFr   )Ú
data_rangec                óÆ  — t        | |«       |€�| j                  |j                  k7  rt        d«       t        | j                  j                     \  }}t        j                  | «      t        j                  | «      }}||kD  s||k  rt        d«      ‚|dk\  r|}n||z
  }t        | |«      \  } }t        | |«      }t        |«      }dt        j                  |dz  |z  «      z  S )a   
    Compute the peak signal to noise ratio (PSNR) for an image.

    Parameters
    ----------
    image_true : ndarray
        Ground-truth image, same shape as im_test.
    image_test : ndarray
        Test image.
    data_range : int, optional
        The data range of the input image (distance between minimum and
        maximum possible values).  By default, this is estimated from the image
        data-type.

    Returns
    -------
    psnr : float
        The PSNR metric.

    Notes
    -----
    .. versionchanged:: 0.16
        This function was renamed from ``skimage.measure.compare_psnr`` to
        ``skimage.metrics.peak_signal_noise_ratio``.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio

    zFInputs have mismatched dtype.  Setting data_range based on image_true.zuimage_true has intensity values outside the range expected for its data type. Please manually specify the data_range.r   é
   r   )r   r   r	   r   Útyper   r"   r!   r#   r   r
   ÚfloatÚlog10)r$   r%   r'   ÚdminÚdmaxÚtrue_minÚtrue_maxÚerrs           r   r   r   s   sç   € ô> ˜ ZÔ0àÐØ×Ñ˜z×/Ñ/Ò/Üðôô ! ×!1Ñ!1×!6Ñ!6Ñ7‰
ˆˆdÜŸV™V JÓ/´·±¸
Ó1C�(ˆØ�dŠ?˜h¨šoÜðMóð ð �qŠ=à‰Jà ™ˆJä'¨
°JÓ?Ñ€J�
ä
˜Z¨Ó
4€CÜ�zÓ"€JØ”—‘˜* a™-¨3Ñ.Ó/Ñ/Ð/r   c                 ó  — t        d„ t        || j                  «      D «       «      st        d|› d| j                  › d�«      ‚t        || j                  «      D ��cg c]  \  }}d||z
  f‘Œ }}}t	        j
                  | |dd¬«      S c c}}w )ab  Pad an array with trailing zeros to a given target shape.

    Parameters
    ----------
    arr : ndarray
        The input array.
    shape : tuple
        The target shape.

    Returns
    -------
    padded : ndarray
        The padded array.

    Examples
    --------
    >>> _pad_to(np.ones((1, 1), dtype=int), (1, 3))
    array([[1, 0, 0]])
    c              3   ó,   K  — | ]  \  }}||k\  –— Œ y ­w)N© )Ú.0ÚsÚis      r   Ú	<genexpr>z_pad_to.<locals>.<genexpr>Â   s   è ø€ Ð8Ñ"7™$˜!˜Qˆq�A�vÑ"7ùs   ‚zTarget shape z# cannot be smaller than inputshape z along any axis.r   Úconstant)Ú	pad_widthÚmodeÚconstant_values)ÚallÚzipÚshaper#   r   Úpad)Úarrr?   r6   r7   Úpaddings        r   Ú_pad_torC   ®   s�   € ô( Ñ8¤# e¨S¯Y©YÔ"7Ó8Ô8ÜØ˜E˜7ð #Ø—Y‘Y�KÐ/ð1ó
ð 	
ô '*¨%°·±Ô&;Ô<Ñ&;™d˜a ��1�q‘5ŠzÐ&;€GÑ<Ü�6‰6�# ¨zÈ1ÔMÐMùó =s   ÁB	éd   )Úbinsc                ó˜  — | j                   |j                   k7  r&t        d| j                   › d|j                   › d�«      ‚| j                  |j                  k7  rCt        j                  | j                  |j                  «      }t        | |«      }t        ||«      }n| |}}t        j                  t        j                  |d«      t        j                  |d«      g|d¬«      \  }}t        t        j                  |d¬«      «      }t        t        j                  |d	¬«      «      }	t        t        j                  |d«      «      }
||	z   |
z  S )
a:  Compute the normalized mutual information (NMI).

    The normalized mutual information of :math:`A` and :math:`B` is given by:

    .. math::

       Y(A, B) = \frac{H(A) + H(B)}{H(A, B)}

    where :math:`H(X) := - \sum_{x \in X}{p(x) \log p(x)}` is the entropy,
    :math:`X` is the set of image values, and :math:`p(x)` is the probability
    of occurrence of value :math:`x \in X`.

    It was proposed to be useful in registering images by Colin Studholme and
    colleagues [1]_. It ranges from 1 (perfectly uncorrelated image values)
    to 2 (perfectly correlated image values, whether positively or negatively).

    Parameters
    ----------
    image0, image1 : ndarray
        Images to be compared. The two input images must have the same number
        of dimensions.
    bins : int or sequence of int, optional
        The number of bins along each axis of the joint histogram.

    Returns
    -------
    nmi : float
        The normalized mutual information between the two arrays, computed at
        the granularity given by ``bins``. Higher NMI implies more similar
        input images.

    Raises
    ------
    ValueError
        If the images don't have the same number of dimensions.

    Notes
    -----
    If the two input images are not the same shape, the smaller image is padded
    with zeros.

    References
    ----------
    .. [1] C. Studholme, D.L.G. Hill, & D.J. Hawkes (1999). An overlap
           invariant entropy measure of 3D medical image alignment.
           Pattern Recognition 32(1):71-86
           :DOI:`10.1016/S0031-3203(98)00091-0`
    z6NMI requires images of same number of dimensions. Got zD for `image0` and zD for `image1`.éÿÿÿÿT)rE   Údensityr   )Úaxisé   )
Úndimr#   r?   r   ÚmaximumrC   ÚhistogramddÚreshaper   Úsum)r   r   rE   Ú	max_shapeÚpadded0Úpadded1ÚhistÚ	bin_edgesÚH0ÚH1ÚH01s              r   r   r   Ë   s  € ðb ‡{�{�f—k‘kÒ!ÜðØ—;‘;�-Ð2Ø�{‰{ˆm˜?ð,ó
ð 	
ð
 ‡|�|�v—|‘|Ò#Ü—J‘J˜vŸ|™|¨V¯\©\Ó:ˆ	Ü˜& )Ó,ˆÜ˜& )Ó,‰à! 6�ˆä—n‘nÜ	�‰�G˜RÓ	 ¤"§*¡*¨W°bÓ"9Ð:ØØô�O€Dˆ)ô 
”—‘˜ 1Ô%Ó	&€BÜ	”—‘˜ 1Ô%Ó	&€BÜ
”"—*‘*˜T 2Ó&Ó
'€Cà�‰G�s‰?Ðr   )Únumpyr   Úscipy.statsr   Úutil._backendsr   Ú
util.dtyper   Ú_shared.utilsr   r   r	   Ú__all__r   r
   r   r   rC   r   r4   r   r   Ú<module>r^      so   ðÛ Ý å )Ý $ß MÑ Mò€òð ñ=ó ð=ð4 ØALó :Gó ð:Gðz CGô 80òvNð: ;>õ Hr   