a b/ants/ops/crop_image.py
1
2
 
3
4
__all__ = ['crop_image', 
5
           'crop_indices',
6
           'decrop_image']
7
8
9
import ants
10
from ants.decorators import image_method
11
from ants.internal import get_lib_fn
12
13
@image_method
14
def crop_image(image, label_image=None, label=1):
15
    """
16
    Use a label image to crop a smaller ANTsImage from within a larger ANTsImage
17
18
    ANTsR function: `cropImage`
19
    
20
    Arguments
21
    ---------
22
    image : ANTsImage  
23
        image to crop
24
    
25
    label_image : ANTsImage
26
        image with label values. If not supplied, estimated from data.
27
    
28
    label : integer   
29
        the label value to use
30
31
    Returns
32
    -------
33
    ANTsImage
34
35
    Example
36
    -------
37
    >>> import ants
38
    >>> fi = ants.image_read( ants.get_ants_data('r16') )
39
    >>> cropped = ants.crop_image(fi)
40
    >>> fi2 = ants.merge_channels([fi,fi])
41
    >>> cropped2 = ants.crop_image(fi2)
42
    >>> cropped = ants.crop_image(fi, fi, 100 )
43
    """
44
    if image.has_components:
45
        return ants.merge_channels([crop_image(img, label_image, label) for img in ants.split_channels(image)],
46
                                   channels_first=image.channels_first)
47
        
48
    inpixeltype = image.pixeltype
49
    ndim = image.dimension
50
    if image.pixeltype != 'float':
51
        image = image.clone('float')
52
53
    if label_image is None:
54
        label_image = ants.get_mask(image)
55
56
    if label_image.pixeltype != 'float':
57
        label_image = label_image.clone('float')
58
59
    libfn = get_lib_fn('cropImage')
60
    itkimage = libfn(image.pointer, label_image.pointer, label, 0, [], [])
61
    return ants.from_pointer(itkimage).clone(inpixeltype)
62
63
64
@image_method
65
def crop_indices(image, lowerind, upperind):
66
    """
67
    Create a proper ANTsImage sub-image by indexing the image with indices. 
68
    This is similar to but different from array sub-setting in that 
69
    the resulting sub-image can be decropped back into its place without 
70
    having to store its original index locations explicitly.
71
    
72
    ANTsR function: `cropIndices`
73
74
    Arguments
75
    ---------
76
    image : ANTsImage  
77
        image to crop
78
    
79
    lowerind : list/tuple of integers  
80
        vector of lower index, should be length image dimensionality
81
    
82
    upperind : list/tuple of integers
83
        vector of upper index, should be length image dimensionality
84
    
85
    Returns
86
    -------
87
    ANTsImage
88
89
    Example
90
    -------
91
    >>> import ants
92
    >>> fi = ants.image_read( ants.get_ants_data("r16"))
93
    >>> cropped = ants.crop_indices( fi, (10,10), (100,100) )
94
    >>> cropped = ants.smooth_image( cropped, 5 )
95
    >>> decropped = ants.decrop_image( cropped, fi )
96
    """
97
    if image.has_components:
98
        return ants.merge_channels([crop_indices(img, lowerind, upperind) for img in ants.split_channels(image)],
99
                                   channels_first=image.channels_first)
100
        
101
    inpixeltype = 'float'
102
    if image.pixeltype != 'float':
103
        inpixeltype = image.pixeltype
104
        image = image.clone('float')
105
106
    if (image.dimension != len(lowerind)) or (image.dimension != len(upperind)):
107
        raise ValueError('image dimensionality and index length must match')
108
109
    libfn = get_lib_fn('cropImage')
110
    itkimage = libfn(image.pointer, image.pointer, 1, 2, lowerind, upperind)
111
    ants_image = ants.from_pointer(itkimage)
112
    if inpixeltype != 'float':
113
        ants_image = ants_image.clone(inpixeltype)
114
    return ants_image
115
116
@image_method
117
def decrop_image(cropped_image, full_image):
118
    """
119
    The inverse function for `ants.crop_image`
120
121
    ANTsR function: `decropImage`
122
    
123
    Arguments
124
    ---------
125
    cropped_image : ANTsImage
126
        cropped image
127
128
    full_image : ANTsImage
129
        image in which the cropped image will be put back
130
131
    Returns
132
    -------
133
    ANTsImage
134
135
    Example
136
    -------
137
    >>> import ants
138
    >>> fi = ants.image_read(ants.get_ants_data('r16'))
139
    >>> mask = ants.get_mask(fi)
140
    >>> cropped = ants.crop_image(fi, mask, 1)
141
    >>> cropped = ants.smooth_image(cropped, 1)
142
    >>> decropped = ants.decrop_image(cropped, fi)
143
    """
144
    inpixeltype = 'float'
145
    if cropped_image.pixeltype != 'float':
146
        inpixeltype= cropped_image.pixeltype
147
        cropped_image = cropped_image.clone('float')
148
    if full_image.pixeltype != 'float':
149
        full_image = full_image.clone('float')
150
    
151
    libfn = get_lib_fn('cropImage')
152
    itkimage = libfn(cropped_image.pointer, full_image.pointer, 1, 1, [], [])
153
    ants_image = ants.from_pointer(itkimage)
154
    if inpixeltype != 'float':
155
        ants_image = ants_image.clone(inpixeltype)
156
157
    return ants_image
158