o
    eB5                     @   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 Z	dd	 Z
	
	 	dddddZ	
	 	dddddZ	
	 	dddddZ	
	 	dddddZdS )    N   )gaussian)convert_to_float)resizec                 C   sJ   t | }|dur|| j }|f| jd  }nd}t| |||||d |S )z?Return image with each channel smoothed by the Gaussian filter.N   )outputmodecvalchannel_axis)npZ
empty_likendimr   )imagesigmar   r	   r
   smoothed r   JD:\Projects\ConvertPro\env\Lib\site-packages\skimage/transform/pyramids.py_smooth
   s   

r   c                 C   s   | dkrt dd S )Nr   z#scale factor must be greater than 1)
ValueError)factorr   r   r   _check_factor   s   r   r   reflectFr
   c                   s   t  t| |}  dur" | j  t fddt| jD }ntfdd| jD }|du r8d d }t| ||| }	t|	||||dd}
|
S )	a6  Smooth and then downsample image.

    Parameters
    ----------
    image : ndarray
        Input image.
    downscale : float, optional
        Downscale factor.
    sigma : float, optional
        Sigma for Gaussian filter. Default is `2 * downscale / 6.0` which
        corresponds to a filter mask twice the size of the scale factor that
        covers more than 99% of the Gaussian distribution.
    order : int, optional
        Order of splines used in interpolation of downsampling. See
        `skimage.transform.warp` for detail.
    mode : {'reflect', 'constant', 'edge', 'symmetric', 'wrap'}, optional
        The mode parameter determines how the array borders are handled, where
        cval is the value when mode is equal to 'constant'.
    cval : float, optional
        Value to fill past edges of input if mode is 'constant'.
    preserve_range : bool, optional
        Whether to keep the original range of values. Otherwise, the input
        image is converted according to the conventions of `img_as_float`.
        Also see https://scikit-image.org/docs/dev/user_guide/data_types.html
    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.

    Returns
    -------
    out : array
        Smoothed and downsampled float image.

    References
    ----------
    .. [1] http://persci.mit.edu/pub_pdfs/pyramid83.pdf

    Nc                 3   2    | ]\}}| krt |t n|V  qd S Nmathceilfloat.0axdr
   	downscaler   r   	<genexpr>Q   
    
z!pyramid_reduce.<locals>.<genexpr>c                 3   "    | ]}t |t  V  qd S r   r   r   r!   r#   r   r   r$   V   s     r         @Forderr   r	   Zanti_aliasing)r   r   r   tuple	enumerateshaper   r   )r   r#   r   r+   r   r	   preserve_ranger
   	out_shaper   outr   r"   r   pyramid_reduce   s   -


r2   c                   s   t  t| |}  dur" | j  t fddt| jD }ntfdd| jD }|du r8d d }t| ||||dd}	t|	||| }
|
S )	a*  Upsample and then smooth image.

    Parameters
    ----------
    image : ndarray
        Input image.
    upscale : float, optional
        Upscale factor.
    sigma : float, optional
        Sigma for Gaussian filter. Default is `2 * upscale / 6.0` which
        corresponds to a filter mask twice the size of the scale factor that
        covers more than 99% of the Gaussian distribution.
    order : int, optional
        Order of splines used in interpolation of upsampling. See
        `skimage.transform.warp` for detail.
    mode : {'reflect', 'constant', 'edge', 'symmetric', 'wrap'}, optional
        The mode parameter determines how the array borders are handled, where
        cval is the value when mode is equal to 'constant'.
    cval : float, optional
        Value to fill past edges of input if mode is 'constant'.
    preserve_range : bool, optional
        Whether to keep the original range of values. Otherwise, the input
        image is converted according to the conventions of `img_as_float`.
        Also see https://scikit-image.org/docs/dev/user_guide/data_types.html
    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.

    Returns
    -------
    out : array
        Upsampled and smoothed float image.

    References
    ----------
    .. [1] http://persci.mit.edu/pub_pdfs/pyramid83.pdf

    Nc                 3   s.    | ]\}}| krt | n|V  qd S r   r   r   r   r
   upscaler   r   r$      s
    
z!pyramid_expand.<locals>.<genexpr>c                 3   s    | ]
}t  | V  qd S r   r3   r'   )r5   r   r   r$      s    r   r)   Fr*   )r   r   r   r,   r-   r.   r   r   )r   r5   r   r+   r   r	   r/   r
   r0   Zresizedr1   r   r4   r   pyramid_expandc   s   -


r6   c             	   c   s~    t | t| |} d}	| j}
| }| V  |	|kr=|	d7 }	t|||||||d}|
}|}|j}
|
|kr4dS |V  |	|ksdS dS )a  Yield images of the Gaussian pyramid formed by the input image.

    Recursively applies the `pyramid_reduce` function to the image, and yields
    the downscaled images.

    Note that the first image of the pyramid will be the original, unscaled
    image. The total number of images is `max_layer + 1`. In case all layers
    are computed, the last image is either a one-pixel image or the image where
    the reduction does not change its shape.

    Parameters
    ----------
    image : ndarray
        Input image.
    max_layer : int, optional
        Number of layers for the pyramid. 0th layer is the original image.
        Default is -1 which builds all possible layers.
    downscale : float, optional
        Downscale factor.
    sigma : float, optional
        Sigma for Gaussian filter. Default is `2 * downscale / 6.0` which
        corresponds to a filter mask twice the size of the scale factor that
        covers more than 99% of the Gaussian distribution.
    order : int, optional
        Order of splines used in interpolation of downsampling. See
        `skimage.transform.warp` for detail.
    mode : {'reflect', 'constant', 'edge', 'symmetric', 'wrap'}, optional
        The mode parameter determines how the array borders are handled, where
        cval is the value when mode is equal to 'constant'.
    cval : float, optional
        Value to fill past edges of input if mode is 'constant'.
    preserve_range : bool, optional
        Whether to keep the original range of values. Otherwise, the input
        image is converted according to the conventions of `img_as_float`.
        Also see https://scikit-image.org/docs/dev/user_guide/data_types.html
    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.

    Returns
    -------
    pyramid : generator
        Generator yielding pyramid layers as float images.

    References
    ----------
    .. [1] http://persci.mit.edu/pub_pdfs/pyramid83.pdf

    r   r   r   N)r   r   r.   r2   )r   	max_layerr#   r   r+   r   r	   r/   r
   layercurrent_shapeZprev_layer_imageZlayer_imageZ
prev_shaper   r   r   pyramid_gaussian   s&   8

r;   c             	   #   s   t  t| |} |du rd d }| j}	t| ||| }
| |
 V   dur; | j  t|	}|  t|}n|	}|dkrLt	t
t|}t|D ]:} duret fddt|	D }ntfdd|	D }t|
||||dd	}t|||| }
|j}	||
 V  qPdS )
ag	  Yield images of the laplacian pyramid formed by the input image.

    Each layer contains the difference between the downsampled and the
    downsampled, smoothed image::

        layer = resize(prev_layer) - smooth(resize(prev_layer))

    Note that the first image of the pyramid will be the difference between the
    original, unscaled image and its smoothed version. The total number of
    images is `max_layer + 1`. In case all layers are computed, the last image
    is either a one-pixel image or the image where the reduction does not
    change its shape.

    Parameters
    ----------
    image : ndarray
        Input image.
    max_layer : int, optional
        Number of layers for the pyramid. 0th layer is the original image.
        Default is -1 which builds all possible layers.
    downscale : float, optional
        Downscale factor.
    sigma : float, optional
        Sigma for Gaussian filter. Default is `2 * downscale / 6.0` which
        corresponds to a filter mask twice the size of the scale factor that
        covers more than 99% of the Gaussian distribution.
    order : int, optional
        Order of splines used in interpolation of downsampling. See
        `skimage.transform.warp` for detail.
    mode : {'reflect', 'constant', 'edge', 'symmetric', 'wrap'}, optional
        The mode parameter determines how the array borders are handled, where
        cval is the value when mode is equal to 'constant'.
    cval : float, optional
        Value to fill past edges of input if mode is 'constant'.
    preserve_range : bool, optional
        Whether to keep the original range of values. Otherwise, the input
        image is converted according to the conventions of `img_as_float`.
        Also see https://scikit-image.org/docs/dev/user_guide/data_types.html
    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.

    Returns
    -------
    pyramid : generator
        Generator yielding pyramid layers as float images.

    References
    ----------
    .. [1] http://persci.mit.edu/pub_pdfs/pyramid83.pdf
    .. [2] http://sepwww.stanford.edu/data/media/public/sep/morgan/texturematch/paper_html/node3.html

    Nr   r)   r7   c                 3   r   r   r   r   r"   r   r   r$   V  r%   z$pyramid_laplacian.<locals>.<genexpr>c                 3   r&   r   r   r'   r(   r   r   r$   [  s    Fr*   )r   r   r.   r   r   listpopr,   r   r   logmaxranger-   r   )r   r8   r#   r   r+   r   r	   r/   r
   r:   Zsmoothed_imageZshape_without_channelsr9   r0   Zresized_imager   r"   r   pyramid_laplacian   sB   <






rA   )r   Nr   r   r   F)r7   r   Nr   r   r   F)r   numpyr   Z_shared.filtersr   Z_shared.utilsr   Z	transformr   r   r   r2   r6   r;   rA   r   r   r   r   <module>   s>    DCV