prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  measure.label speed
  This is triggered by [this Stackoverflow question](https://stackoverflow.com/questions/62804953/performance-differences-between-bwlabeln-on-matlab-and-skimage-measure-label-on/62842582#62842582). When I have large binary arrays to label and performance issues, I usually resort to calling the ndimage version. Could we imagine having a `fast_binary` flag which would call the ndimage function? A factor of 3-4 (from a few tests I just ran) is not bad...
  
  --- 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/measure/_label.py`
  Content:
  ```
  1 from ._ccomp import label_cython as clabel
  2 
  3 
  4 def label(input, background=None, return_num=False, connectivity=None):
  5     r"""Label connected regions of an integer array.
  6 
  7     Two pixels are connected when they are neighbors and have the same value.
  8     In 2D, they can be neighbors either in a 1- or 2-connected sense.
  9     The value refers to the maximum number of orthogonal hops to consider a
  10     pixel/voxel a neighbor::
  11 
  12       1-connectivity     2-connectivity     diagonal connection close-up
  13 
  14            [ ]           [ ]  [ ]  [ ]             [ ]
  15             |               \  |  /                 |  <- hop 2
  16       [ ]--[x]--[ ]      [ ]--[x]--[ ]        [x]--[ ]
  17             |               /  |  \             hop 1
  18            [ ]           [ ]  [ ]  [ ]
  19 
  20     Parameters
  21     ----------
  22     input : ndarray of dtype int
  23         Image to label.
  24     background : int, optional
  25         Consider all pixels with this value as background pixels, and label
  26         them as 0. By default, 0-valued pixels are considered as background
  27         pixels.
  28     return_num : bool, optional
  29         Whether to return the number of assigned labels.
  30     connectivity : int, optional
  31         Maximum number of orthogonal hops to consider a pixel/voxel
  32         as a neighbor.
  33         Accepted values are ranging from  1 to input.ndim. If ``None``, a full
  34         connectivity of ``input.ndim`` is used.
  35 
  36     Returns
  37     -------
  38     labels : ndarray of dtype int
  39         Labeled array, where all connected regions are assigned the
  40         same integer value.
  41     num : int, optional
  42         Number of labels, which equals the maximum label index and is only
  43         returned if return_num is `True`.
  44 
  45     See Also
  46     --------
  47     regionprops
  48 
  49     References
  50     ----------
  51     .. [1] Christophe Fiorio and Jens Gustedt, "Two linear time Union-Find
  52            strategies for image processing", Theoretical Computer Science
  53            154 (1996), pp. 165-181.
  54     .. [2] Kensheng Wu, Ekow Otoo and Arie Shoshani, "Optimizing connected
  55            component labeling algorithms", Paper LBNL-56864, 2005,
  56            Lawrence Berkeley National Laboratory (University of California),
  57            http://repositories.cdlib.org/lbnl/LBNL-56864
  58 
  59     Examples
  60     --------
  61     >>> import numpy as np
  62     >>> x = np.eye(3).astype(int)
  63     >>> print(x)
  64     [[1 0 0]
  65      [0 1 0]
  66      [0 0 1]]
  67     >>> print(label(x, connectivity=1))
  68     [[1 0 0]
  69      [0 2 0]
  70      [0 0 3]]
  71     >>> print(label(x, connectivity=2))
  72     [[1 0 0]
  73      [0 1 0]
  74      [0 0 1]]
  75     >>> print(label(x, background=-1))
  76     [[1 2 2]
  77      [2 1 2]
  78      [2 2 1]]
  79     >>> x = np.array([[1, 0, 0],
  80     ...               [1, 1, 5],
  81     ...               [0, 0, 0]])
  82     >>> print(label(x))
  83     [[1 0 0]
  84      [1 1 2]
  85      [0 0 0]]
  86     """
  87     return clabel(input, background, return_num, connectivity)
  88
  ```
  --- 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()
  
  ```
  
golden_diff: diff --git a/skimage/measure/_label.py b/skimage/measure/_label.py
  --- a/skimage/measure/_label.py
  +++ b/skimage/measure/_label.py
  @@ -1,6 +1,34 @@
  +from scipy import ndimage
   from ._ccomp import label_cython as clabel
   
   
  +def _label_bool(image, background=None, return_num=False, connectivity=None):
  +    """Faster implementation of clabel for boolean input.
  +
  +    See context: https://github.com/scikit-image/scikit-image/issues/4833
  +    """
  +    from ..morphology._util import _resolve_neighborhood
  +    if background == 1:
  +        image = ~image
  +
  +    if connectivity is None:
  +        connectivity = image.ndim
  +
  +    if not 1 <= connectivity <= image.ndim:
  +        raise ValueError(
  +            f'Connectivity for {image.ndim}D image should '
  +            f'be in [1, ..., {image.ndim}]. Got {connectivity}.'
  +        )
  +
  +    selem = _resolve_neighborhood(None, connectivity, image.ndim)
  +    result = ndimage.label(image, structure=selem)
  +
  +    if return_num:
  +        return result
  +    else:
  +        return result[0]
  +
  +
   def label(input, background=None, return_num=False, connectivity=None):
       r"""Label connected regions of an integer array.
   
  @@ -84,4 +112,8 @@
        [1 1 2]
        [0 0 0]]
       """
  -    return clabel(input, background, return_num, connectivity)
  +    if input.dtype == bool:
  +        return _label_bool(input, background=background,
  +                           return_num=return_num, connectivity=connectivity)
  +    else:
  +        return clabel(input, background, return_num, connectivity)
  
verification_info: {"golden_diff": "diff --git a/skimage/measure/_label.py b/skimage/measure/_label.py\n--- a/skimage/measure/_label.py\n+++ b/skimage/measure/_label.py\n@@ -1,6 +1,34 @@\n+from scipy import ndimage\n from ._ccomp import label_cython as clabel\n \n \n+def _label_bool(image, background=None, return_num=False, connectivity=None):\n+    \"\"\"Faster implementation of clabel for boolean input.\n+\n+    See context: https://github.com/scikit-image/scikit-image/issues/4833\n+    \"\"\"\n+    from ..morphology._util import _resolve_neighborhood\n+    if background == 1:\n+        image = ~image\n+\n+    if connectivity is None:\n+        connectivity = image.ndim\n+\n+    if not 1 <= connectivity <= image.ndim:\n+        raise ValueError(\n+            f'Connectivity for {image.ndim}D image should '\n+            f'be in [1, ..., {image.ndim}]. Got {connectivity}.'\n+        )\n+\n+    selem = _resolve_neighborhood(None, connectivity, image.ndim)\n+    result = ndimage.label(image, structure=selem)\n+\n+    if return_num:\n+        return result\n+    else:\n+        return result[0]\n+\n+\n def label(input, background=None, return_num=False, connectivity=None):\n     r\"\"\"Label connected regions of an integer array.\n \n@@ -84,4 +112,8 @@\n      [1 1 2]\n      [0 0 0]]\n     \"\"\"\n-    return clabel(input, background, return_num, connectivity)\n+    if input.dtype == bool:\n+        return _label_bool(input, background=background,\n+                           return_num=return_num, connectivity=connectivity)\n+    else:\n+        return clabel(input, background, return_num, connectivity)\n", "issue": "measure.label speed\nThis is triggered by [this Stackoverflow question](https://stackoverflow.com/questions/62804953/performance-differences-between-bwlabeln-on-matlab-and-skimage-measure-label-on/62842582#62842582). When I have large binary arrays to label and performance issues, I usually resort to calling the ndimage version. Could we imagine having a `fast_binary` flag which would call the ndimage function? A factor of 3-4 (from a few tests I just ran) is not bad...\n", "before_files": [{"content": "from ._ccomp import label_cython as clabel\n\n\ndef label(input, background=None, return_num=False, connectivity=None):\n    r\"\"\"Label connected regions of an integer array.\n\n    Two pixels are connected when they are neighbors and have the same value.\n    In 2D, they can be neighbors either in a 1- or 2-connected sense.\n    The value refers to the maximum number of orthogonal hops to consider a\n    pixel/voxel a neighbor::\n\n      1-connectivity     2-connectivity     diagonal connection close-up\n\n           [ ]           [ ]  [ ]  [ ]             [ ]\n            |               \\  |  /                 |  <- hop 2\n      [ ]--[x]--[ ]      [ ]--[x]--[ ]        [x]--[ ]\n            |               /  |  \\             hop 1\n           [ ]           [ ]  [ ]  [ ]\n\n    Parameters\n    ----------\n    input : ndarray of dtype int\n        Image to label.\n    background : int, optional\n        Consider all pixels with this value as background pixels, and label\n        them as 0. By default, 0-valued pixels are considered as background\n        pixels.\n    return_num : bool, optional\n        Whether to return the number of assigned labels.\n    connectivity : int, optional\n        Maximum number of orthogonal hops to consider a pixel/voxel\n        as a neighbor.\n        Accepted values are ranging from  1 to input.ndim. If ``None``, a full\n        connectivity of ``input.ndim`` is used.\n\n    Returns\n    -------\n    labels : ndarray of dtype int\n        Labeled array, where all connected regions are assigned the\n        same integer value.\n    num : int, optional\n        Number of labels, which equals the maximum label index and is only\n        returned if return_num is `True`.\n\n    See Also\n    --------\n    regionprops\n\n    References\n    ----------\n    .. [1] Christophe Fiorio and Jens Gustedt, \"Two linear time Union-Find\n           strategies for image processing\", Theoretical Computer Science\n           154 (1996), pp. 165-181.\n    .. [2] Kensheng Wu, Ekow Otoo and Arie Shoshani, \"Optimizing connected\n           component labeling algorithms\", Paper LBNL-56864, 2005,\n           Lawrence Berkeley National Laboratory (University of California),\n           http://repositories.cdlib.org/lbnl/LBNL-56864\n\n    Examples\n    --------\n    >>> import numpy as np\n    >>> x = np.eye(3).astype(int)\n    >>> print(x)\n    [[1 0 0]\n     [0 1 0]\n     [0 0 1]]\n    >>> print(label(x, connectivity=1))\n    [[1 0 0]\n     [0 2 0]\n     [0 0 3]]\n    >>> print(label(x, connectivity=2))\n    [[1 0 0]\n     [0 1 0]\n     [0 0 1]]\n    >>> print(label(x, background=-1))\n    [[1 2 2]\n     [2 1 2]\n     [2 2 1]]\n    >>> x = np.array([[1, 0, 0],\n    ...               [1, 1, 5],\n    ...               [0, 0, 0]])\n    >>> print(label(x))\n    [[1 0 0]\n     [1 1 2]\n     [0 0 0]]\n    \"\"\"\n    return clabel(input, background, return_num, connectivity)\n", "path": "skimage/measure/_label.py"}], "after_files": [{"content": "from scipy import ndimage\nfrom ._ccomp import label_cython as clabel\n\n\ndef _label_bool(image, background=None, return_num=False, connectivity=None):\n    \"\"\"Faster implementation of clabel for boolean input.\n\n    See context: https://github.com/scikit-image/scikit-image/issues/4833\n    \"\"\"\n    from ..morphology._util import _resolve_neighborhood\n    if background == 1:\n        image = ~image\n\n    if connectivity is None:\n        connectivity = image.ndim\n\n    if not 1 <= connectivity <= image.ndim:\n        raise ValueError(\n            f'Connectivity for {image.ndim}D image should '\n            f'be in [1, ..., {image.ndim}]. Got {connectivity}.'\n        )\n\n    selem = _resolve_neighborhood(None, connectivity, image.ndim)\n    result = ndimage.label(image, structure=selem)\n\n    if return_num:\n        return result\n    else:\n        return result[0]\n\n\ndef label(input, background=None, return_num=False, connectivity=None):\n    r\"\"\"Label connected regions of an integer array.\n\n    Two pixels are connected when they are neighbors and have the same value.\n    In 2D, they can be neighbors either in a 1- or 2-connected sense.\n    The value refers to the maximum number of orthogonal hops to consider a\n    pixel/voxel a neighbor::\n\n      1-connectivity     2-connectivity     diagonal connection close-up\n\n           [ ]           [ ]  [ ]  [ ]             [ ]\n            |               \\  |  /                 |  <- hop 2\n      [ ]--[x]--[ ]      [ ]--[x]--[ ]        [x]--[ ]\n            |               /  |  \\             hop 1\n           [ ]           [ ]  [ ]  [ ]\n\n    Parameters\n    ----------\n    input : ndarray of dtype int\n        Image to label.\n    background : int, optional\n        Consider all pixels with this value as background pixels, and label\n        them as 0. By default, 0-valued pixels are considered as background\n        pixels.\n    return_num : bool, optional\n        Whether to return the number of assigned labels.\n    connectivity : int, optional\n        Maximum number of orthogonal hops to consider a pixel/voxel\n        as a neighbor.\n        Accepted values are ranging from  1 to input.ndim. If ``None``, a full\n        connectivity of ``input.ndim`` is used.\n\n    Returns\n    -------\n    labels : ndarray of dtype int\n        Labeled array, where all connected regions are assigned the\n        same integer value.\n    num : int, optional\n        Number of labels, which equals the maximum label index and is only\n        returned if return_num is `True`.\n\n    See Also\n    --------\n    regionprops\n\n    References\n    ----------\n    .. [1] Christophe Fiorio and Jens Gustedt, \"Two linear time Union-Find\n           strategies for image processing\", Theoretical Computer Science\n           154 (1996), pp. 165-181.\n    .. [2] Kensheng Wu, Ekow Otoo and Arie Shoshani, \"Optimizing connected\n           component labeling algorithms\", Paper LBNL-56864, 2005,\n           Lawrence Berkeley National Laboratory (University of California),\n           http://repositories.cdlib.org/lbnl/LBNL-56864\n\n    Examples\n    --------\n    >>> import numpy as np\n    >>> x = np.eye(3).astype(int)\n    >>> print(x)\n    [[1 0 0]\n     [0 1 0]\n     [0 0 1]]\n    >>> print(label(x, connectivity=1))\n    [[1 0 0]\n     [0 2 0]\n     [0 0 3]]\n    >>> print(label(x, connectivity=2))\n    [[1 0 0]\n     [0 1 0]\n     [0 0 1]]\n    >>> print(label(x, background=-1))\n    [[1 2 2]\n     [2 1 2]\n     [2 2 1]]\n    >>> x = np.array([[1, 0, 0],\n    ...               [1, 1, 5],\n    ...               [0, 0, 0]])\n    >>> print(label(x))\n    [[1 0 0]\n     [1 1 2]\n     [0 0 0]]\n    \"\"\"\n    if input.dtype == bool:\n        return _label_bool(input, background=background,\n                           return_num=return_num, connectivity=connectivity)\n    else:\n        return clabel(input, background, return_num, connectivity)\n", "path": "skimage/measure/_label.py"}]}