Utility functions¶
This module contains several utility functions which can be used e.g. for thresholding the alpha-shearlet coefficients or for using the alpha-shearlet transform for denoising.
Finally, it also contains the functions my_ravel() and my_unravel()
which can be used to convert the alpha-shearlet coefficients into a
1-dimensional vector and back. This is in particular convenient for the
subsampled transform, where this conversion is not entirely trivial, since the
different “coefficient images” have varying dimensions.
Thresholding¶
-
AlphaTransformUtility.threshold(coeffs, thresh_value, mode)¶ Given a set of coefficients, this function performs a thresholding procedure, i.e., either soft or hard thresholding.
Required parameters
Parameters: - coeffs –
The coefficients to be thresholded.
Either a three-dimensional
numpy.ndarrayor a generator producing two dimensionalnumpy.ndarrayobjects. - thresh_value (float) – The thresholding cutoff
for the coefficients, see also
modefor more details. - mode (string) –
Either
'hard'or'soft'. This parameter determines whether the hard thresholding operator
or the soft thresholding operator

is applied to each entry of the coefficients.
Return value
Returns: A generator producing the thresholded coefficients. Each thresholded “coefficient image”, i.e., each thresholded 2-dimensional array, is produced in turn. - coeffs –
Denoising¶
-
AlphaTransformUtility.denoise(img, trafo, noise_lvl, multipliers=None)¶ Given a noisy image
, this function performs a denoising
procedure based on shearlet thresholding. More precisely:- A scale dependent threshold parameter
is calculated
according to
, where
is a multiplier for the jth scale,
is the
noise level present in the image
and
are its dimensions. - The alpha-shearlet transform of
is calculated
using trafo. - Hard thesholding with threshold parameter (cutoff)
is
performed on alpha-shearlet coefficients, i.e., for each scale j, each of the coefficients belonging to the jth scale is set to zero if its absolute value is smaller than
and otherwise it is
left unchanged. - The (pseudo)-inverse of the alpha-shearlet transform is applied to the thresholded coefficients and this reconstruction is the return value of the function.
Required parameters
Parameters: - img (numpy.ndarray) – The “image” (2 dimensional array) that should be denoised.
- trafo –
An object of class
AlphaTransform.AlphaShearletTransform. This object is used to calculate the (inverse) alpha-shearlet transform during the denoising procedure.The dimension of the transform and of
imgneed to coincide. - noise_lvl (float) –
The (presumed) noise level present in
img. Ifimg = img_clean + noise, thennoise_lvlshould be approximately equal to the
norm of noise.In particular, if
imis obtained by adding Gaussian noise with standard deviation
(in each entry) to a noise free
image
, then the noise level
is given by
; see also
AdaptiveAlpha.optimize_denoising().
Keyword parameter
Parameters: multipliers (list) – A list of multipliers (floats) for each scale.
multipliers[j]determines the value of
and thus of the cutoff
for scale j.In particular,
len(multipliers)needs to be equal to the number of the scales oftrafo.Return value
Returns: The denoised image, i.e., the result of the denoising procedure described above. - A scale dependent threshold parameter
Vectorizing the alpha-shearlet coefficients¶
-
AlphaTransformUtility.my_ravel(coeff)¶ The subsampled alpha-shearlet transform returns a list of differently sized(!) two-dimensional arrays. Likewise, the fully sampled transform yields a three dimensional numpy array containing the coefficients. The present function can be used (in both cases) to convert this list into a single one-dimensional numpy array.
Note
In order to invert this conversion to a one-dimensional array, use the associated function
my_unravel(). Precisely,my_unravel()satisfiesmy_unravel(my_trafo, my_ravel(coeff)) == coeff, if coeff is obtained from callingmy_trafo.transform(im)for some imageim.The preceding equality holds at least up to (negligible) differences (the left-hand side is a generator while the right-hand side could also be a list).
Required parameter
Parameters: coeff (list) – A list (or a generator) containing/producing two-dimensional numpy arrays. Return value
Returns: A one-dimensional numpy.ndarrayfrom which coeff can be reconstructed.
-
AlphaTransformUtility.my_unravel(trafo, coeff)¶ This method is a companion method to
my_ravel(). See the documentation of that function for more details.Required parameters
Parameters: - trafo – An object of class
AlphaTransform.AlphaShearletTransform. - coeff (numpy.ndarray) – A one-dimensional numpy array, obtained via
my_ravel(coeff_unrav), wherecoeff_unravis of the same dimensions as the output oftrafo.transform(im), whereimis an image.
Return value
Returns: A generator producing the same values as coeff_unrav, i.e., an “unravelled” version ofcoeff.- trafo – An object of class
Miscelaneous utility functions¶
-
AlphaTransformUtility.find_free_file(file_template)¶ This function finds the first nonexistent (“free”) file obtained by “counting upwards” using the passed template/pattern.
Required Parameter
Parameters: file_template (string) – This should be a string whose
format()method can be called using only an integer argument, e.g.'/home/test_{0:0>2d}.txt', which would result infind_free_fileconsecutively checking the following files for existence:/home/test_00.txt, /home/test_01.txt, ...
Return value
Returns: file_template.format(i)for the first value ofifor which the corresponding file does not yet exist.
-
AlphaTransformUtility.scale_gen(trafo)¶ Required parameter
Parameters: trafo – An object of class AlphaTransform.AlphaShearletTransform.Return value
Returns: A generator producing integers. The i-th produced integer is the scale (starting from -1 for the low-pass part) of the i-th alpha-shearlet associated to trafo.Hence, if
coeff = trafo.transform(im), then the following iteration produces the associated scale to each “coefficient image”:for scale, c in zip(scale_gen(trafo), coeff): ...
-
AlphaTransformUtility.image_load(path)¶ Given a ‘.npy’ or ‘.png’ file, this function loads the file and returns its content as a two-dimensional
numpy.ndarrayoffloatvalues.For ‘.png’ images, the pixel values are normalized to be between 0 and 1 (instead of between 0 and 255) and color images are converted to grey-scale.
Required parameter
Parameters: path (string) – Path to the image to be converted, either of a ‘.png’ or ‘.npy’ file. Return value
Returns: The loaded image as a two-dimensional numpy.ndarray.