|
a |
|
b/ants/registration/affine_initializer.py |
|
|
1 |
|
|
|
2 |
__all__ = ['affine_initializer'] |
|
|
3 |
|
|
|
4 |
import warnings |
|
|
5 |
from tempfile import mktemp |
|
|
6 |
|
|
|
7 |
from ants.internal import get_lib_fn, process_arguments |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
def affine_initializer(fixed_image, moving_image, search_factor=20, |
|
|
11 |
radian_fraction=0.1, use_principal_axis=False, |
|
|
12 |
local_search_iterations=10, mask=None, txfn=None ): |
|
|
13 |
""" |
|
|
14 |
A multi-start optimizer for affine registration |
|
|
15 |
Searches over the sphere to find a good initialization for further |
|
|
16 |
registration refinement, if needed. This is a wrapper for the ANTs |
|
|
17 |
function antsAffineInitializer. |
|
|
18 |
|
|
|
19 |
ANTsR function: `affineInitializer` |
|
|
20 |
|
|
|
21 |
Arguments |
|
|
22 |
--------- |
|
|
23 |
fixed_image : ANTsImage |
|
|
24 |
the fixed reference image |
|
|
25 |
moving_image : ANTsImage |
|
|
26 |
the moving image to be mapped to the fixed space |
|
|
27 |
search_factor : scalar |
|
|
28 |
degree of increments on the sphere to search |
|
|
29 |
radian_fraction : scalar |
|
|
30 |
between zero and one, defines the arc to search over |
|
|
31 |
use_principal_axis : boolean |
|
|
32 |
boolean to initialize by principal axis |
|
|
33 |
local_search_iterations : scalar |
|
|
34 |
gradient descent iterations |
|
|
35 |
mask : ANTsImage (optional) |
|
|
36 |
optional mask to restrict registration |
|
|
37 |
txfn : string (optional) |
|
|
38 |
filename for the transformation |
|
|
39 |
|
|
|
40 |
Returns |
|
|
41 |
------- |
|
|
42 |
ndarray |
|
|
43 |
transformation matrix |
|
|
44 |
|
|
|
45 |
Example |
|
|
46 |
------- |
|
|
47 |
>>> import ants |
|
|
48 |
>>> fi = ants.image_read(ants.get_ants_data('r16')) |
|
|
49 |
>>> mi = ants.image_read(ants.get_ants_data('r27')) |
|
|
50 |
>>> txfile = ants.affine_initializer( fi, mi ) |
|
|
51 |
>>> tx = ants.read_transform(txfile, dimension=2) |
|
|
52 |
""" |
|
|
53 |
|
|
|
54 |
if txfn is None: |
|
|
55 |
txfn = mktemp(suffix='.mat') |
|
|
56 |
|
|
|
57 |
veccer = [fixed_image.dimension, fixed_image, moving_image, txfn, |
|
|
58 |
search_factor, radian_fraction, int(use_principal_axis), |
|
|
59 |
local_search_iterations] |
|
|
60 |
if mask is not None: |
|
|
61 |
veccer.append(mask) |
|
|
62 |
|
|
|
63 |
xxx = process_arguments(veccer) |
|
|
64 |
libfn = get_lib_fn('antsAffineInitializer') |
|
|
65 |
retval = libfn(xxx) |
|
|
66 |
|
|
|
67 |
if retval != 0: |
|
|
68 |
warnings.warn('ERROR: Non-zero exit status!') |
|
|
69 |
|
|
|
70 |
return txfn |