We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
label2rgb index error when using average and background color
When using averaged label2rgb with bg_label the out image indexing fails with error:

```
Traceback (most recent call last):
  File "file.py", line 222, in generate
    overlay = color.label2rgb(domain, image=img, bg_label=0, bg_color=1.0, kind="avg")
  File "[...]/python3.6/site-packages/skimage/color/colorlabel.py", line 116, in label2rgb
    return _label2rgb_avg(label, image, bg_label, bg_color)
  File "[...]/python3.6/site-packages/skimage/color/colorlabel.py", line 225, in _label2rgb_avg
    out[bg] = bg_color
IndexError: boolean index did not match indexed array along dimension 0; dimension is 100 but corresponding boolean dimension is 2
```

--- 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
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 def label2rgb(label, image=None, colors=None, alpha=0.3,
75               bg_label=-1, bg_color=(0, 0, 0), image_alpha=1, kind='overlay'):
76     """Return an RGB image where color-coded labels are painted over the image.
77 
78     Parameters
79     ----------
80     label : array, shape (M, N)
81         Integer array of labels with the same shape as `image`.
82     image : array, shape (M, N, 3), optional
83         Image used as underlay for labels. If the input is an RGB image, it's
84         converted to grayscale before coloring.
85     colors : list, optional
86         List of colors. If the number of labels exceeds the number of colors,
87         then the colors are cycled.
88     alpha : float [0, 1], optional
89         Opacity of colorized labels. Ignored if image is `None`.
90     bg_label : int, optional
91         Label that's treated as the background.
92     bg_color : str or array, optional
93         Background color. Must be a name in `color_dict` or RGB float values
94         between [0, 1].
95     image_alpha : float [0, 1], optional
96         Opacity of the image.
97     kind : string, one of {'overlay', 'avg'}
98         The kind of color image desired. 'overlay' cycles over defined colors
99         and overlays the colored labels over the original image. 'avg' replaces
100         each labeled segment with its average color, for a stained-class or
101         pastel painting appearance.
102 
103     Returns
104     -------
105     result : array of float, shape (M, N, 3)
106         The result of blending a cycling colormap (`colors`) for each distinct
107         value in `label` with the image, at a certain alpha value.
108     """
109     if kind == 'overlay':
110         return _label2rgb_overlay(label, image, colors, alpha, bg_label,
111                                   bg_color, image_alpha)
112     else:
113         return _label2rgb_avg(label, image, bg_label, bg_color)
114 
115 
116 def _label2rgb_overlay(label, image=None, colors=None, alpha=0.3,
117                        bg_label=-1, bg_color=None, image_alpha=1):
118     """Return an RGB image where color-coded labels are painted over the image.
119 
120     Parameters
121     ----------
122     label : array, shape (M, N)
123         Integer array of labels with the same shape as `image`.
124     image : array, shape (M, N, 3), optional
125         Image used as underlay for labels. If the input is an RGB image, it's
126         converted to grayscale before coloring.
127     colors : list, optional
128         List of colors. If the number of labels exceeds the number of colors,
129         then the colors are cycled.
130     alpha : float [0, 1], optional
131         Opacity of colorized labels. Ignored if image is `None`.
132     bg_label : int, optional
133         Label that's treated as the background.
134     bg_color : str or array, optional
135         Background color. Must be a name in `color_dict` or RGB float values
136         between [0, 1].
137     image_alpha : float [0, 1], optional
138         Opacity of the image.
139 
140     Returns
141     -------
142     result : array of float, shape (M, N, 3)
143         The result of blending a cycling colormap (`colors`) for each distinct
144         value in `label` with the image, at a certain alpha value.
145     """
146     if colors is None:
147         colors = DEFAULT_COLORS
148     colors = [_rgb_vector(c) for c in colors]
149 
150     if image is None:
151         image = np.zeros(label.shape + (3,), dtype=np.float64)
152         # Opacity doesn't make sense if no image exists.
153         alpha = 1
154     else:
155         if not image.shape[:2] == label.shape:
156             raise ValueError("`image` and `label` must be the same shape")
157 
158         if image.min() < 0:
159             warn("Negative intensities in `image` are not supported")
160 
161         image = img_as_float(rgb2gray(image))
162         image = gray2rgb(image) * image_alpha + (1 - image_alpha)
163 
164     # Ensure that all labels are non-negative so we can index into
165     # `label_to_color` correctly.
166     offset = min(label.min(), bg_label)
167     if offset != 0:
168         label = label - offset  # Make sure you don't modify the input array.
169         bg_label -= offset
170 
171     new_type = np.min_scalar_type(int(label.max()))
172     if new_type == np.bool:
173         new_type = np.uint8
174     label = label.astype(new_type)
175 
176     mapped_labels_flat, color_cycle = _match_label_with_color(label, colors,
177                                                               bg_label, bg_color)
178 
179     if len(mapped_labels_flat) == 0:
180         return image
181 
182     dense_labels = range(max(mapped_labels_flat) + 1)
183 
184     label_to_color = np.array([c for i, c in zip(dense_labels, color_cycle)])
185 
186     mapped_labels = label
187     mapped_labels.flat = mapped_labels_flat
188     result = label_to_color[mapped_labels] * alpha + image * (1 - alpha)
189 
190     # Remove background label if its color was not specified.
191     remove_background = 0 in mapped_labels_flat and bg_color is None
192     if remove_background:
193         result[label == bg_label] = image[label == bg_label]
194 
195     return result
196 
197 
198 def _label2rgb_avg(label_field, image, bg_label=0, bg_color=(0, 0, 0)):
199     """Visualise each segment in `label_field` with its mean color in `image`.
200 
201     Parameters
202     ----------
203     label_field : array of int
204         A segmentation of an image.
205     image : array, shape ``label_field.shape + (3,)``
206         A color image of the same spatial shape as `label_field`.
207     bg_label : int, optional
208         A value in `label_field` to be treated as background.
209     bg_color : 3-tuple of int, optional
210         The color for the background label
211 
212     Returns
213     -------
214     out : array, same shape and type as `image`
215         The output visualization.
216     """
217     out = np.zeros_like(image)
218     labels = np.unique(label_field)
219     bg = (labels == bg_label)
220     if bg.any():
221         labels = labels[labels != bg_label]
222         out[bg] = bg_color
223     for label in labels:
224         mask = (label_field == label).nonzero()
225         color = image[mask].mean(axis=0)
226         out[mask] = color
227     return out
228
```
--- 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
@@ -219,7 +219,8 @@
     bg = (labels == bg_label)
     if bg.any():
         labels = labels[labels != bg_label]
-        out[bg] = bg_color
+        mask = (label_field == bg_label).nonzero()
+        out[mask] = bg_color
     for label in labels:
         mask = (label_field == label).nonzero()
         color = image[mask].mean(axis=0)

{"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@@ -219,7 +219,8 @@\n     bg = (labels == bg_label)\n     if bg.any():\n         labels = labels[labels != bg_label]\n-        out[bg] = bg_color\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", "issue": "label2rgb index error when using average and background color\nWhen using averaged label2rgb with bg_label the out image indexing fails with error:\r\n\r\n```\r\nTraceback (most recent call last):\r\n  File \"file.py\", line 222, in generate\r\n    overlay = color.label2rgb(domain, image=img, bg_label=0, bg_color=1.0, kind=\"avg\")\r\n  File \"[...]/python3.6/site-packages/skimage/color/colorlabel.py\", line 116, in label2rgb\r\n    return _label2rgb_avg(label, image, bg_label, bg_color)\r\n  File \"[...]/python3.6/site-packages/skimage/color/colorlabel.py\", line 225, in _label2rgb_avg\r\n    out[bg] = bg_color\r\nIndexError: boolean index did not match indexed array along dimension 0; dimension is 100 but corresponding boolean dimension is 2\r\n```\n", "before_files": [{"content": "import itertools\n\nimport numpy as np\n\nfrom .._shared.utils import warn\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\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.\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    else:\n        return _label2rgb_avg(label, image, bg_label, bg_color)\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.\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        image = img_as_float(rgb2gray(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 == np.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(max(mapped_labels_flat) + 1)\n\n    label_to_color = np.array([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_like(image)\n    labels = np.unique(label_field)\n    bg = (labels == bg_label)\n    if bg.any():\n        labels = labels[labels != bg_label]\n        out[bg] = 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\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\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.\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    else:\n        return _label2rgb_avg(label, image, bg_label, bg_color)\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.\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        image = img_as_float(rgb2gray(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 == np.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(max(mapped_labels_flat) + 1)\n\n    label_to_color = np.array([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_like(image)\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"}]}