a
    =b#                  	   @   s   d dl Z 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 ddlmZ ddlmZ d	gZe dd
ddd
d
d
ddd	ZdS )    N)uniform_filter   )utils)gaussian)_supported_float_typecheck_shape_equalitywarn)crop)dtype_rangestructural_similarityF)win_sizegradient
data_rangechannel_axismultichannelgaussian_weightsfullc          2      K   s  t | | t| j}
|durlt|||d||d}||	 | j| }tj||
d}|rhtj| j|
d}|r|tj| j|
d}|| j }t	j
tj|d}t|D ]}t| || ||| fi |}|r|r|\||< |||< |||< q|r|\||< |||< q|r$|\||< |||< q|||< q| }|rL|rL|||fS |rZ||fS |rh||fS |S |	dd}|	dd}|	d	d
}|dk rtd|dk rtd|dk rtd|	dd}|rd}|du r|r
t|| d }d| d }nd}tt| j| dk r2td|d dksHtd|du r| j|jkrltddd t| jj \}}|| }| j}|rt}||dd}nt}d|i}| j|
dd} |j|
dd}|| }|r||d  }nd}|| fi |} ||fi |}!|| |  fi |}"||| fi |}#|| | fi |}$||"| |    }%||#|!|!   }&||$| |!   }'|}(||( d })||( d }*d|  |! |) d|' |* | d |!d  |) |%|& |* f\}+},}-}.|-|. }/|+|, |/ }|d d }0t||0jtjd}|r||+|/ fi ||  }1|1|| |. fi || 7 }1|1|| |,|+  |!|.|-  |  |/ fi |7 }1|1d| j 9 }1|r||1|fS ||1fS n|r||fS |S dS ) a  
    Compute the mean structural similarity index between two images.

    Parameters
    ----------
    im1, im2 : ndarray
        Images. Any dimensionality with same shape.
    win_size : int or None, optional
        The side-length of the sliding window used in comparison. Must be an
        odd value. If `gaussian_weights` is True, this is ignored and the
        window size will depend on `sigma`.
    gradient : bool, optional
        If True, also return the gradient with respect to im2.
    data_range : float, 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.
    channel_axis : int or None, optional
        If None, the image is assumed to be a grayscale (single channel) image.
        Otherwise, this parameter indicates which axis of the array corresponds
        to channels.

        .. versionadded:: 0.19
           ``channel_axis`` was added in 0.19.
    multichannel : bool, optional
        If True, treat the last dimension of the array as channels. Similarity
        calculations are done independently for each channel then averaged.
        This argument is deprecated: specify `channel_axis` instead.
    gaussian_weights : bool, optional
        If True, each patch has its mean and variance spatially weighted by a
        normalized Gaussian kernel of width sigma=1.5.
    full : bool, optional
        If True, also return the full structural similarity image.

    Other Parameters
    ----------------
    use_sample_covariance : bool
        If True, normalize covariances by N-1 rather than, N where N is the
        number of pixels within the sliding window.
    K1 : float
        Algorithm parameter, K1 (small constant, see [1]_).
    K2 : float
        Algorithm parameter, K2 (small constant, see [1]_).
    sigma : float
        Standard deviation for the Gaussian when `gaussian_weights` is True.

    Returns
    -------
    mssim : float
        The mean structural similarity index over the image.
    grad : ndarray
        The gradient of the structural similarity between im1 and im2 [2]_.
        This is only returned if `gradient` is set to True.
    S : ndarray
        The full SSIM image.  This is only returned if `full` is set to True.

    Notes
    -----
    To match the implementation of Wang et. al. [1]_, set `gaussian_weights`
    to True, `sigma` to 1.5, and `use_sample_covariance` to False.

    .. versionchanged:: 0.16
        This function was renamed from ``skimage.measure.compare_ssim`` to
        ``skimage.metrics.structural_similarity``.

    References
    ----------
    .. [1] Wang, Z., Bovik, A. C., Sheikh, H. R., & Simoncelli, E. P.
       (2004). Image quality assessment: From error visibility to
       structural similarity. IEEE Transactions on Image Processing,
       13, 600-612.
       https://ece.uwaterloo.ca/~z70wang/publications/ssim.pdf,
       :DOI:`10.1109/TIP.2003.819861`

    .. [2] Avanaki, A. N. (2009). Exact global histogram specification
       optimized for structural similarity. Optical Review, 16, 613-621.
       :arxiv:`0901.0065`
       :DOI:`10.1007/s10043-009-0119-z`

    N)r   r   r   r   r   r   )dtype)ZaxisK1g{Gz?K2gQ?sigmag      ?r   zK1 must be positivezK2 must be positivezsigma must be positiveuse_sample_covarianceTg      @g      ?r         aD  win_size exceeds image extent. Either ensure that your images are at least 7x7; or pass win_size explicitly in the function call, with an odd value less than or equal to the smaller side of your images. If your images are multichannel (with color channels), set channel_axis to the axis number corresponding to the channels.zWindow size must be odd.zEInputs have mismatched dtype.  Setting data_range based on im1.dtype.)
stacklevelZreflect)r   truncatemodesizeF)copyg      ?)r   r   r   dictupdateshapenpemptyndim	functoolspartialr   Zslice_at_axisranger   Zmeanpop
ValueErrorintanyZasarrayr   r
   typer   r   Zastyper	   Zfloat64r   )2Zim1Zim2r   r   r   r   r   r   r   kwargsZ
float_typeargsZnchZmssimGSZ_atZchZ	ch_resultr   r   r   r   r   rZdminZdmaxr$   Zfilter_funcZfilter_argsZNPZcov_normZuxZuyZuxxZuyyZuxyZvxZvyZvxyRZC1ZC2ZA1ZA2ZB1ZB2DZpadZgrad r4   Elib/python3.9/site-packages/skimage/metrics/_structural_similarity.pyr      s    V






 









&

)r%   Znumpyr"   Zscipy.ndimager   Z_sharedr   Z_shared.filtersr   Z_shared.utilsr   r   r   Zutil.arraycropr	   Z
util.dtyper
   __all__Zdeprecate_multichannel_kwargr   r4   r4   r4   r5   <module>   s   