diff options
author | Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr> | 2019-12-23 13:45:09 +0100 |
---|---|---|
committer | Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr> | 2019-12-23 13:45:09 +0100 |
commit | 5d647cf9a6159afd2933da594b9c79ad93d3cd9b (patch) | |
tree | 2571025a602f68fc8933b01104dc712d41f84034 /silx/image/utils.py | |
parent | 654a6ac93513c3cc1ef97cacd782ff674c6f4559 (diff) |
New upstream version 0.12.0~b0+dfsg
Diffstat (limited to 'silx/image/utils.py')
-rw-r--r-- | silx/image/utils.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/silx/image/utils.py b/silx/image/utils.py new file mode 100644 index 0000000..996d010 --- /dev/null +++ b/silx/image/utils.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +# /*########################################################################## +# Copyright (C) 2019 European Synchrotron Radiation Facility +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# ############################################################################*/ + +import numpy as np +from math import ceil + +def gaussian_kernel(sigma, cutoff=4, force_odd_size=False): + """ + Generates a Gaussian convolution kernel. + + :param sigma: Standard Deviation of the Gaussian curve. + :param cutoff: Parameter tuning the truncation of the Gaussian. + The higher cutoff, the biggest the array will be (and the closest to + a "true" Gaussian function). + :param force_odd_size: when set to True, the resulting array will always + have an odd size, regardless of the values of "sigma" and "cutoff". + :return: a numpy.ndarray containing the truncated Gaussian function. + The array size is 2*c*s+1 where c=cutoff, s=sigma. + + Nota: due to the quick decay of the Gaussian function, small values of the + "cutoff" parameter are usually fine. The energy difference between a + Gaussian truncated to [-c, c] and a "true" one is + erfc(c/(sqrt(2)*s)) + so choosing cutoff=4*sigma keeps the truncation error below 1e-4. + """ + size = int(ceil(2 * cutoff * sigma + 1)) + if force_odd_size and size % 2 == 0: + size += 1 + x = np.arange(size) - (size - 1.0) / 2.0 + g = np.exp(-(x / sigma) ** 2 / 2.0) + g /= g.sum() + return g |