Datasets:

Modalities:
Image
Text
Formats:
text
Size:
< 1K
Libraries:
Datasets
License:
File size: 13,525 Bytes
d381118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"""
Binary morphological operations
"""

import warnings

import numpy as np
from scipy import ndimage as ndi

from .footprints import _footprint_is_sequence, pad_footprint
from .misc import default_footprint
from .._shared.utils import deprecate_func


def _iterate_binary_func(binary_func, image, footprint, out, border_value):
    """Helper to call `binary_func` for each footprint in a sequence.

    binary_func is a binary morphology function that accepts "structure",
    "output" and "iterations" keyword arguments
    (e.g. `scipy.ndimage.binary_erosion`).
    """
    fp, num_iter = footprint[0]
    binary_func(
        image, structure=fp, output=out, iterations=num_iter, border_value=border_value
    )
    for fp, num_iter in footprint[1:]:
        # Note: out.copy() because the computation cannot be in-place!
        #       SciPy <= 1.7 did not automatically make a copy if needed.
        binary_func(
            out.copy(),
            structure=fp,
            output=out,
            iterations=num_iter,
            border_value=border_value,
        )
    return out


# The default_footprint decorator provides a diamond footprint as
# default with the same dimension as the input image and size 3 along each
# axis.
@default_footprint
@deprecate_func(
    deprecated_version="0.26",
    removed_version="0.28",
    hint="Use `skimage.morphology.erosion` instead. "
    "Note the pixel shift by 1 for even-sized footprints (see docstring notes).",
)
def binary_erosion(image, footprint=None, out=None, *, mode='ignore'):
    """Return fast binary morphological erosion of an image.

    This function returns the same result as grayscale erosion but performs
    faster for binary images.

    Morphological erosion sets a pixel at ``(i,j)`` to the minimum over all
    pixels in the neighborhood centered at ``(i,j)``. Erosion shrinks bright
    regions and enlarges dark regions.

    Parameters
    ----------
    image : ndarray
        Binary input image.
    footprint : ndarray or tuple, optional
        The neighborhood expressed as a 2-D array of 1's and 0's.
        If None, use a cross-shaped footprint (connectivity=1). The footprint
        can also be provided as a sequence of smaller footprints as described
        in the notes below.
    out : ndarray of bool, optional
        The array to store the result of the morphology. If None is
        passed, a new array will be allocated.
    mode : str, optional
        The `mode` parameter determines how the array borders are handled.
        Valid modes are: 'max', 'min', 'ignore'.
        If 'max' or 'ignore', pixels outside the image domain are assumed
        to be `True`, which causes them to not influence the result.
        Default is 'ignore'.

        .. versionadded:: 0.23
            `mode` was added in 0.23.

    Returns
    -------
    eroded : ndarray of bool or uint
        The result of the morphological erosion taking values in
        ``[False, True]``.

    Notes
    -----
    The footprint can also be a provided as a sequence of 2-tuples where the
    first element of each 2-tuple is a footprint ndarray and the second element
    is an integer describing the number of times it should be iterated. For
    example ``footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)]``
    would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net
    effect that is the same as ``footprint=np.ones((9, 9))``, but with lower
    computational cost. Most of the builtin footprints such as
    :func:`skimage.morphology.disk` provide an option to automatically generate a
    footprint sequence of this type.

    For even-sized footprints, :func:`skimage.morphology.erosion` and
    this function produce an output that differs: one is shifted by one pixel
    compared to the other. :func:`skimage.morphology.pad_footprint´ is available
    to account for this.

    See also
    --------
    skimage.morphology.isotropic_erosion

    """
    if out is None:
        out = np.empty(image.shape, dtype=bool)

    if mode not in {"max", "min", "ignore"}:
        raise ValueError(f"unsupported mode, got {mode!r}")
    border_value = False if mode == 'min' else True

    footprint = pad_footprint(footprint, pad_end=True)
    if not _footprint_is_sequence(footprint):
        footprint = [(footprint, 1)]

    out = _iterate_binary_func(
        binary_func=ndi.binary_erosion,
        image=image,
        footprint=footprint,
        out=out,
        border_value=border_value,
    )
    return out


@default_footprint
@deprecate_func(
    deprecated_version="0.26",
    removed_version="0.28",
    hint="Use `skimage.morphology.dilation` instead. "
    "Note the lack of mirroring for non-symmetric footprints (see docstring notes).",
)
def binary_dilation(image, footprint=None, out=None, *, mode='ignore'):
    """Return fast binary morphological dilation of an image.

    This function returns the same result as grayscale dilation but performs
    faster for binary images.

    Morphological dilation sets a pixel at ``(i,j)`` to the maximum over all
    pixels in the neighborhood centered at ``(i,j)``. Dilation enlarges bright
    regions and shrinks dark regions.

    Parameters
    ----------
    image : ndarray
        Binary input image.
    footprint : ndarray or tuple, optional
        The neighborhood expressed as a 2-D array of 1's and 0's.
        If None, use a cross-shaped footprint (connectivity=1). The footprint
        can also be provided as a sequence of smaller footprints as described
        in the notes below.
    out : ndarray of bool, optional
        The array to store the result of the morphology. If None is
        passed, a new array will be allocated.
    mode : str, optional
        The `mode` parameter determines how the array borders are handled.
        Valid modes are: 'max', 'min', 'ignore'.
        If 'min' or 'ignore', pixels outside the image domain are assumed
        to be `False`, which causes them to not influence the result.
        Default is 'ignore'.

        .. versionadded:: 0.23
            `mode` was added in 0.23.

    Returns
    -------
    dilated : ndarray of bool or uint
        The result of the morphological dilation with values in
        ``[False, True]``.

    Notes
    -----
    The footprint can also be a provided as a sequence of 2-tuples where the
    first element of each 2-tuple is a footprint ndarray and the second element
    is an integer describing the number of times it should be iterated. For
    example ``footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)]``
    would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net
    effect that is the same as ``footprint=np.ones((9, 9))``, but with lower
    computational cost. Most of the builtin footprints such as
    :func:`skimage.morphology.disk` provide an option to automatically generate a
    footprint sequence of this type.

    For non-symmetric footprints, :func:`skimage.morphology.binary_dilation`
    and :func:`skimage.morphology.dilation` produce an output that differs:
    `binary_dilation` mirrors the footprint, whereas `dilation` does not.
    :func:`skimage.morphology.mirror_footprint` is available to correct for this.

    See also
    --------
    skimage.morphology.isotropic_dilation

    """
    if out is None:
        out = np.empty(image.shape, dtype=bool)

    if mode not in {"max", "min", "ignore"}:
        raise ValueError(f"unsupported mode, got {mode!r}")
    border_value = True if mode == 'max' else False

    footprint = pad_footprint(footprint, pad_end=True)
    if not _footprint_is_sequence(footprint):
        footprint = [(footprint, 1)]

    out = _iterate_binary_func(
        binary_func=ndi.binary_dilation,
        image=image,
        footprint=footprint,
        out=out,
        border_value=border_value,
    )
    return out


@default_footprint
@deprecate_func(
    deprecated_version="0.26",
    removed_version="0.28",
    hint="Use `skimage.morphology.opening` instead.",
)
def binary_opening(image, footprint=None, out=None, *, mode='ignore'):
    """Return fast binary morphological opening of an image.

    This function returns the same result as grayscale opening but performs
    faster for binary images.

    The morphological opening on an image is defined as an erosion followed by
    a dilation. Opening can remove small bright spots (i.e. "salt") and connect
    small dark cracks. This tends to "open" up (dark) gaps between (bright)
    features.

    Parameters
    ----------
    image : ndarray
        Binary input image.
    footprint : ndarray or tuple, optional
        The neighborhood expressed as a 2-D array of 1's and 0's.
        If None, use a cross-shaped footprint (connectivity=1). The footprint
        can also be provided as a sequence of smaller footprints as described
        in the notes below.
    out : ndarray of bool, optional
        The array to store the result of the morphology. If None
        is passed, a new array will be allocated.
    mode : str, optional
        The `mode` parameter determines how the array borders are handled.
        Valid modes are: 'max', 'min', 'ignore'.
        If 'ignore', pixels outside the image domain are assumed to be `True`
        for the erosion and `False` for the dilation, which causes them to not
        influence the result. Default is 'ignore'.

        .. versionadded:: 0.23
            `mode` was added in 0.23.

    Returns
    -------
    opening : ndarray of bool
        The result of the morphological opening.

    Notes
    -----
    The footprint can also be a provided as a sequence of 2-tuples where the
    first element of each 2-tuple is a footprint ndarray and the second element
    is an integer describing the number of times it should be iterated. For
    example ``footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)]``
    would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net
    effect that is the same as ``footprint=np.ones((9, 9))``, but with lower
    computational cost. Most of the builtin footprints such as
    :func:`skimage.morphology.disk` provide an option to automatically generate a
    footprint sequence of this type.

    See also
    --------
    skimage.morphology.isotropic_opening

    """
    with warnings.catch_warnings():
        warnings.filterwarnings(
            action="ignore",
            message="`binary_(dilation|erosion)` is deprecated",
            category=FutureWarning,
            module="skimage",
        )
        tmp = binary_erosion(image, footprint, mode=mode)
        out = binary_dilation(tmp, footprint, out=out, mode=mode)
    return out


@default_footprint
@deprecate_func(
    deprecated_version="0.26",
    removed_version="0.28",
    hint="Use `skimage.morphology.closing` instead.",
)
def binary_closing(image, footprint=None, out=None, *, mode='ignore'):
    """Return fast binary morphological closing of an image.

    This function returns the same result as grayscale closing but performs
    faster for binary images.

    The morphological closing on an image is defined as a dilation followed by
    an erosion. Closing can remove small dark spots (i.e. "pepper") and connect
    small bright cracks. This tends to "close" up (dark) gaps between (bright)
    features.

    Parameters
    ----------
    image : ndarray
        Binary input image.
    footprint : ndarray or tuple, optional
        The neighborhood expressed as a 2-D array of 1's and 0's.
        If None, use a cross-shaped footprint (connectivity=1). The footprint
        can also be provided as a sequence of smaller footprints as described
        in the notes below.
    out : ndarray of bool, optional
        The array to store the result of the morphology. If None,
        is passed, a new array will be allocated.
    mode : str, optional
        The `mode` parameter determines how the array borders are handled.
        Valid modes are: 'max', 'min', 'ignore'.
        If 'ignore', pixels outside the image domain are assumed to be `True`
        for the erosion and `False` for the dilation, which causes them to not
        influence the result. Default is 'ignore'.

        .. versionadded:: 0.23
            `mode` was added in 0.23.

    Returns
    -------
    closing : ndarray of bool
        The result of the morphological closing.

    Notes
    -----
    The footprint can also be a provided as a sequence of 2-tuples where the
    first element of each 2-tuple is a footprint ndarray and the second element
    is an integer describing the number of times it should be iterated. For
    example ``footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)]``
    would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net
    effect that is the same as ``footprint=np.ones((9, 9))``, but with lower
    computational cost. Most of the builtin footprints such as
    :func:`skimage.morphology.disk` provide an option to automatically generate a
    footprint sequence of this type.

    See also
    --------
    skimage.morphology.isotropic_closing

    """
    with warnings.catch_warnings():
        warnings.filterwarnings(
            action="ignore",
            message="`binary_(dilation|erosion)` is deprecated",
            category=FutureWarning,
            module="skimage",
        )
        tmp = binary_dilation(image, footprint, mode=mode)
        out = binary_erosion(tmp, footprint, out=out, mode=mode)
    return out