
\c           @   s   d  d l  m Z d  d l Z d  d l m Z m Z d d l m Z d d l	 m
 Z
 d d l m Z d g Z d e d e e e d	  Z d S(
   i(   t   divisionN(   t   uniform_filtert   gaussian_filteri   (   t   dtype_range(   t   crop(   t   warnt   compare_ssimc   -      K   s  |  j  | j  k s! t d   n  | rt d | d | d | d t d | d |  }	 |	 j |  |  j  d }
 t j |
  } | r t j |  j   } n  | r t j |  j   } n  x t |
  D] } t |  d	 | f | d	 | f |	  } | r(| r(| \ | d	 | f <| d	 | f <| d	 | f <q | rQ| \ | d	 | f <| d	 | f <q | rz| \ | d	 | f <| d	 | f <q | | d	 | f <q W| j	   } | r| r| | | f S| r| | f S| r| | f S| Sn  | j
 d
 d  } | j
 d d  } | j
 d d  } | d k  r+t d   n  | d k  rFt d   n  | d k  rat d   n  | j
 d t  } | d k r| rd } qd } n  t j t j |  j   | d k   rt d   n  | d d k st d   n  | d k r8|  j | j k rt d  n  t |  j j \ } } | | } n  |  j } | r]t } i | d 6} n t } i | d 6} |  j t j  }  | j t j  } | | } | r| | d } n d } | |  |  } | | |  } | |  |  |  } | | | |  } | |  | |  } | | | | }  | | | | }! | | | | }" | }# | |# d }$ | |# d }% d | | |$ d |" |% | d | d |$ |  |! |% f \ }& }' }( }) |( |) }* |& |' |* } | d d }+ t | |+  j	   } | r| |& |* |  |  }, |, | | |) |  | 7}, |, | | |' |& | |) |( | |* |  7}, |, d |  j 9}, | r{| |, | f S| |, f Sn | r| | f S| Sd S(   sy
  Compute the mean structural similarity index between two images.

    Parameters
    ----------
    X, Y : ndarray
        Image.  Any dimensionality.
    win_size : int or None
        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 Y.
    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.
    multichannel : bool, optional
        If True, treat the last dimension of the array as channels. Similarity
        calculations are done independently for each channel then averaged.
    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, return the full structural similarity image instead of the
        mean value.

    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
        sigma for the Gaussian when `gaussian_weights` is True.

    Returns
    -------
    mssim : float
        The mean structural similarity over the image.
    grad : ndarray
        The gradient of the structural similarity index between X and Y [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.

    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.
       http://arxiv.org/abs/0901.0065,
       DOI:10.1007/s10043-009-0119-z

    s+   Input images must have the same dimensions.t   win_sizet   gradientt
   data_ranget   multichannelt   gaussian_weightst   fulli.t   K1g{Gz?t   K2gQ?t   sigmag      ?i    s   K1 must be positives   K2 must be positives   sigma must be positivet   use_sample_covariancei   i   sd   win_size exceeds image extent.  If the input is a multichannel (color) image, set multichannel=True.i   i   s   Window size must be odd.sC   Inputs have mismatched dtype.  Setting data_range based on X.dtype.t   sizeg      ?N(   t   shapet
   ValueErrort   dictt   Falset   updatet   npt   emptyt   rangeR   t   meant   popt   Truet   Nonet   anyt   asarrayt   dtypeR   R   t   typet   ndimR   R   t   astypet   float64R   R   (-   t   Xt   YR   R   R	   R
   R   R   t   kwargst   argst   ncht   mssimt   Gt   St   cht	   ch_resultR   R   R   R   t   dmint   dmaxR"   t   filter_funct   filter_argst   NPt   cov_normt   uxt   uyt   uxxt   uyyt   uxyt   vxt   vyt   vxyt   Rt   C1t   C2t   A1t   A2t   B1t   B2t   Dt   padt   grad(    (    sE   lib/python2.7/site-packages/skimage/measure/_structural_similarity.pyR      s    G	&0##

		%	

%

(   t
   __future__R    t   numpyR   t   scipy.ndimageR   R   t
   util.dtypeR   t   util.arraycropR   t   _shared.utilsR   t   __all__R   R   R   (    (    (    sE   lib/python2.7/site-packages/skimage/measure/_structural_similarity.pyt   <module>   s   		