o
    :ήc)                     @   s   d dl 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mZmZ g dZd	Zd
d e	j D Zdd Zdd Z		dd ddddZ			 dddZdddZdS )    N   )_supported_float_typewarn)img_as_float   )
rgb_colors)gray2rgbrgb2hsvhsv2rgb)
color_dict	label2rgbDEFAULT_COLORS)
redblueyellowmagentagreenindigo
darkorangecyanpinkyellowgreenc                 C   s    i | ]\}}t |tr||qS  )
isinstancetuple).0kvr   r   ?/tmp/pip-target-vg8gfxp4/lib/python/skimage/color/colorlabel.py
<dictcomp>   s    r   c                 C   s$   t | tr	t|  } t| dd S )aA  Return RGB color as (1, 3) array.

    This RGB array gets multiplied by masked regions of an RGB image, which are
    partially flattened by masking (i.e. dimensions 2D + RGB -> 1D + RGB).

    Parameters
    ----------
    color : str or array
        Color name in `color_dict` or RGB float values between [0, 1].
    N   )r   strr   nparray)colorr   r   r   _rgb_vector   s   
r%   c           	      C   s   |du rd}t |}tj| dd\}}|| j|k }t|dkr6|d }|||k   d7  < d|| j|k< n|d7 }t|}t|g|}||fS )zReturn `unique_labels` and `color_cycle` for label array and color list.

    Colors are cycled for normal labels, but the background color should only
    be used for the background.
    Nr   r   r   T)return_inverser   r   )r%   r"   uniqueflatlen	itertoolscyclechain)	labelcolorsbg_labelbg_colorunique_labelsmapped_labelsbg_label_rank_listbg_label_rankcolor_cycler   r   r   _match_label_with_color'   s   
r7   333333?r&   overlay)
saturationchannel_axisc             	   C   sh   |durt j||	dd}|dkrt| |||||||}
n|dkr(t| |||}
ntdt j|
d|	dS )a  Return an RGB image where color-coded labels are painted over the image.

    Parameters
    ----------
    label : ndarray
        Integer array of labels with the same shape as `image`.
    image : ndarray, optional
        Image used as underlay for labels. It should have the same shape as
        `labels`, optionally with an additional RGB (channels) axis. If `image`
        is an RGB image, it is converted to grayscale before coloring.
    colors : list, optional
        List of colors. If the number of labels exceeds the number of colors,
        then the colors are cycled.
    alpha : float [0, 1], optional
        Opacity of colorized labels. Ignored if image is `None`.
    bg_label : int, optional
        Label that's treated as the background. If `bg_label` is specified,
        `bg_color` is `None`, and `kind` is `overlay`,
        background is not painted by any colors.
    bg_color : str or array, optional
        Background color. Must be a name in `color_dict` or RGB float values
        between [0, 1].
    image_alpha : float [0, 1], optional
        Opacity of the image.
    kind : string, one of {'overlay', 'avg'}
        The kind of color image desired. 'overlay' cycles over defined colors
        and overlays the colored labels over the original image. 'avg' replaces
        each labeled segment with its average color, for a stained-class or
        pastel painting appearance.
    saturation : float [0, 1], optional
        Parameter to control the saturation applied to the original image
        between fully saturated (original RGB, `saturation=1`) and fully
        unsaturated (grayscale, `saturation=0`). Only applies when
        `kind='overlay'`.
    channel_axis : int, optional
        This parameter indicates which axis of the output array will correspond
        to channels. If `image` is provided, this must also match the axis of
        `image` that corresponds to channels.

        .. versionadded:: 0.19
           ``channel_axis`` was added in 0.19.

    Returns
    -------
    result : ndarray of float, same shape as `image`
        The result of blending a cycling colormap (`colors`) for each distinct
        value in `label` with the image, at a certain alpha value.
    Nr:   )sourcedestinationr9   avgz)`kind` must be either 'overlay' or 'avg'.)r"   moveaxis_label2rgb_overlay_label2rgb_avg
ValueError)r.   imager/   alphar0   r1   image_alphakindr;   r<   rgbr   r   r   r   J   s   3r   c                 C   s(  d|  kr
dksn t d|  |du rt}dd |D }|du r1tj| jd tjd}d}nk|jd| j | jksD|j| jd krHtd	|j| jd kr[|jd
 dkr[td| dk ret d t	|j
}t|j|dd}|j| jkrt|}	|	d  |9  < t|	}n
|j| jkrt|}|| d|  }t|  |}
|
dkr| |
 } ||
8 }tt|  }|tkrtj}| |} t| |||\}}t|dkr|S tt|d }tdd t||D }| }||_|| | |d|   }d|v o|du }|r|| |k || |k< |S )a  Return an RGB image where color-coded labels are painted over the image.

    Parameters
    ----------
    label : ndarray
        Integer array of labels with the same shape as `image`.
    image : ndarray, optional
        Image used as underlay for labels. It should have the same shape as
        `labels`, optionally with an additional RGB (channels) axis. If `image`
        is an RGB image, it is converted to grayscale before coloring.
    colors : list, optional
        List of colors. If the number of labels exceeds the number of colors,
        then the colors are cycled.
    alpha : float [0, 1], optional
        Opacity of colorized labels. Ignored if image is `None`.
    bg_label : int, optional
        Label that's treated as the background. If `bg_label` is specified and
        `bg_color` is `None`, background is not painted by any colors.
    bg_color : str or array, optional
        Background color. Must be a name in `color_dict` or RGB float values
        between [0, 1].
    image_alpha : float [0, 1], optional
        Opacity of the image.
    saturation : float [0, 1], optional
        Parameter to control the saturation applied to the original image
        between fully saturated (original RGB, `saturation=1`) and fully
        unsaturated (grayscale, `saturation=0`).

    Returns
    -------
    result : ndarray of float, same shape as `image`
        The result of blending a cycling colormap (`colors`) for each distinct
        value in `label` with the image, at a certain alpha value.
    r   r   z(saturation must be in range [0, 1], got Nc                 S   s   g | ]}t |qS r   )r%   )r   cr   r   r   
<listcomp>       z&_label2rgb_overlay.<locals>.<listcomp>r    dtypez*`image` and `label` must be the same shaper:   r    z0`image` must be RGB (image.shape[-1] must be 3).z1Negative intensities in `image` are not supportedF)copy).r   c                 S   s   g | ]\}}|qS r   r   )r   irI   r   r   r   rJ      rK   )r   r   r"   zerosshapefloat64ndimrC   minr   rN   r   astyper	   r
   r   min_scalar_typeintmaxbooluint8r7   r*   rangestackzipr)   )r.   rD   r/   rE   r0   r1   rF   r;   float_dtypehsvoffsetnew_typemapped_labels_flatr6   dense_labelslabel_to_colorr3   resultremove_backgroundr   r   r   rA      sd   %


rA   c           
      C   s   t j| jd |jd}t | }||k}| r(|||k }| |k }|||< |D ]}| |k }|| jdd}	|	||< q*|S )aC  Visualise each segment in `label_field` with its mean color in `image`.

    Parameters
    ----------
    label_field : ndarray of int
        A segmentation of an image.
    image : array, shape ``label_field.shape + (3,)``
        A color image of the same spatial shape as `label_field`.
    bg_label : int, optional
        A value in `label_field` to be treated as background.
    bg_color : 3-tuple of int, optional
        The color for the background label

    Returns
    -------
    out : ndarray, same shape and type as `image`
        The output visualization.
    rL   rM   r   )axis)r"   rQ   rR   rN   r(   anynonzeromean)
label_fieldrD   r0   r1   outlabelsbgmaskr.   r$   r   r   r   rB      s   

rB   )NNr8   r   r&   r   r9   )NNr8   r:   Nr   r   )r   r&   )r+   numpyr"   _shared.utilsr   r   utilr    r   	colorconvr   r	   r
   __all__r   __dict__itemsr   r%   r7   r   rA   rB   r   r   r   r   <module>   s*    #?
j