a b/ants/ops/denoise_image.py
1
__all__ = ["denoise_image"]
2
3
4
from ants.decorators import image_method
5
from ants.internal import get_lib_fn, process_arguments
6
7
@image_method
8
def denoise_image(image, mask=None, shrink_factor=1, p=1, r=2, noise_model="Rician", v=0):
9
    """
10
    Denoise an image using a spatially adaptive filter originally described in
11
    J. V. Manjon, P. Coupe, Luis Marti-Bonmati, D. L. Collins, and M. Robles.
12
    Adaptive Non-Local Means Denoising of MR Images With Spatially Varying
13
    Noise Levels, Journal of Magnetic Resonance Imaging, 31:192-203, June 2010.
14
15
    ANTsR function: `denoiseImage`
16
17
    Arguments
18
    ---------
19
    image : ANTsImage
20
        scalar image to denoise.
21
22
    mask : ANTsImage
23
        to limit the denoise region.
24
25
    shrink_factor : scalar
26
        downsampling level performed within the algorithm.
27
28
    p : integer or character of format '2x2' where the x separates vector entries
29
        patch radius for local sample.
30
31
    r : integer or character of format '2x2' where the x separates vector entries
32
        search radius from which to choose extra local samples.
33
34
    noise_model : string
35
        'Rician' or 'Gaussian'
36
37
    Returns
38
    -------
39
    ANTsImage
40
41
    Example
42
    -------
43
    >>> import ants
44
    >>> import numpy as np
45
    >>> image = ants.image_read(ants.get_ants_data('r16'))
46
    >>> # add fairly large salt and pepper noise
47
    >>> imagenoise = image + np.random.randn(*image.shape).astype('float32')*5
48
    >>> imagedenoise = ants.denoise_image(imagenoise, ants.get_mask(image))
49
    """
50
    inpixeltype = image.pixeltype
51
    outimage = image.clone("float")
52
53
    mydim = image.dimension
54
55
    if mask is None:
56
        myargs = {
57
            "d": mydim,
58
            "i": image,
59
            "n": noise_model,
60
            "s": int(shrink_factor),
61
            "p": p,
62
            "r": r,
63
            "o": outimage,
64
            "v": v,
65
        }
66
    else:
67
        myargs = {
68
            "d": mydim,
69
            "i": image,
70
            "n": noise_model,
71
            "x": mask.clone("unsigned char"),
72
            "s": int(shrink_factor),
73
            "p": p,
74
            "r": r,
75
            "o": outimage,
76
            "v": v,
77
        }
78
79
    processed_args = process_arguments(myargs)
80
    libfn = get_lib_fn("DenoiseImage")
81
    libfn(processed_args)
82
    return outimage.clone(inpixeltype)