Diff of /ants/math/averaging.py [000000] .. [5d12a0]

Switch to unified view

a b/ants/math/averaging.py
1
import os
2
from tempfile import mktemp
3
4
import numpy as np
5
6
import ants
7
8
__all__ = ['average_images']
9
10
11
def average_images( x, normalize=True, mask=None, imagetype=0, sum_image_threshold=3, return_sum_image=False, verbose=False ):
12
    """
13
    average a list of images
14
15
    images will be resampled automatically to the largest image space;
16
    this is not a registration so images should be in the same physical
17
    space to begin with.
18
19
    x : a list containing either filenames or antsImages 
20
21
    normalize : boolean
22
23
    mask : None or integer; this will perform a masked averaging which can 
24
        be useful when images have only partial coverage. integer greater 
25
        than zero will perform morphological closing.
26
27
    imagetype : integer
28
        choose 0/1/2/3 mapping to scalar/vector/tensor/time-series
29
30
    sum_image_threshold : integer
31
        only average regions with overlap greater than or equal to this value
32
33
    return_sum_image : boolean
34
        returns the average and the image that show ROI overlap; primarily for debugging
35
36
    verbose : boolean
37
        will print progress
38
39
    Returns
40
    -------
41
    ANTsImage
42
43
    Example
44
    -------
45
    >>> import ants
46
    >>> x0=[ ants.get_data('r16'), ants.get_data('r27'), ants.get_data('r62'), ants.get_data('r64') ]
47
    >>> x1=[]
48
    >>> for k in range(len(x0)):
49
    >>>     x1.append( ants.image_read( x0[k] ) )
50
    >>> avg=ants.average_images(x0)
51
    >>> avg1=ants.average_images(x1)
52
    >>> avg2=ants.average_images(x1,mask=0)
53
    >>> avg3=ants.average_images(x1,mask=1,normalize=True)
54
    """
55
    import numpy as np
56
57
    def gli( y, normalize=False ):
58
        if isinstance(y,str):
59
            y=ants.image_read(y)
60
        if normalize:
61
            y=y/y.mean()
62
        return y
63
64
    biggest=0
65
    biggestind=0
66
    for k in range( len( x ) ):
67
        locimg = gli( x[k], False )
68
        sz=np.prod( locimg.shape )
69
        if sz > biggest:
70
            biggest=sz
71
            biggestind=k
72
73
    avg = gli( x[biggestind], False ) * 0
74
    scl = float( 1.0 / len(x))
75
    if mask is not None:
76
        sumimg = gli( x[biggestind], False ) * 0
77
78
    for k in range( len( x ) ):
79
        if verbose and k % 20 == 0:
80
            print( str(k)+'...', end='',flush=True)
81
        locimg = gli( x[k], normalize )
82
        temp = ants.resample_image_to_target( locimg, avg, interp_type='linear', imagetype=imagetype )
83
        avg = avg + temp
84
        if mask is not None:
85
            fgmask = ants.threshold_image(temp,'Otsu',1)
86
            if mask > 0:
87
                fgmask = ants.morphology(fgmask,"close",mask)
88
            sumimg = sumimg + fgmask
89
90
    if return_sum_image:
91
        return avg * scl, sumimg
92
    if mask is None:
93
        avg = avg * scl
94
    else:
95
        nonzero = sumimg > sum_image_threshold
96
        tozero = sumimg <= sum_image_threshold
97
        avg[nonzero] = avg[nonzero] / sumimg[nonzero]
98
        avg[tozero] = 0
99
    return avg        
100