
    0-Ph(                         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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gaussian_weightsfullc                
   t          | |           t          | j                  }	|st          |||d||          }
|
                    |           | j        |         }t          j        ||	          }|rt          j        | j        |	          }|rt          j        | j        |	          }|| j        z  }t          j
        t          j        |          }t          |          D ]}t          |  ||                   | ||                   fi |
}|r$|r"|\  ||<   | ||          <   | ||          <   T|r|\  ||<   | ||          <   k|r|\  ||<   | ||          <   |||<   |                                }|r|r|||fS |r||fS |r||fS |S |                    dd          }|                    dd          }|                    d	d
          }|dk     rt#          d          |dk     rt#          d          |dk     rt#          d          |                    dd          }|rd}|"|rt%          ||z  dz             }d|z  dz   }nd}t          j        t          j        | j                  |z
  dk               rt#          d          |dz  dk    st#          d          |t          j        | j        t          j                  s$t          j        |j        t          j                  rt#          d          | j        |j        k    rt/          dd           t0          | j        j                 \  }}||z
  }t          j        | j        t          j                  r1| j        t          j        k    rt/          dd|ddz   dz   d           | j        }|rt8          }||d d!}nt:          }d"|i}|                     |	d#$          } |                    |	d#$          }||z  }|r	||dz
  z  }nd%} || fi |} ||fi |}  || | z  fi |}! |||z  fi |}" || |z  fi |}#||!||z  z
  z  }$||"| | z  z
  z  }%||#|| z  z
  z  }&|}'||'z  dz  }(||'z  dz  })d|z  | z  |(z   d|&z  |)z   |dz  | dz  z   |(z   |$|%z   |)z   f\  }*}+},}-|,|-z  }.|*|+z  |.z  }|dz
  dz  }/t?          ||/                              t          j                   }|r[ ||*|.z  fi || z  }0|0 || |-z  fi ||z  z  }0|0 |||+|*z
  z  | |-|,z
  z  |z  z
  |.z  fi |z  }0|0d| j!        z  z  }0|r||0|fS ||0fS |r||fS |S )&a  
    Compute the mean structural similarity index between two images.
    Please pay attention to the `data_range` parameter with floating-point 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 (difference between maximum and
        minimum possible values). By default, this is estimated from the image
        data type. This estimate may be wrong for floating-point image data.
        Therefore it is recommended to always pass this scalar value explicitly
        (see note below).
    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.
    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
    -----
    If `data_range` is not specified, the range is automatically guessed
    based on the image data type. However for floating-point image data, this
    estimate yields a result double the value of the desired range, as the
    `dtype_range` in `skimage.util.dtype.py` has defined intervals from -1 to
    +1. This yields an estimate of 2, instead of 1, which is most often
    required when working with image data (as negative light intensities are
    nonsensical). In case of working with YCbCr-like color data, note that
    these ranges are different per channel (Cb and Cr have double the range
    of Y), so one cannot calculate a channel-averaged SSIM with a single call
    to this function, as identical ranges are assumed for each channel.

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

    .. 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`

    Nr   )dtype)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.zSince image dtype is floating point, you must specify the data_range parameter. Please read the documentation carefully (including the note). It is recommended that you always specify the data_range anyway.zEInputs have mismatched dtypes. Setting data_range based on im1.dtype.)
stacklevelz'Setting data_range based on im1.dtype. zdata_range = z.0fz. z7Please specify data_range explicitly to avoid mistakes.reflect)r   truncatemodesizeF)copyg      ?)"r   r   r   dictupdateshapenpemptyndim	functoolspartialr   slice_at_axisranger   meanpop
ValueErrorintanyasarray
issubdtypefloatingr	   r   typeintegeruint8r   r   astyper
   float64r!   )1im1im2r   r   r   r   r   r   kwargs
float_typeargsnchmssimGS_atch	ch_resultr   r   r   r   r   rdmindmaxr(   filter_funcfilter_argsNPcov_normuxuyuxxuyyuxyvxvyvxyRC1C2A1A2B1B2Dpadgrads1                                                    f/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/metrics/_structural_similarity.pyr   r      s   N c"""&sy11J!-
 
 
 	Fi%J/// 	6*555A 	6*555A#ch. 3,GGG** 		& 		&B-c##b''lCBLQQDQQI &D &4=1b	1SSWW:qRzz &(1%b	1SSWW:: &(1%b	1SSWW::%b		

 	 	!Q; 	!8O 	!8OL	D$		B	D$		BJJw$$E	Avv.///	Avv.///qyy1222"JJ'>EE   	Hu$s*++A1uqyHHH	vrz#)$$x/1455 

=	
 	
 		
 qLA3444=BK00 	BMIr{5
 5
 	 <   9	!!W    !0
dD[
=BJ// 	SY"(5J5J94*44445KL 	    8D ) %8YOO$x( **Ze*
,
,C
**Ze*
,
,C	4B  a= 
S	(	(K	(	(B	S	(	(K	(	(B +cCi
/
/;
/
/C
+cCi
/
/;
/
/C
+cCi
/
/;
/
/C	S27]	#B	S27]	#B
cBGm
$CA
q&QB
q&QB 	
Bb	C"
AA
R"	NBB 	RA	bAA a<A
C CLLBJ//E {2611[11C7QBG33{33c99R27^bBGnq.@@AEUUUUUCH 	$>!$; 	!8OL    )r)   numpyr&   scipy.ndimager   _sharedr   _shared.filtersr   _shared.utilsr   r   r	   util.arraycropr
   
util.dtyper   __all__r    r`   r_   <module>rj      s            ( ( ( ( ( (       & & & & & & M M M M M M M M M M ! ! ! ! ! ! $ $ $ $ $ $"
# 	U U U U U U Ur`   