Debian Codegraph @

dask_image/ndfourier/__init__.py

raw
1 # -*- coding: utf-8 -*-
2 import numbers
3
4 import dask.array as da
5
6 from . import _utils
7
8 __all__ = [
9 "fourier_gaussian",
10 "fourier_shift",
11 "fourier_uniform",
12 ]
13
14
15 def fourier_gaussian(image, sigma, n=-1, axis=-1):
16 """
17 Multi-dimensional Gaussian fourier filter.
18
19 The array is multiplied with the fourier transform of a Gaussian
20 kernel.
21
22 Parameters
23 ----------
24 image : array_like
25 The input image.
26 sigma : float or sequence
27 The sigma of the Gaussian kernel. If a float, `sigma` is the same for
28 all axes. If a sequence, `sigma` has to contain one value for each
29 axis.
30 n : int, optional
31 If `n` is negative (default), then the image is assumed to be the
32 result of a complex fft.
33 If `n` is larger than or equal to zero, the image is assumed to be the
34 result of a real fft, and `n` gives the length of the array before
35 transformation along the real transform direction.
36 axis : int, optional
37 The axis of the real transform.
38
39 Returns
40 -------
41 fourier_gaussian : Dask Array
42
43 Examples
44 --------
45 >>> from scipy import ndimage, misc
46 >>> import numpy.fft
47 >>> import matplotlib.pyplot as plt
48 >>> fig, (ax1, ax2) = plt.subplots(1, 2)
49 >>> plt.gray() # show the filtered result in grayscale
50 >>> ascent = misc.ascent()
51 >>> image = numpy.fft.fft2(ascent)
52 >>> result = ndimage.fourier_gaussian(image, sigma=4)
53 >>> result = numpy.fft.ifft2(result)
54 >>> ax1.imshow(ascent)
55 """
56
57 # Validate and normalize arguments
58 image, sigma, n, axis = _utils._norm_args(image, sigma, n=n, axis=axis)
59
60 # Compute frequencies
61 ang_freq_grid = _utils._get_ang_freq_grid(
62 image.shape,
63 chunks=image.chunks,
64 n=n,
65 axis=axis,
66 dtype=sigma.dtype
67 )
68
69 # Compute Fourier transformed Gaussian
70 result = image.copy()
71 scale = (sigma ** 2) / -2
72
73 for ax, f in enumerate(ang_freq_grid):
74 f *= f
75 gaussian = da.exp(scale[ax] * f)
76 gaussian = _utils._reshape_nd(gaussian, ndim=image.ndim, axis=ax)
77 result *= gaussian
78
79 return result
80
81
82 def fourier_shift(image, shift, n=-1, axis=-1):
83 """
84 Multi-dimensional fourier shift filter.
85
86 The array is multiplied with the fourier transform of a shift operation.
87
88 Parameters
89 ----------
90 image : array_like
91 The input image.
92 shift : float or sequence
93 The size of the box used for filtering.
94 If a float, `shift` is the same for all axes. If a sequence, `shift`
95 has to contain one value for each axis.
96 n : int, optional
97 If `n` is negative (default), then the image is assumed to be the
98 result of a complex fft.
99 If `n` is larger than or equal to zero, the image is assumed to be the
100 result of a real fft, and `n` gives the length of the array before
101 transformation along the real transform direction.
102 axis : int, optional
103 The axis of the real transform.
104
105 Returns
106 -------
107 fourier_shift : Dask Array
108
109 Examples
110 --------
111 >>> from scipy import ndimage, misc
112 >>> import matplotlib.pyplot as plt
113 >>> import numpy.fft
114 >>> fig, (ax1, ax2) = plt.subplots(1, 2)
115 >>> plt.gray() # show the filtered result in grayscale
116 >>> ascent = misc.ascent()
117 >>> image = numpy.fft.fft2(ascent)
118 >>> result = ndimage.fourier_shift(image, shift=200)
119 >>> result = numpy.fft.ifft2(result)
120 >>> ax1.imshow(ascent)
121 >>> ax2.imshow(result.real) # the imaginary part is an artifact
122 >>> plt.show()
123 """
124
125 if issubclass(image.dtype.type, numbers.Real):
126 image = image.astype(complex)
127
128 # Validate and normalize arguments
129 image, shift, n, axis = _utils._norm_args(image, shift, n=n, axis=axis)
130
131 # Constants with type converted
132 J = image.dtype.type(1j)
133
134 # Get the grid of frequencies
135 ang_freq_grid = _utils._get_ang_freq_grid(
136 image.shape,
137 chunks=image.chunks,
138 n=n,
139 axis=axis,
140 dtype=shift.dtype
141 )
142
143 # Apply shift
144 result = image.copy()
145 for ax, f in enumerate(ang_freq_grid):
146 phase_shift = da.exp((-J) * shift[ax] * f)
147 phase_shift = _utils._reshape_nd(phase_shift, ndim=image.ndim, axis=ax)
148 result *= phase_shift
149
150 return result
151
152
153 def fourier_uniform(image, size, n=-1, axis=-1):
154 """
155 Multi-dimensional uniform fourier filter.
156
157 The array is multiplied with the fourier transform of a box of given
158 size.
159
160 Parameters
161 ----------
162 image : array_like
163 The input image.
164 size : float or sequence
165 The size of the box used for filtering.
166 If a float, `size` is the same for all axes. If a sequence, `size` has
167 to contain one value for each axis.
168 n : int, optional
169 If `n` is negative (default), then the image is assumed to be the
170 result of a complex fft.
171 If `n` is larger than or equal to zero, the image is assumed to be the
172 result of a real fft, and `n` gives the length of the array before
173 transformation along the real transform direction.
174 axis : int, optional
175 The axis of the real transform.
176
177 Returns
178 -------
179 fourier_uniform : Dask Array
180 The filtered image. If `output` is given as a parameter, None is
181 returned.
182
183 Examples
184 --------
185 >>> from scipy import ndimage, misc
186 >>> import numpy.fft
187 >>> import matplotlib.pyplot as plt
188 >>> fig, (ax1, ax2) = plt.subplots(1, 2)
189 >>> plt.gray() # show the filtered result in grayscale
190 >>> ascent = misc.ascent()
191 >>> image = numpy.fft.fft2(ascent)
192 >>> result = ndimage.fourier_uniform(image, size=20)
193 >>> result = numpy.fft.ifft2(result)
194 >>> ax1.imshow(ascent)
195 >>> ax2.imshow(result.real) # the imaginary part is an artifact
196 >>> plt.show()
197 """
198
199 # Validate and normalize arguments
200 image, size, n, axis = _utils._norm_args(image, size, n=n, axis=axis)
201
202 # Get the grid of frequencies
203 freq_grid = _utils._get_freq_grid(
204 image.shape,
205 chunks=image.chunks,
206 n=n,
207 axis=axis,
208 dtype=size.dtype
209 )
210
211 # Compute uniform filter
212 result = image.copy()
213 for ax, f in enumerate(freq_grid):
214 uniform = da.sinc(size[ax] * f)
215 uniform = _utils._reshape_nd(uniform, ndim=image.ndim, axis=ax)
216 result *= uniform
217
218 return result