|
a |
|
b/ants/ops/iMath.py |
|
|
1 |
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
__all__ = ['iMath', |
|
|
5 |
'image_math', |
|
|
6 |
'multiply_images', |
|
|
7 |
'iMath_get_largest_component', |
|
|
8 |
'iMath_normalize', |
|
|
9 |
'iMath_truncate_intensity', |
|
|
10 |
'iMath_sharpen', |
|
|
11 |
'iMath_pad', |
|
|
12 |
'iMath_maurer_distance', |
|
|
13 |
'iMath_perona_malik', |
|
|
14 |
'iMath_grad', |
|
|
15 |
'iMath_laplacian', |
|
|
16 |
'iMath_canny', |
|
|
17 |
'iMath_histogram_equalization', |
|
|
18 |
'iMath_MD', |
|
|
19 |
'iMath_ME', |
|
|
20 |
'iMath_MO', |
|
|
21 |
'iMath_MC', |
|
|
22 |
'iMath_GD', |
|
|
23 |
'iMath_GE', |
|
|
24 |
'iMath_GO', |
|
|
25 |
'iMath_GC', |
|
|
26 |
'iMath_fill_holes', |
|
|
27 |
'iMath_get_largest_component', |
|
|
28 |
'iMath_normalize', |
|
|
29 |
'iMath_truncate_intensity', |
|
|
30 |
'iMath_sharpen', |
|
|
31 |
'iMath_propagate_labels_through_mask'] |
|
|
32 |
|
|
|
33 |
from ants.internal import get_lib_fn, process_arguments |
|
|
34 |
from ants.decorators import image_method |
|
|
35 |
|
|
|
36 |
_iMathOps = {'FillHoles', |
|
|
37 |
'GetLargestComponent', |
|
|
38 |
'Normalize', |
|
|
39 |
'Sharpen', |
|
|
40 |
'Pad', |
|
|
41 |
'D', |
|
|
42 |
'MaurerDistance', |
|
|
43 |
'PeronaMalik', |
|
|
44 |
'Grad', |
|
|
45 |
'Laplacian', |
|
|
46 |
'Canny', |
|
|
47 |
'HistogramEqualization', |
|
|
48 |
'MD', |
|
|
49 |
'ME', |
|
|
50 |
'MO', |
|
|
51 |
'MC', |
|
|
52 |
'GD', |
|
|
53 |
'GE', |
|
|
54 |
'GO', |
|
|
55 |
'GC', |
|
|
56 |
'FillHoles', |
|
|
57 |
'GetLargestComponent', |
|
|
58 |
'LabelStats', |
|
|
59 |
'Normalize', |
|
|
60 |
'TruncateIntensity', |
|
|
61 |
'Sharpen', |
|
|
62 |
'PropagateLabelsThroughMask'} |
|
|
63 |
|
|
|
64 |
@image_method |
|
|
65 |
def multiply_images(image1, image2): |
|
|
66 |
return image1 * image2 |
|
|
67 |
|
|
|
68 |
|
|
|
69 |
@image_method |
|
|
70 |
def iMath(image, operation, *args): |
|
|
71 |
""" |
|
|
72 |
Perform various (often mathematical) operations on the input image/s. |
|
|
73 |
Additional parameters should be specific for each operation. |
|
|
74 |
See the the full iMath in ANTs, on which this function is based. |
|
|
75 |
|
|
|
76 |
ANTsR function: `iMath` |
|
|
77 |
|
|
|
78 |
Arguments |
|
|
79 |
--------- |
|
|
80 |
image : ANTsImage |
|
|
81 |
input object, usually antsImage |
|
|
82 |
|
|
|
83 |
operation |
|
|
84 |
a string e.g. "GetLargestComponent" ... the special case of "GetOperations" |
|
|
85 |
or "GetOperationsFull" will return a list of operations and brief |
|
|
86 |
description. Some operations may not be valid (WIP), but most are. |
|
|
87 |
|
|
|
88 |
*args : non-keyword arguments |
|
|
89 |
additional parameters specific to the operation |
|
|
90 |
|
|
|
91 |
Example |
|
|
92 |
------- |
|
|
93 |
>>> import ants |
|
|
94 |
>>> img = ants.image_read(ants.get_ants_data('r16')) |
|
|
95 |
>>> img2 = ants.iMath(img, 'Canny', 1, 5, 12) |
|
|
96 |
""" |
|
|
97 |
if operation not in _iMathOps: |
|
|
98 |
raise ValueError('Operation not recognized') |
|
|
99 |
|
|
|
100 |
imagedim = image.dimension |
|
|
101 |
outimage = image.clone() |
|
|
102 |
args = [imagedim, outimage, operation, image] + [a for a in args] |
|
|
103 |
processed_args = process_arguments(args) |
|
|
104 |
|
|
|
105 |
libfn = get_lib_fn('iMath') |
|
|
106 |
libfn(processed_args) |
|
|
107 |
return outimage |
|
|
108 |
image_math = iMath |
|
|
109 |
|
|
|
110 |
|
|
|
111 |
def iMath_ops(): |
|
|
112 |
return _iMathOps |
|
|
113 |
|
|
|
114 |
@image_method |
|
|
115 |
def iMath_canny(image, sigma, lower, upper): |
|
|
116 |
return iMath(image, 'Canny', sigma, lower, upper) |
|
|
117 |
|
|
|
118 |
@image_method |
|
|
119 |
def iMath_fill_holes(image, hole_type=2): |
|
|
120 |
return iMath(image, 'FillHoles', hole_type) |
|
|
121 |
|
|
|
122 |
@image_method |
|
|
123 |
def iMath_GC(image, radius=1): |
|
|
124 |
return iMath(image, 'GC', radius) |
|
|
125 |
|
|
|
126 |
@image_method |
|
|
127 |
def iMath_GD(image, radius=1): |
|
|
128 |
return iMath(image, 'GD', radius) |
|
|
129 |
|
|
|
130 |
@image_method |
|
|
131 |
def iMath_GE(image, radius=1): |
|
|
132 |
return iMath(image, 'GE', radius) |
|
|
133 |
|
|
|
134 |
@image_method |
|
|
135 |
def iMath_GO(image, radius=1): |
|
|
136 |
return iMath(image, 'GO', radius) |
|
|
137 |
|
|
|
138 |
@image_method |
|
|
139 |
def iMath_get_largest_component(image, min_size=50): |
|
|
140 |
return iMath(image, 'GetLargestComponent', min_size) |
|
|
141 |
|
|
|
142 |
@image_method |
|
|
143 |
def iMath_grad(image, sigma=0.5, normalize=False): |
|
|
144 |
return iMath(image, 'Grad', sigma, normalize) |
|
|
145 |
|
|
|
146 |
@image_method |
|
|
147 |
def iMath_histogram_equalization(image, alpha, beta): |
|
|
148 |
return iMath(image, 'HistogramEqualization', alpha, beta) |
|
|
149 |
|
|
|
150 |
@image_method |
|
|
151 |
def iMath_laplacian(image, sigma=0.5, normalize=False): |
|
|
152 |
return iMath(image, 'Laplacian', sigma, normalize) |
|
|
153 |
|
|
|
154 |
@image_method |
|
|
155 |
def iMath_MC(image, radius=1, value=1, shape=1, parametric=False, lines=3, thickness=1, include_center=False): |
|
|
156 |
return iMath(image, 'MC', radius, value, shape, parametric, lines, thickness, include_center) |
|
|
157 |
|
|
|
158 |
@image_method |
|
|
159 |
def iMath_MD(image, radius=1, value=1, shape=1, parametric=False, lines=3, thickness=1, include_center=False): |
|
|
160 |
return iMath(image, 'MD', radius, value, shape, parametric, lines, thickness, include_center) |
|
|
161 |
|
|
|
162 |
@image_method |
|
|
163 |
def iMath_ME(image, radius=1, value=1, shape=1, parametric=False, lines=3, thickness=1, include_center=False): |
|
|
164 |
return iMath(image, 'ME', radius, value, shape, parametric, lines, thickness, include_center) |
|
|
165 |
|
|
|
166 |
@image_method |
|
|
167 |
def iMath_MO(image, radius=1, value=1, shape=1, parametric=False, lines=3, thickness=1, include_center=False): |
|
|
168 |
return iMath(image, 'MO', radius, value, shape, parametric, lines, thickness, include_center) |
|
|
169 |
|
|
|
170 |
@image_method |
|
|
171 |
def iMath_maurer_distance(image, foreground=1): |
|
|
172 |
return iMath(image, 'MaurerDistance', foreground) |
|
|
173 |
|
|
|
174 |
@image_method |
|
|
175 |
def iMath_normalize(image): |
|
|
176 |
return iMath(image, 'Normalize') |
|
|
177 |
|
|
|
178 |
@image_method |
|
|
179 |
def iMath_pad(image, padding): |
|
|
180 |
return iMath(image, 'Pad', padding) |
|
|
181 |
|
|
|
182 |
@image_method |
|
|
183 |
def iMath_perona_malik(image, conductance=0.25, n_iterations=1): |
|
|
184 |
return iMath(image, 'PeronaMalik', conductance, n_iterations) |
|
|
185 |
|
|
|
186 |
@image_method |
|
|
187 |
def iMath_sharpen(image): |
|
|
188 |
return iMath(image, 'Sharpen') |
|
|
189 |
|
|
|
190 |
@image_method |
|
|
191 |
def iMath_propagate_labels_through_mask(image, labels, stopping_value=100, propagation_method=0): |
|
|
192 |
""" |
|
|
193 |
>>> import ants |
|
|
194 |
>>> wms = ants.image_read('~/desktop/wms.nii.gz') |
|
|
195 |
>>> thal = ants.image_read('~/desktop/thal.nii.gz') |
|
|
196 |
>>> img2 = ants.iMath_propagate_labels_through_mask(wms, thal, 500, 0) |
|
|
197 |
""" |
|
|
198 |
return iMath(image, 'PropagateLabelsThroughMask', labels, stopping_value, propagation_method) |
|
|
199 |
|
|
|
200 |
@image_method |
|
|
201 |
def iMath_truncate_intensity(image, lower_q, upper_q, n_bins=64): |
|
|
202 |
""" |
|
|
203 |
>>> import ants |
|
|
204 |
>>> img = ants.image_read(ants.get_ants_data('r16')) |
|
|
205 |
>>> ants.iMath_truncate_intensity( img, 0.2, 0.8 ) |
|
|
206 |
""" |
|
|
207 |
return iMath(image, 'TruncateIntensity', lower_q, upper_q, n_bins ) |