|
a |
|
b/ants/ops/symmetrize_image.py |
|
|
1 |
|
|
|
2 |
|
|
|
3 |
__all__ = ['symmetrize_image'] |
|
|
4 |
|
|
|
5 |
from tempfile import mktemp |
|
|
6 |
|
|
|
7 |
import ants |
|
|
8 |
from ants.decorators import image_method |
|
|
9 |
|
|
|
10 |
@image_method |
|
|
11 |
def symmetrize_image(image): |
|
|
12 |
""" |
|
|
13 |
Use registration and reflection to make an image symmetric |
|
|
14 |
|
|
|
15 |
ANTsR function: N/A |
|
|
16 |
|
|
|
17 |
Arguments |
|
|
18 |
--------- |
|
|
19 |
image : ANTsImage |
|
|
20 |
image to make symmetric |
|
|
21 |
|
|
|
22 |
Returns |
|
|
23 |
------- |
|
|
24 |
ANTsImage |
|
|
25 |
|
|
|
26 |
Example |
|
|
27 |
------- |
|
|
28 |
>>> import ants |
|
|
29 |
>>> image = ants.image_read( ants.get_ants_data('r16') , 'float') |
|
|
30 |
>>> simage = ants.symimage(image) |
|
|
31 |
""" |
|
|
32 |
imager = ants.reflect_image(image, axis=0) |
|
|
33 |
imageavg = imager * 0.5 + image |
|
|
34 |
for i in range(5): |
|
|
35 |
w1 = ants.registration(imageavg, image, type_of_transform='SyN') |
|
|
36 |
w2 = ants.registration(imageavg, imager, type_of_transform='SyN') |
|
|
37 |
|
|
|
38 |
xavg = w1['warpedmovout']*0.5 + w2['warpedmovout']*0.5 |
|
|
39 |
nada1 = ants.apply_transforms(image, image, w1['fwdtransforms'], compose=w1['fwdtransforms'][0]) |
|
|
40 |
nada2 = ants.apply_transforms(image, image, w2['fwdtransforms'], compose=w2['fwdtransforms'][0]) |
|
|
41 |
|
|
|
42 |
wavg = (ants.image_read(nada1) + ants.image_read(nada2)) * (-0.5) |
|
|
43 |
wavgfn = mktemp(suffix='.nii.gz') |
|
|
44 |
ants.image_write(wavg, wavgfn) |
|
|
45 |
xavg = ants.apply_transforms(image, imageavg, wavgfn) |
|
|
46 |
|
|
|
47 |
return xavg |
|
|
48 |
|
|
|
49 |
|