File: edges.py

package info (click to toggle)
python-sigima 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 25,608 kB
  • sloc: python: 35,251; makefile: 3
file content (328 lines) | stat: -rw-r--r-- 8,408 bytes parent folder | download
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
# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.

"""
Edge detection computation module.
----------------------------------

This module implements edge detection computation functions, enabling the identification
of boundaries in an image. Edge detection may be used for image segmentation, shape
analysis, and feature extraction. Methods rely on detection of significant transitions
in pixel intensity.

Available filters (in alphabetical order):
    * canny: Canny edge detector
    * farid: Farid filter
    * farid_h: Horizontal Farid filter
    * farid_v: Vertical Farid filter
    * laplace: Laplace filter
    * prewitt: Prewitt filter
    * prewitt_h: Horizontal Prewitt filter
    * prewitt_v: Vertical Prewitt filter
    * roberts: Roberts filter
    * scharr: Scharr filter
    * scharr_h: Horizontal Scharr filter
    * scharr_v: Vertical Scharr filter
    * sobel: Sobel filter
    * sobel_h: Horizontal Sobel filter
    * sobel_v: Vertical Sobel filter

"""

# pylint: disable=invalid-name  # Allows short names like x, y...

# Note:
# ----
# - All `guidata.dataset.DataSet` parameter classes must also be imported in the
#   `sigima.params` module.
# - All functions decorated with `computation_function` must be imported in the upper
#   level `sigima.proc.image` module.

from __future__ import annotations

import guidata.dataset as gds  # type: ignore[import]
from skimage import feature, filters  # type: ignore[import]
from skimage.util import img_as_ubyte  # type: ignore[import]

from sigima.config import _
from sigima.enums import FilterMode
from sigima.objects.image import ImageObj
from sigima.proc.decorator import computation_function
from sigima.proc.image.base import Wrap1to1Func, dst_1_to_1, restore_data_outside_roi

# NOTE: Only parameter classes DEFINED in this module should be included in __all__.
# Parameter classes imported from other modules (like sigima.proc.base) should NOT
# be re-exported to avoid Sphinx cross-reference conflicts. The sigima.params module
# serves as the central API point that imports and re-exports all parameter classes.
__all__ = [
    "CannyParam",
    "canny",
    "farid",
    "farid_h",
    "farid_v",
    "laplace",
    "prewitt",
    "prewitt_h",
    "prewitt_v",
    "roberts",
    "scharr",
    "scharr_h",
    "scharr_v",
    "sobel",
    "sobel_h",
    "sobel_v",
]


class CannyParam(gds.DataSet):
    """Canny filter parameters."""

    sigma = gds.FloatItem(
        "Sigma",
        default=1.0,
        unit="pixels",
        min=0.0,
        nonzero=True,
        help=_("Standard deviation of the Gaussian filter."),
    )
    low_threshold = gds.FloatItem(
        _("Low threshold"),
        default=0.1,
        min=0.0,
        help=_("Lower bound for hysteresis thresholding (linking edges)."),
    )
    high_threshold = gds.FloatItem(
        _("High threshold"),
        default=0.9,
        min=0.0,
        help=_("Upper bound for hysteresis thresholding (linking edges)."),
    )
    use_quantiles = gds.BoolItem(
        _("Use quantiles"),
        default=True,
        help=_(
            "If True then treat low_threshold and high_threshold as quantiles "
            "of the edge magnitude image, rather than absolute edge magnitude "
            "values. If True then the thresholds must be in the range [0, 1]."
        ),
    )
    mode = gds.ChoiceItem(_("Mode"), FilterMode, default=FilterMode.CONSTANT)
    cval = gds.FloatItem(
        "cval",
        default=0.0,
        help=_("Value to fill past edges of input if mode is constant."),
    )


@computation_function()
def canny(src: ImageObj, p: CannyParam) -> ImageObj:
    """Compute Canny filter with :py:func:`skimage.feature.canny`.

    Args:
        src: Input image object.
        p: Parameters.

    Returns:
        Output image object.
    """
    dst = dst_1_to_1(
        src,
        "canny",
        f"sigma={p.sigma}, low_threshold={p.low_threshold}, "
        f"high_threshold={p.high_threshold}, use_quantiles={p.use_quantiles}, "
        f"mode={p.mode}, cval={p.cval}",
    )
    dst.data = img_as_ubyte(
        feature.canny(
            src.data,
            sigma=p.sigma,
            low_threshold=p.low_threshold,
            high_threshold=p.high_threshold,
            use_quantiles=p.use_quantiles,
            mode=p.mode,
            cval=p.cval,
        )
    )
    restore_data_outside_roi(dst, src)
    return dst


@computation_function()
def farid(src: ImageObj) -> ImageObj:
    """Compute Farid filter with :py:func:`skimage.filters.farid`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.farid)(src)


@computation_function()
def farid_h(src: ImageObj) -> ImageObj:
    """Compute horizontal Farid filter with :py:func:`skimage.filters.farid_h`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.farid_h)(src)


@computation_function()
def farid_v(src: ImageObj) -> ImageObj:
    """Compute vertical Farid filter with :py:func:`skimage.filters.farid_v`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.farid_v)(src)


@computation_function()
def laplace(src: ImageObj) -> ImageObj:
    """Compute Laplace filter with :py:func:`skimage.filters.laplace`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.laplace)(src)


@computation_function()
def prewitt(src: ImageObj) -> ImageObj:
    """Compute Prewitt filter with :py:func:`skimage.filters.prewitt`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.prewitt)(src)


@computation_function()
def prewitt_h(src: ImageObj) -> ImageObj:
    """Compute horizontal Prewitt filter with :py:func:`skimage.filters.prewitt_h`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.prewitt_h)(src)


@computation_function()
def prewitt_v(src: ImageObj) -> ImageObj:
    """Compute vertical Prewitt filter with :py:func:`skimage.filters.prewitt_v`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.prewitt_v)(src)


@computation_function()
def roberts(src: ImageObj) -> ImageObj:
    """Compute Roberts filter with :py:func:`skimage.filters.roberts`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.roberts)(src)


@computation_function()
def scharr(src: ImageObj) -> ImageObj:
    """Compute Scharr filter with :py:func:`skimage.filters.scharr`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.scharr)(src)


@computation_function()
def scharr_h(src: ImageObj) -> ImageObj:
    """Compute horizontal Scharr filter with :py:func:`skimage.filters.scharr_h`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.scharr_h)(src)


@computation_function()
def sobel(src: ImageObj) -> ImageObj:
    """Compute Sobel filter with :py:func:`skimage.filters.sobel`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.sobel)(src)


@computation_function()
def sobel_h(src: ImageObj) -> ImageObj:
    """Compute horizontal Sobel filter with :py:func:`skimage.filters.sobel_h`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.sobel_h)(src)


@computation_function()
def sobel_v(src: ImageObj) -> ImageObj:
    """Compute vertical Sobel filter with :py:func:`skimage.filters.sobel_v`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.sobel_v)(src)


@computation_function()
def scharr_v(src: ImageObj) -> ImageObj:
    """Compute vertical Scharr filter with :py:func:`skimage.filters.scharr_v`.

    Args:
        src: Input image object.

    Returns:
        Output image object.
    """
    return Wrap1to1Func(filters.scharr_v)(src)