o
    :ήc8                     @   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
 dd Zd	d
 Zejdd			dddddZejdd			dddddZejdd			dddddZejdd			dddddZdS )    N   )utils)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)np
empty_likendimr   )imagesigmar	   r
   r   smoothed r   A/tmp/pip-target-vg8gfxp4/lib/python/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      )multichannel_positionr   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 )	a  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'.
    multichannel : bool, optional
        Whether the last axis of the image is to be interpreted as multiple
        channels or another spatial dimension. This argument is deprecated:
        specify `channel_axis` instead.
    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>W   
    
z!pyramid_reduce.<locals>.<genexpr>c                 3   "    | ]}t |t  V  qd S r   r   r#   r%   r'   r   r   r(   \   s     r         @Forderr	   r
   anti_aliasing)r   r   r   tuple	enumerateshaper   r   )r   r'   r   r/   r	   r
   multichannelpreserve_ranger   	out_shaper   outr   r&   r   pyramid_reduce    s   2


r8   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'.
    multichannel : bool, optional
        Whether the last axis of the image is to be interpreted as multiple
        channels or another spatial dimension. This argument is deprecated:
        specify `channel_axis` instead.
    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   r9   r+   )r;   r   r   r(      s    r   r-   Fr.   )r   r   r   r1   r2   r3   r   r   )r   r;   r   r/   r	   r
   r4   r5   r   r6   resizedr7   r   r:   r   pyramid_expandi   s   2


r=      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 )an	  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'.
    multichannel : bool, optional
        Whether the last axis of the image is to be interpreted as multiple
        channels or another spatial dimension. This argument is deprecated:
        specify `channel_axis` instead.
    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   r3   r8   )r   	max_layerr'   r   r/   r	   r
   r4   r5   r   layercurrent_shapeprev_layer_imagelayer_image
prev_shaper   r   r   pyramid_gaussian   s&   =

rF   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 )
aI
  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'.
    multichannel : bool, optional
        Whether the last axis of the image is to be interpreted as multiple
        channels or another spatial dimension. This argument is deprecated:
        specify `channel_axis` instead.
    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-   r?   c                 3   r   r   r   r"   r&   r   r   r(   k  r)   z$pyramid_laplacian.<locals>.<genexpr>c                 3   r*   r   r   r+   r,   r   r   r(   p  s    Fr.   )r   r   r3   r   r   listpopr1   r   r    logmaxranger2   r   )r   r@   r'   r   r/   r	   r
   r4   r5   r   rB   smoothed_imageshape_without_channelsrA   r6   resized_imager   r&   r   pyramid_laplacian  sB   A






rO   )r   Nr   r   r   FF)r?   r   Nr   r   r   FF)r   numpyr   _sharedr   _shared.filtersr   _shared.utilsr   	transformr   r   r   deprecate_multichannel_kwargr8   r=   rF   rO   r   r   r   r   <module>   sH    

H
G
Z