We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
DOCS: Return dtype of _label2rgb_avg function
## Description

With skimage release 0.18.0, and in particular after PR #4840, there is a mismatch between the return dtype of function `_label2rgb_avg` in the `color/colorlabel` module: it is stated to be the same dtype as the image, but using `np.zeros` instead of `np.zeros_like` makes the return type always to be float


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `skimage/color/colorlabel.py`
Content:
```
1 import itertools
2 
3 import numpy as np
4 
5 from .._shared.utils import warn, change_default_value
6 from ..util import img_as_float
7 from . import rgb_colors
8 from .colorconv import rgb2gray, gray2rgb
9 
10 
11 __all__ = ['color_dict', 'label2rgb', 'DEFAULT_COLORS']
12 
13 
14 DEFAULT_COLORS = ('red', 'blue', 'yellow', 'magenta', 'green',
15                   'indigo', 'darkorange', 'cyan', 'pink', 'yellowgreen')
16 
17 
18 color_dict = {k: v for k, v in rgb_colors.__dict__.items()
19               if isinstance(v, tuple)}
20 
21 
22 def _rgb_vector(color):
23     """Return RGB color as (1, 3) array.
24 
25     This RGB array gets multiplied by masked regions of an RGB image, which are
26     partially flattened by masking (i.e. dimensions 2D + RGB -> 1D + RGB).
27 
28     Parameters
29     ----------
30     color : str or array
31         Color name in `color_dict` or RGB float values between [0, 1].
32     """
33     if isinstance(color, str):
34         color = color_dict[color]
35     # Slice to handle RGBA colors.
36     return np.array(color[:3])
37 
38 
39 def _match_label_with_color(label, colors, bg_label, bg_color):
40     """Return `unique_labels` and `color_cycle` for label array and color list.
41 
42     Colors are cycled for normal labels, but the background color should only
43     be used for the background.
44     """
45     # Temporarily set background color; it will be removed later.
46     if bg_color is None:
47         bg_color = (0, 0, 0)
48     bg_color = _rgb_vector(bg_color)
49 
50     # map labels to their ranks among all labels from small to large
51     unique_labels, mapped_labels = np.unique(label, return_inverse=True)
52 
53     # get rank of bg_label
54     bg_label_rank_list = mapped_labels[label.flat == bg_label]
55 
56     # The rank of each label is the index of the color it is matched to in
57     # color cycle. bg_label should always be mapped to the first color, so
58     # its rank must be 0. Other labels should be ranked from small to large
59     # from 1.
60     if len(bg_label_rank_list) > 0:
61         bg_label_rank = bg_label_rank_list[0]
62         mapped_labels[mapped_labels < bg_label_rank] += 1
63         mapped_labels[label.flat == bg_label] = 0
64     else:
65         mapped_labels += 1
66 
67     # Modify labels and color cycle so background color is used only once.
68     color_cycle = itertools.cycle(colors)
69     color_cycle = itertools.chain([bg_color], color_cycle)
70 
71     return mapped_labels, color_cycle
72 
73 
74 @change_default_value("bg_label", new_value=0, changed_version="0.19")
75 def label2rgb(label, image=None, colors=None, alpha=0.3,
76               bg_label=-1, bg_color=(0, 0, 0), image_alpha=1, kind='overlay'):
77     """Return an RGB image where color-coded labels are painted over the image.
78 
79     Parameters
80     ----------
81     label : array, shape (M, N)
82         Integer array of labels with the same shape as `image`.
83     image : array, shape (M, N, 3), optional
84         Image used as underlay for labels. If the input is an RGB image, it's
85         converted to grayscale before coloring.
86     colors : list, optional
87         List of colors. If the number of labels exceeds the number of colors,
88         then the colors are cycled.
89     alpha : float [0, 1], optional
90         Opacity of colorized labels. Ignored if image is `None`.
91     bg_label : int, optional
92         Label that's treated as the background. If `bg_label` is specified,
93         `bg_color` is `None`, and `kind` is `overlay`,
94         background is not painted by any colors.
95     bg_color : str or array, optional
96         Background color. Must be a name in `color_dict` or RGB float values
97         between [0, 1].
98     image_alpha : float [0, 1], optional
99         Opacity of the image.
100     kind : string, one of {'overlay', 'avg'}
101         The kind of color image desired. 'overlay' cycles over defined colors
102         and overlays the colored labels over the original image. 'avg' replaces
103         each labeled segment with its average color, for a stained-class or
104         pastel painting appearance.
105 
106     Returns
107     -------
108     result : array of float, shape (M, N, 3)
109         The result of blending a cycling colormap (`colors`) for each distinct
110         value in `label` with the image, at a certain alpha value.
111     """
112     if kind == 'overlay':
113         return _label2rgb_overlay(label, image, colors, alpha, bg_label,
114                                   bg_color, image_alpha)
115     elif kind == 'avg':
116         return _label2rgb_avg(label, image, bg_label, bg_color)
117     else:
118         raise ValueError("`kind` must be either 'overlay' or 'avg'.")
119 
120 
121 def _label2rgb_overlay(label, image=None, colors=None, alpha=0.3,
122                        bg_label=-1, bg_color=None, image_alpha=1):
123     """Return an RGB image where color-coded labels are painted over the image.
124 
125     Parameters
126     ----------
127     label : array, shape (M, N)
128         Integer array of labels with the same shape as `image`.
129     image : array, shape (M, N, 3), optional
130         Image used as underlay for labels. If the input is an RGB image, it's
131         converted to grayscale before coloring.
132     colors : list, optional
133         List of colors. If the number of labels exceeds the number of colors,
134         then the colors are cycled.
135     alpha : float [0, 1], optional
136         Opacity of colorized labels. Ignored if image is `None`.
137     bg_label : int, optional
138         Label that's treated as the background. If `bg_label` is specified and
139         `bg_color` is `None`, background is not painted by any colors.
140     bg_color : str or array, optional
141         Background color. Must be a name in `color_dict` or RGB float values
142         between [0, 1].
143     image_alpha : float [0, 1], optional
144         Opacity of the image.
145 
146     Returns
147     -------
148     result : array of float, shape (M, N, 3)
149         The result of blending a cycling colormap (`colors`) for each distinct
150         value in `label` with the image, at a certain alpha value.
151     """
152     if colors is None:
153         colors = DEFAULT_COLORS
154     colors = [_rgb_vector(c) for c in colors]
155 
156     if image is None:
157         image = np.zeros(label.shape + (3,), dtype=np.float64)
158         # Opacity doesn't make sense if no image exists.
159         alpha = 1
160     else:
161         if not image.shape[:2] == label.shape:
162             raise ValueError("`image` and `label` must be the same shape")
163 
164         if image.min() < 0:
165             warn("Negative intensities in `image` are not supported")
166 
167         if image.ndim > label.ndim:
168             image = img_as_float(rgb2gray(image))
169         else:
170             image = img_as_float(image)
171         image = gray2rgb(image) * image_alpha + (1 - image_alpha)
172 
173     # Ensure that all labels are non-negative so we can index into
174     # `label_to_color` correctly.
175     offset = min(label.min(), bg_label)
176     if offset != 0:
177         label = label - offset  # Make sure you don't modify the input array.
178         bg_label -= offset
179 
180     new_type = np.min_scalar_type(int(label.max()))
181     if new_type == bool:
182         new_type = np.uint8
183     label = label.astype(new_type)
184 
185     mapped_labels_flat, color_cycle = _match_label_with_color(label, colors,
186                                                               bg_label, bg_color)
187 
188     if len(mapped_labels_flat) == 0:
189         return image
190 
191     dense_labels = range(np.max(mapped_labels_flat) + 1)
192 
193     label_to_color = np.stack([c for i, c in zip(dense_labels, color_cycle)])
194 
195     mapped_labels = label
196     mapped_labels.flat = mapped_labels_flat
197     result = label_to_color[mapped_labels] * alpha + image * (1 - alpha)
198 
199     # Remove background label if its color was not specified.
200     remove_background = 0 in mapped_labels_flat and bg_color is None
201     if remove_background:
202         result[label == bg_label] = image[label == bg_label]
203 
204     return result
205 
206 
207 def _label2rgb_avg(label_field, image, bg_label=0, bg_color=(0, 0, 0)):
208     """Visualise each segment in `label_field` with its mean color in `image`.
209 
210     Parameters
211     ----------
212     label_field : array of int
213         A segmentation of an image.
214     image : array, shape ``label_field.shape + (3,)``
215         A color image of the same spatial shape as `label_field`.
216     bg_label : int, optional
217         A value in `label_field` to be treated as background.
218     bg_color : 3-tuple of int, optional
219         The color for the background label
220 
221     Returns
222     -------
223     out : array, same shape and type as `image`
224         The output visualization.
225     """
226     out = np.zeros(label_field.shape + (3,))
227     labels = np.unique(label_field)
228     bg = (labels == bg_label)
229     if bg.any():
230         labels = labels[labels != bg_label]
231         mask = (label_field == bg_label).nonzero()
232         out[mask] = bg_color
233     for label in labels:
234         mask = (label_field == label).nonzero()
235         color = image[mask].mean(axis=0)
236         out[mask] = color
237     return out
238
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/skimage/color/colorlabel.py b/skimage/color/colorlabel.py
--- a/skimage/color/colorlabel.py
+++ b/skimage/color/colorlabel.py
@@ -223,7 +223,7 @@
     out : array, same shape and type as `image`
         The output visualization.
     """
-    out = np.zeros(label_field.shape + (3,))
+    out = np.zeros(label_field.shape + (3,), dtype=image.dtype)
     labels = np.unique(label_field)
     bg = (labels == bg_label)
     if bg.any():

{"golden_diff": "diff --git a/skimage/color/colorlabel.py b/skimage/color/colorlabel.py\n--- a/skimage/color/colorlabel.py\n+++ b/skimage/color/colorlabel.py\n@@ -223,7 +223,7 @@\n     out : array, same shape and type as `image`\n         The output visualization.\n     \"\"\"\n-    out = np.zeros(label_field.shape + (3,))\n+    out = np.zeros(label_field.shape + (3,), dtype=image.dtype)\n     labels = np.unique(label_field)\n     bg = (labels == bg_label)\n     if bg.any():\n", "issue": "DOCS: Return dtype of _label2rgb_avg function\n## Description\r\n\r\nWith skimage release 0.18.0, and in particular after PR #4840, there is a mismatch between the return dtype of function `_label2rgb_avg` in the `color/colorlabel` module: it is stated to be the same dtype as the image, but using `np.zeros` instead of `np.zeros_like` makes the return type always to be float\r\n\n", "before_files": [{"content": "import itertools\n\nimport numpy as np\n\nfrom .._shared.utils import warn, change_default_value\nfrom ..util import img_as_float\nfrom . import rgb_colors\nfrom .colorconv import rgb2gray, gray2rgb\n\n\n__all__ = ['color_dict', 'label2rgb', 'DEFAULT_COLORS']\n\n\nDEFAULT_COLORS = ('red', 'blue', 'yellow', 'magenta', 'green',\n                  'indigo', 'darkorange', 'cyan', 'pink', 'yellowgreen')\n\n\ncolor_dict = {k: v for k, v in rgb_colors.__dict__.items()\n              if isinstance(v, tuple)}\n\n\ndef _rgb_vector(color):\n    \"\"\"Return RGB color as (1, 3) array.\n\n    This RGB array gets multiplied by masked regions of an RGB image, which are\n    partially flattened by masking (i.e. dimensions 2D + RGB -> 1D + RGB).\n\n    Parameters\n    ----------\n    color : str or array\n        Color name in `color_dict` or RGB float values between [0, 1].\n    \"\"\"\n    if isinstance(color, str):\n        color = color_dict[color]\n    # Slice to handle RGBA colors.\n    return np.array(color[:3])\n\n\ndef _match_label_with_color(label, colors, bg_label, bg_color):\n    \"\"\"Return `unique_labels` and `color_cycle` for label array and color list.\n\n    Colors are cycled for normal labels, but the background color should only\n    be used for the background.\n    \"\"\"\n    # Temporarily set background color; it will be removed later.\n    if bg_color is None:\n        bg_color = (0, 0, 0)\n    bg_color = _rgb_vector(bg_color)\n\n    # map labels to their ranks among all labels from small to large\n    unique_labels, mapped_labels = np.unique(label, return_inverse=True)\n\n    # get rank of bg_label\n    bg_label_rank_list = mapped_labels[label.flat == bg_label]\n\n    # The rank of each label is the index of the color it is matched to in\n    # color cycle. bg_label should always be mapped to the first color, so\n    # its rank must be 0. Other labels should be ranked from small to large\n    # from 1.\n    if len(bg_label_rank_list) > 0:\n        bg_label_rank = bg_label_rank_list[0]\n        mapped_labels[mapped_labels < bg_label_rank] += 1\n        mapped_labels[label.flat == bg_label] = 0\n    else:\n        mapped_labels += 1\n\n    # Modify labels and color cycle so background color is used only once.\n    color_cycle = itertools.cycle(colors)\n    color_cycle = itertools.chain([bg_color], color_cycle)\n\n    return mapped_labels, color_cycle\n\n\n@change_default_value(\"bg_label\", new_value=0, changed_version=\"0.19\")\ndef label2rgb(label, image=None, colors=None, alpha=0.3,\n              bg_label=-1, bg_color=(0, 0, 0), image_alpha=1, kind='overlay'):\n    \"\"\"Return an RGB image where color-coded labels are painted over the image.\n\n    Parameters\n    ----------\n    label : array, shape (M, N)\n        Integer array of labels with the same shape as `image`.\n    image : array, shape (M, N, 3), optional\n        Image used as underlay for labels. If the input is an RGB image, it's\n        converted to grayscale before coloring.\n    colors : list, optional\n        List of colors. If the number of labels exceeds the number of colors,\n        then the colors are cycled.\n    alpha : float [0, 1], optional\n        Opacity of colorized labels. Ignored if image is `None`.\n    bg_label : int, optional\n        Label that's treated as the background. If `bg_label` is specified,\n        `bg_color` is `None`, and `kind` is `overlay`,\n        background is not painted by any colors.\n    bg_color : str or array, optional\n        Background color. Must be a name in `color_dict` or RGB float values\n        between [0, 1].\n    image_alpha : float [0, 1], optional\n        Opacity of the image.\n    kind : string, one of {'overlay', 'avg'}\n        The kind of color image desired. 'overlay' cycles over defined colors\n        and overlays the colored labels over the original image. 'avg' replaces\n        each labeled segment with its average color, for a stained-class or\n        pastel painting appearance.\n\n    Returns\n    -------\n    result : array of float, shape (M, N, 3)\n        The result of blending a cycling colormap (`colors`) for each distinct\n        value in `label` with the image, at a certain alpha value.\n    \"\"\"\n    if kind == 'overlay':\n        return _label2rgb_overlay(label, image, colors, alpha, bg_label,\n                                  bg_color, image_alpha)\n    elif kind == 'avg':\n        return _label2rgb_avg(label, image, bg_label, bg_color)\n    else:\n        raise ValueError(\"`kind` must be either 'overlay' or 'avg'.\")\n\n\ndef _label2rgb_overlay(label, image=None, colors=None, alpha=0.3,\n                       bg_label=-1, bg_color=None, image_alpha=1):\n    \"\"\"Return an RGB image where color-coded labels are painted over the image.\n\n    Parameters\n    ----------\n    label : array, shape (M, N)\n        Integer array of labels with the same shape as `image`.\n    image : array, shape (M, N, 3), optional\n        Image used as underlay for labels. If the input is an RGB image, it's\n        converted to grayscale before coloring.\n    colors : list, optional\n        List of colors. If the number of labels exceeds the number of colors,\n        then the colors are cycled.\n    alpha : float [0, 1], optional\n        Opacity of colorized labels. Ignored if image is `None`.\n    bg_label : int, optional\n        Label that's treated as the background. If `bg_label` is specified and\n        `bg_color` is `None`, background is not painted by any colors.\n    bg_color : str or array, optional\n        Background color. Must be a name in `color_dict` or RGB float values\n        between [0, 1].\n    image_alpha : float [0, 1], optional\n        Opacity of the image.\n\n    Returns\n    -------\n    result : array of float, shape (M, N, 3)\n        The result of blending a cycling colormap (`colors`) for each distinct\n        value in `label` with the image, at a certain alpha value.\n    \"\"\"\n    if colors is None:\n        colors = DEFAULT_COLORS\n    colors = [_rgb_vector(c) for c in colors]\n\n    if image is None:\n        image = np.zeros(label.shape + (3,), dtype=np.float64)\n        # Opacity doesn't make sense if no image exists.\n        alpha = 1\n    else:\n        if not image.shape[:2] == label.shape:\n            raise ValueError(\"`image` and `label` must be the same shape\")\n\n        if image.min() < 0:\n            warn(\"Negative intensities in `image` are not supported\")\n\n        if image.ndim > label.ndim:\n            image = img_as_float(rgb2gray(image))\n        else:\n            image = img_as_float(image)\n        image = gray2rgb(image) * image_alpha + (1 - image_alpha)\n\n    # Ensure that all labels are non-negative so we can index into\n    # `label_to_color` correctly.\n    offset = min(label.min(), bg_label)\n    if offset != 0:\n        label = label - offset  # Make sure you don't modify the input array.\n        bg_label -= offset\n\n    new_type = np.min_scalar_type(int(label.max()))\n    if new_type == bool:\n        new_type = np.uint8\n    label = label.astype(new_type)\n\n    mapped_labels_flat, color_cycle = _match_label_with_color(label, colors,\n                                                              bg_label, bg_color)\n\n    if len(mapped_labels_flat) == 0:\n        return image\n\n    dense_labels = range(np.max(mapped_labels_flat) + 1)\n\n    label_to_color = np.stack([c for i, c in zip(dense_labels, color_cycle)])\n\n    mapped_labels = label\n    mapped_labels.flat = mapped_labels_flat\n    result = label_to_color[mapped_labels] * alpha + image * (1 - alpha)\n\n    # Remove background label if its color was not specified.\n    remove_background = 0 in mapped_labels_flat and bg_color is None\n    if remove_background:\n        result[label == bg_label] = image[label == bg_label]\n\n    return result\n\n\ndef _label2rgb_avg(label_field, image, bg_label=0, bg_color=(0, 0, 0)):\n    \"\"\"Visualise each segment in `label_field` with its mean color in `image`.\n\n    Parameters\n    ----------\n    label_field : array of int\n        A segmentation of an image.\n    image : array, shape ``label_field.shape + (3,)``\n        A color image of the same spatial shape as `label_field`.\n    bg_label : int, optional\n        A value in `label_field` to be treated as background.\n    bg_color : 3-tuple of int, optional\n        The color for the background label\n\n    Returns\n    -------\n    out : array, same shape and type as `image`\n        The output visualization.\n    \"\"\"\n    out = np.zeros(label_field.shape + (3,))\n    labels = np.unique(label_field)\n    bg = (labels == bg_label)\n    if bg.any():\n        labels = labels[labels != bg_label]\n        mask = (label_field == bg_label).nonzero()\n        out[mask] = bg_color\n    for label in labels:\n        mask = (label_field == label).nonzero()\n        color = image[mask].mean(axis=0)\n        out[mask] = color\n    return out\n", "path": "skimage/color/colorlabel.py"}], "after_files": [{"content": "import itertools\n\nimport numpy as np\n\nfrom .._shared.utils import warn, change_default_value\nfrom ..util import img_as_float\nfrom . import rgb_colors\nfrom .colorconv import rgb2gray, gray2rgb\n\n\n__all__ = ['color_dict', 'label2rgb', 'DEFAULT_COLORS']\n\n\nDEFAULT_COLORS = ('red', 'blue', 'yellow', 'magenta', 'green',\n                  'indigo', 'darkorange', 'cyan', 'pink', 'yellowgreen')\n\n\ncolor_dict = {k: v for k, v in rgb_colors.__dict__.items()\n              if isinstance(v, tuple)}\n\n\ndef _rgb_vector(color):\n    \"\"\"Return RGB color as (1, 3) array.\n\n    This RGB array gets multiplied by masked regions of an RGB image, which are\n    partially flattened by masking (i.e. dimensions 2D + RGB -> 1D + RGB).\n\n    Parameters\n    ----------\n    color : str or array\n        Color name in `color_dict` or RGB float values between [0, 1].\n    \"\"\"\n    if isinstance(color, str):\n        color = color_dict[color]\n    # Slice to handle RGBA colors.\n    return np.array(color[:3])\n\n\ndef _match_label_with_color(label, colors, bg_label, bg_color):\n    \"\"\"Return `unique_labels` and `color_cycle` for label array and color list.\n\n    Colors are cycled for normal labels, but the background color should only\n    be used for the background.\n    \"\"\"\n    # Temporarily set background color; it will be removed later.\n    if bg_color is None:\n        bg_color = (0, 0, 0)\n    bg_color = _rgb_vector(bg_color)\n\n    # map labels to their ranks among all labels from small to large\n    unique_labels, mapped_labels = np.unique(label, return_inverse=True)\n\n    # get rank of bg_label\n    bg_label_rank_list = mapped_labels[label.flat == bg_label]\n\n    # The rank of each label is the index of the color it is matched to in\n    # color cycle. bg_label should always be mapped to the first color, so\n    # its rank must be 0. Other labels should be ranked from small to large\n    # from 1.\n    if len(bg_label_rank_list) > 0:\n        bg_label_rank = bg_label_rank_list[0]\n        mapped_labels[mapped_labels < bg_label_rank] += 1\n        mapped_labels[label.flat == bg_label] = 0\n    else:\n        mapped_labels += 1\n\n    # Modify labels and color cycle so background color is used only once.\n    color_cycle = itertools.cycle(colors)\n    color_cycle = itertools.chain([bg_color], color_cycle)\n\n    return mapped_labels, color_cycle\n\n\n@change_default_value(\"bg_label\", new_value=0, changed_version=\"0.19\")\ndef label2rgb(label, image=None, colors=None, alpha=0.3,\n              bg_label=-1, bg_color=(0, 0, 0), image_alpha=1, kind='overlay'):\n    \"\"\"Return an RGB image where color-coded labels are painted over the image.\n\n    Parameters\n    ----------\n    label : array, shape (M, N)\n        Integer array of labels with the same shape as `image`.\n    image : array, shape (M, N, 3), optional\n        Image used as underlay for labels. If the input is an RGB image, it's\n        converted to grayscale before coloring.\n    colors : list, optional\n        List of colors. If the number of labels exceeds the number of colors,\n        then the colors are cycled.\n    alpha : float [0, 1], optional\n        Opacity of colorized labels. Ignored if image is `None`.\n    bg_label : int, optional\n        Label that's treated as the background. If `bg_label` is specified,\n        `bg_color` is `None`, and `kind` is `overlay`,\n        background is not painted by any colors.\n    bg_color : str or array, optional\n        Background color. Must be a name in `color_dict` or RGB float values\n        between [0, 1].\n    image_alpha : float [0, 1], optional\n        Opacity of the image.\n    kind : string, one of {'overlay', 'avg'}\n        The kind of color image desired. 'overlay' cycles over defined colors\n        and overlays the colored labels over the original image. 'avg' replaces\n        each labeled segment with its average color, for a stained-class or\n        pastel painting appearance.\n\n    Returns\n    -------\n    result : array of float, shape (M, N, 3)\n        The result of blending a cycling colormap (`colors`) for each distinct\n        value in `label` with the image, at a certain alpha value.\n    \"\"\"\n    if kind == 'overlay':\n        return _label2rgb_overlay(label, image, colors, alpha, bg_label,\n                                  bg_color, image_alpha)\n    elif kind == 'avg':\n        return _label2rgb_avg(label, image, bg_label, bg_color)\n    else:\n        raise ValueError(\"`kind` must be either 'overlay' or 'avg'.\")\n\n\ndef _label2rgb_overlay(label, image=None, colors=None, alpha=0.3,\n                       bg_label=-1, bg_color=None, image_alpha=1):\n    \"\"\"Return an RGB image where color-coded labels are painted over the image.\n\n    Parameters\n    ----------\n    label : array, shape (M, N)\n        Integer array of labels with the same shape as `image`.\n    image : array, shape (M, N, 3), optional\n        Image used as underlay for labels. If the input is an RGB image, it's\n        converted to grayscale before coloring.\n    colors : list, optional\n        List of colors. If the number of labels exceeds the number of colors,\n        then the colors are cycled.\n    alpha : float [0, 1], optional\n        Opacity of colorized labels. Ignored if image is `None`.\n    bg_label : int, optional\n        Label that's treated as the background. If `bg_label` is specified and\n        `bg_color` is `None`, background is not painted by any colors.\n    bg_color : str or array, optional\n        Background color. Must be a name in `color_dict` or RGB float values\n        between [0, 1].\n    image_alpha : float [0, 1], optional\n        Opacity of the image.\n\n    Returns\n    -------\n    result : array of float, shape (M, N, 3)\n        The result of blending a cycling colormap (`colors`) for each distinct\n        value in `label` with the image, at a certain alpha value.\n    \"\"\"\n    if colors is None:\n        colors = DEFAULT_COLORS\n    colors = [_rgb_vector(c) for c in colors]\n\n    if image is None:\n        image = np.zeros(label.shape + (3,), dtype=np.float64)\n        # Opacity doesn't make sense if no image exists.\n        alpha = 1\n    else:\n        if not image.shape[:2] == label.shape:\n            raise ValueError(\"`image` and `label` must be the same shape\")\n\n        if image.min() < 0:\n            warn(\"Negative intensities in `image` are not supported\")\n\n        if image.ndim > label.ndim:\n            image = img_as_float(rgb2gray(image))\n        else:\n            image = img_as_float(image)\n        image = gray2rgb(image) * image_alpha + (1 - image_alpha)\n\n    # Ensure that all labels are non-negative so we can index into\n    # `label_to_color` correctly.\n    offset = min(label.min(), bg_label)\n    if offset != 0:\n        label = label - offset  # Make sure you don't modify the input array.\n        bg_label -= offset\n\n    new_type = np.min_scalar_type(int(label.max()))\n    if new_type == bool:\n        new_type = np.uint8\n    label = label.astype(new_type)\n\n    mapped_labels_flat, color_cycle = _match_label_with_color(label, colors,\n                                                              bg_label, bg_color)\n\n    if len(mapped_labels_flat) == 0:\n        return image\n\n    dense_labels = range(np.max(mapped_labels_flat) + 1)\n\n    label_to_color = np.stack([c for i, c in zip(dense_labels, color_cycle)])\n\n    mapped_labels = label\n    mapped_labels.flat = mapped_labels_flat\n    result = label_to_color[mapped_labels] * alpha + image * (1 - alpha)\n\n    # Remove background label if its color was not specified.\n    remove_background = 0 in mapped_labels_flat and bg_color is None\n    if remove_background:\n        result[label == bg_label] = image[label == bg_label]\n\n    return result\n\n\ndef _label2rgb_avg(label_field, image, bg_label=0, bg_color=(0, 0, 0)):\n    \"\"\"Visualise each segment in `label_field` with its mean color in `image`.\n\n    Parameters\n    ----------\n    label_field : array of int\n        A segmentation of an image.\n    image : array, shape ``label_field.shape + (3,)``\n        A color image of the same spatial shape as `label_field`.\n    bg_label : int, optional\n        A value in `label_field` to be treated as background.\n    bg_color : 3-tuple of int, optional\n        The color for the background label\n\n    Returns\n    -------\n    out : array, same shape and type as `image`\n        The output visualization.\n    \"\"\"\n    out = np.zeros(label_field.shape + (3,), dtype=image.dtype)\n    labels = np.unique(label_field)\n    bg = (labels == bg_label)\n    if bg.any():\n        labels = labels[labels != bg_label]\n        mask = (label_field == bg_label).nonzero()\n        out[mask] = bg_color\n    for label in labels:\n        mask = (label_field == label).nonzero()\n        color = image[mask].mean(axis=0)\n        out[mask] = color\n    return out\n", "path": "skimage/color/colorlabel.py"}]}