o
    e=                     @   s   d Z ddlmZmZmZ 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
l
mZ dddddejfddZdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd  d ejZdS )!zn
Augmenters that apply artistic image filters.

List of augmenters:

    * :class:`Cartoon`

Added in 0.4.0.

    )print_functiondivisionabsolute_importN)_normalize_cv2_input_arr_   )meta)color   )dtypes)
parameters         ?g       @Tc                 C   sX  t j| dgg ddd | jdkr| jd dks J d| jf ttt|d}t|d	}t|d	}t| jd
d dk }t| |} t	| }|rXtd| }	td| }
ntd| }	td| }
|d
krk| }nt
jt| |	|
|d |rt|||}nt|||}|}|dkd tj}|rt|dddd}t|dddd}tt||||S )aE	  Convert the style of an image to a more cartoonish one.

    This function was primarily designed for images with a size of ``200``
    to ``800`` pixels. Smaller or larger images may cause issues.

    Note that the quality of the results can currently not compete with
    learned style transfer, let alone human-made images. A lack of detected
    edges or also too many detected edges are probably the most significant
    drawbacks.

    This method is loosely based on the one proposed in
    https://stackoverflow.com/a/11614479/3760780

    Added in 0.4.0.

    **Supported dtypes**:

        * ``uint8``: yes; fully tested
        * ``uint16``: no
        * ``uint32``: no
        * ``uint64``: no
        * ``int8``: no
        * ``int16``: no
        * ``int32``: no
        * ``int64``: no
        * ``float16``: no
        * ``float32``: no
        * ``float64``: no
        * ``float128``: no
        * ``bool``: no

    Parameters
    ----------
    image : ndarray
        A ``(H,W,3) uint8`` image array.

    blur_ksize : int, optional
        Kernel size of the median blur filter applied initially to the input
        image. Expected to be an odd value and ``>=0``. If an even value,
        thn automatically increased to an odd one. If ``<=1``, no blur will
        be applied.

    segmentation_size : float, optional
        Size multiplier to decrease/increase the base size of the initial
        mean-shift segmentation of the image. Expected to be ``>=0``.
        Note that the base size is increased by roughly a factor of two for
        images with height and/or width ``>=400``.

    edge_prevalence : float, optional
        Multiplier for the prevalance of edges. Higher values lead to more
        edges. Note that the default value of ``1.0`` is already fairly
        conservative, so there is limit effect from lowerin it further.

    saturation : float, optional
        Multiplier for the saturation. Set to ``1.0`` to not change the
        image's saturation.

    suppress_edges : bool, optional
        Whether to run edge suppression to remove blobs containing too many
        or too few edge pixels.

    from_colorspace : str, optional
        The source colorspace. Use one of ``imgaug.augmenters.color.CSPACE_*``.
        Defaults to ``RGB``.

    Returns
    -------
    ndarray
        Image in cartoonish style.

    uint8)boolZuint16Zuint32Zuint64Zuint128Zuint256Zint8Zint16int32Zint64Zint128Zint256Zfloat16float32Zfloat64Zfloat96Zfloat128Zfloat256N)allowedZ
disallowedZ	augmenterr   r	   z.Expected to get a (H,W,C) image, got shape %s.r           r   i  
         (   )spsrdstd         F)inverse   T)iadtZgate_dtypesndimshapemaxintnpround_blur_medianZ
zeros_likecv2ZpyrMeanShiftFilteringr   _find_edges_canny_find_edges_laplacianastyper   _suppress_edge_blobs	_saturate_blend_edges)image
blur_ksizesegmentation_size
saturationedge_prevalenceZsuppress_edgesfrom_colorspaceZis_small_imageZ	image_segZspatial_window_radiusZcolor_window_radiusZ	edges_rawedges r6   JD:\Projects\ConvertPro\env\Lib\site-packages\imgaug/augmenters/artistic.pystylize_cartoon   sZ   K







r8   c                 C   sL   t jt| t j|d}|d }ttdd|  d}tt	|||}|S )NZto_colorspacer4   .r      r      )
colorlibchange_colorspace_r%   copyCSPACE_GRAYminr$   r(   ZCannyr   )r/   edge_multiplierr4   
image_graythreshr5   r6   r6   r7   r)      s   r)   c                 C   s   t jt| t j|d}|d }tt|d tj}t	|}|d }t
|ttdd|  d}t|d|| }tt|d	 d
dtj}t|d}t|d}|S )Nr9   r:        o@r	   Z   r   c   r   r   r   r   2   )r=   r>   r%   r?   r@   r(   Z	Laplacianr   ZCV_64FabsZ
percentilerA   r$   clipr&   r+   r   r'   
_threshold)r/   rB   r4   rC   Zedges_fZvmaxZedges_uint8r6   r6   r7   r*      s    
 

r*   c                 C   s0   |d dkr
|d7 }|dkr| S t t| |S )Nr	   r   r   )r(   Z
medianBlurr   )r/   Zksizer6   r6   r7   r'      s
   r'   c                 C   s   | |k }t | }d||< |S )Nr   )r%   r?   )r/   rD   maskresultr6   r6   r7   rK      s   
rK   c                 C   sV   t j||ft jd}tt| d d|}|r||k }n||k}t | } d| |< | S )N)ZdtyperE   r   )r%   Zonesr   r(   Zfilter2Dr   r?   )r5   sizerD   r   ZkernelcountsrL   r6   r6   r7   r,      s   

r,   c                 C   s   t | } t j|dddr| S tj| tj|d}|d d d d df }t |t j| ddt j	}||d d d d df< tj||tjd}|S )Nr   g{Gz?)Zatolr9   r   r   r   )
r%   r?   iscloser=   r>   Z
CSPACE_HSVrJ   r+   r   r   )r/   factorr4   ZhsvsatZ	image_satr6   r6   r7   r-      s   
"r-   c                 C   sB   d|d  }t |dt jf d}t t | | ddt jS )Nr   rE   .)r   r   r   r   )r%   ZtileZnewaxisrJ   r&   r+   r   )r/   Zimage_edgesr6   r6   r7   r.      s   r.   c                	       sN   e Zd ZdZddddejddddf	 fdd		Zd
d Zdd Zdd Z	  Z
S )Cartoona  Convert the style of images to a more cartoonish one.

    This augmenter was primarily designed for images with a size of ``200``
    to ``800`` pixels. Smaller or larger images may cause issues.

    Note that the quality of the results can currently not compete with
    learned style transfer, let alone human-made images. A lack of detected
    edges or also too many detected edges are probably the most significant
    drawbacks.

    Added in 0.4.0.

    **Supported dtypes**:

    See :func:`~imgaug.augmenters.artistic.stylize_cartoon`.

    Parameters
    ----------
    blur_ksize : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Median filter kernel size.
        See :func:`~imgaug.augmenters.artistic.stylize_cartoon` for details.

            * If ``number``: That value will be used for all images.
            * If ``tuple (a, b) of number``: A random value will be uniformly
              sampled per image from the interval ``[a, b)``.
            * If ``list``: A random value will be picked per image from the
              ``list``.
            * If ``StochasticParameter``: The parameter will be queried once
              per batch for ``(N,)`` values, where ``N`` is the number of
              images.

    segmentation_size : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Mean-Shift segmentation size multiplier.
        See :func:`~imgaug.augmenters.artistic.stylize_cartoon` for details.

            * If ``number``: That value will be used for all images.
            * If ``tuple (a, b) of number``: A random value will be uniformly
              sampled per image from the interval ``[a, b)``.
            * If ``list``: A random value will be picked per image from the
              ``list``.
            * If ``StochasticParameter``: The parameter will be queried once
              per batch for ``(N,)`` values, where ``N`` is the number of
              images.

    saturation : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Saturation multiplier.
        See :func:`~imgaug.augmenters.artistic.stylize_cartoon` for details.

            * If ``number``: That value will be used for all images.
            * If ``tuple (a, b) of number``: A random value will be uniformly
              sampled per image from the interval ``[a, b)``.
            * If ``list``: A random value will be picked per image from the
              ``list``.
            * If ``StochasticParameter``: The parameter will be queried once
              per batch for ``(N,)`` values, where ``N`` is the number of
              images.

    edge_prevalence : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Multiplier for the prevalence of edges.
        See :func:`~imgaug.augmenters.artistic.stylize_cartoon` for details.

            * If ``number``: That value will be used for all images.
            * If ``tuple (a, b) of number``: A random value will be uniformly
              sampled per image from the interval ``[a, b)``.
            * If ``list``: A random value will be picked per image from the
              ``list``.
            * If ``StochasticParameter``: The parameter will be queried once
              per batch for ``(N,)`` values, where ``N`` is the number of
              images.

    from_colorspace : str, optional
        The source colorspace. Use one of ``imgaug.augmenters.color.CSPACE_*``.
        Defaults to ``RGB``.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.Cartoon()

    Create an example image, then apply a cartoon filter to it.

    >>> aug = iaa.Cartoon(blur_ksize=3, segmentation_size=1.0,
    >>>                   saturation=2.0, edge_prevalence=1.0)

    Create a non-stochastic cartoon augmenter that produces decent-looking
    images.

    )r   r   )g?g333333?)g      ?g      @)g?g?N
deprecatedc
           
         sz   t t| j||||	d tj|ddddd| _tj|ddddd| _tj|ddddd| _tj|d	dddd| _|| _	d S )
N)seednamerandom_statedeterministicr0   )r   NT)Zvalue_rangeZtuple_to_uniformZlist_to_choicer1   )r   Nr2   r3   )
superrT   __init__iapZhandle_continuous_paramr0   r1   r2   r3   r4   )
selfr0   r1   r2   r3   r4   rV   rW   rX   rY   	__class__r6   r7   r[   h  s*   

zCartoon.__init__c              	   C   sh   |j d ur2| ||}t|j D ]!\}}t||d | |d | |d | |d | | jd|d< q|S )Nr   r   r	   r   r0   r1   r2   r3   r4   .)Zimages_draw_samples	enumerater8   r4   )r]   batchrX   parentshooksZsamplesir/   r6   r6   r7   _augment_batch_  s   




zCartoon._augment_batch_c                 C   sJ   |j }| jj|f|d| jj|f|d| jj|f|d| jj|f|dfS )N)rX   )nb_rowsr0   Zdraw_samplesr1   r2   r3   )r]   rc   rX   rh   r6   r6   r7   ra     s   

zCartoon._draw_samplesc                 C   s   | j | j| j| j| jgS )z=See :func:`~imgaug.augmenters.meta.Augmenter.get_parameters`.r`   )r]   r6   r6   r7   get_parameters  s   zCartoon.get_parameters)__name__
__module____qualname____doc__r=   
CSPACE_RGBr[   rg   ra   ri   __classcell__r6   r6   r^   r7   rT      s    krT   )rm   
__future__r   r   r   numpyr%   r(   Zimgaug.imgaugr    r   r   r=   r
   r    r   r\   rn   r8   r)   r*   r'   rK   r,   r-   r.   Z	AugmenterrT   r6   r6   r6   r7   <module>   s.    
 		