U
    ?h                     @   s   d dl Zd dlmZ ddlmZ ddlmZmZm	Z	 dddd	gZ
d
d Zdd ZddddZddddZdd Zdddd	ZdS )    N)entropy   )dtype_range)_supported_float_typecheck_shape_equalitywarnmean_squared_errornormalized_root_msepeak_signal_noise_rationormalized_mutual_informationc                 C   s4   t | j|jf}tj| |d} tj||d}| |fS )zK
    Promote im1, im2 to nearest appropriate floating point precision.
    dtype)r   r   npZasarray)image0image1Z
float_type r   P/var/www/html/venv/lib/python3.8/site-packages/skimage/metrics/simple_metrics.py
_as_floats   s    r   c                 C   s0   t | | t| |\} }tj| | d tjd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   r   r   r   r      s    
	euclidean)normalizationc                C   s   t | | t| |\} }| }|dkrDttj| |  tjd}n4|dkr^|  |   }n|dkrp|  }nt	dtt
| || 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   denomr   r   r   r	   2   s    -

)
data_rangec                C   s   t | | |dkrz| j|jkr&td t| jj \}}t| t|  }}||ks\||k rdtd|dkrr|}n|| }t	| |\} }t
| |}dt|d |  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

    NzFInputs 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   log10)r   r   r    ZdminZdmaxZtrue_minZtrue_maxerrr   r   r   r
   o   s     

c                 C   sZ   t dd t|| jD s2td| d| j ddd t|| jD }tj| |dd	d
S )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                 s   s   | ]\}}||kV  qd S )Nr   .0sir   r   r   	<genexpr>   s     z_pad_to.<locals>.<genexpr>zTarget shape z# cannot be smaller than inputshape z along any axis.c                 S   s   g | ]\}}d || fqS )r   r   r%   r   r   r   
<listcomp>   s     z_pad_to.<locals>.<listcomp>Zconstantr   )Z	pad_widthmodeZconstant_values)allzipshaper   r   pad)Zarrr.   paddingr   r   r   _pad_to   s    r1   d   )binsc                C   s   | j |j kr&td| j  d|j  d| j|jkrXt| j|j}t| |}t||}n
| | }}tjt|dt|dg|dd\}}ttj	|dd}ttj	|d	d}	tt|d}
||	 |
 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}{x \log x}` is the entropy.

    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)r3   Zdensityr   )Zaxis   )
ndimr   r.   r   maximumr1   ZhistogramddZreshaper   sum)r   r   r3   Z	max_shapeZpadded0Zpadded1histZ	bin_edgesZH0ZH1ZH01r   r   r   r      s     /


)numpyr   Zscipy.statsr   Z
util.dtyper   Z_shared.utilsr   r   r   __all__r   r   r	   r
   r1   r   r   r   r   r   <module>   s   
=7