Debian Codegraph @

dask_image/ndfilters/_gaussian.py

raw
1 # -*- coding: utf-8 -*-
2
3
4 import numbers
5
6 import numpy as np
7 import scipy.ndimage
8
9 from ..dispatch._dispatch_ndfilters import (
10 dispatch_gaussian_filter, dispatch_gaussian_gradient_magnitude,
11 dispatch_gaussian_laplace)
12 from . import _utils
13
14 __all__ = [
15 "gaussian_filter",
16 "gaussian_gradient_magnitude",
17 "gaussian_laplace",
18 "gaussian"
19 ]
20
21
22 def _get_sigmas(image, sigma):
23 ndim = image.ndim
24
25 nsigmas = np.array(sigma)
26 if nsigmas.ndim == 0:
27 nsigmas = np.array(ndim * [nsigmas[()]])
28
29 if nsigmas.ndim != 1:
30 raise RuntimeError(
31 "Must have a single sigma or a single sequence."
32 )
33
34 if ndim != len(nsigmas):
35 raise RuntimeError(
36 "Must have an equal number of sigmas to image dimensions."
37 )
38
39 if not issubclass(nsigmas.dtype.type, numbers.Real):
40 raise TypeError("Must have real sigmas.")
41
42 nsigmas = tuple(nsigmas)
43
44 return nsigmas
45
46
47 def _get_border(image, sigma, truncate):
48 sigma = np.array(_get_sigmas(image, sigma))
49
50 if not isinstance(truncate, numbers.Real):
51 raise TypeError("Must have a real truncate value.")
52
53 half_shape = tuple(np.ceil(sigma * truncate).astype(int))
54
55 return half_shape
56
57
58 @_utils._update_wrapper(scipy.ndimage.gaussian_filter)
59 def gaussian_filter(image,
60 sigma,
61 order=0,
62 mode='reflect',
63 cval=0.0,
64 truncate=4.0):
65 sigma = _get_sigmas(image, sigma)
66 depth = _get_border(image, sigma, truncate)
67
68 depth, boundary = _utils._get_depth_boundary(image.ndim, depth, "none")
69
70 result = image.map_overlap(
71 dispatch_gaussian_filter(image),
72 depth=depth,
73 boundary=boundary,
74 dtype=image.dtype,
75 meta=image._meta,
76 sigma=sigma,
77 order=order,
78 mode=mode,
79 cval=cval,
80 truncate=truncate
81 )
82
83 return result
84
85
86 def gaussian(image,
87 sigma,
88 order=0,
89 mode='reflect',
90 cval=0.0,
91 truncate=4.0):
92 """Alias of `dask_image.ndfilters.gaussian_filter`."""
93 return gaussian_filter(image,
94 sigma,
95 order=order,
96 mode=mode,
97 cval=cval,
98 truncate=truncate)
99
100
101 @_utils._update_wrapper(scipy.ndimage.gaussian_gradient_magnitude)
102 def gaussian_gradient_magnitude(image,
103 sigma,
104 mode='reflect',
105 cval=0.0,
106 truncate=4.0,
107 **kwargs):
108 sigma = _get_sigmas(image, sigma)
109 depth = _get_border(image, sigma, truncate)
110
111 depth, boundary = _utils._get_depth_boundary(image.ndim, depth, "none")
112
113 result = image.map_overlap(
114 dispatch_gaussian_gradient_magnitude(image),
115 depth=depth,
116 boundary=boundary,
117 dtype=image.dtype,
118 meta=image._meta,
119 sigma=sigma,
120 mode=mode,
121 cval=cval,
122 truncate=truncate,
123 **kwargs
124 )
125
126 return result
127
128
129 @_utils._update_wrapper(scipy.ndimage.gaussian_laplace)
130 def gaussian_laplace(image,
131 sigma,
132 mode='reflect',
133 cval=0.0,
134 truncate=4.0,
135 **kwargs):
136 sigma = _get_sigmas(image, sigma)
137 depth = _get_border(image, sigma, truncate)
138
139 depth, boundary = _utils._get_depth_boundary(image.ndim, depth, "none")
140
141 result = image.map_overlap(
142 dispatch_gaussian_laplace(image),
143 depth=depth,
144 boundary=boundary,
145 dtype=image.dtype,
146 meta=image._meta,
147 sigma=sigma,
148 mode=mode,
149 cval=cval,
150 truncate=truncate,
151 **kwargs
152 )
153
154 return result