-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdemons_mind_2d_cpp.py
123 lines (107 loc) · 4.21 KB
/
demons_mind_2d_cpp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import sys
import os
import platform
src_path = os.path.abspath(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, src_path)
import numpy as np
from matplotlib import pyplot as plt
from numpy.ctypeslib import ndpointer
import ctypes
current_path = os.path.abspath(os.path.dirname(__file__))
if platform.system().lower() == "linux":
libs_path = os.path.join(current_path, "libs", "unix")
elif platform.system().lower() == "windows":
raise ValueError("Windows not supported yet.")
else:
raise ValueError("Not supported OS.")
if platform.system().lower() == "linux":
library_path = os.path.join(libs_path, "demons_mind_2d.so")
elif platform.system().lower() == "windows":
raise ValueError("Windows not supported yet.")
else:
raise ValueError("Not supported OS.")
lib = ctypes.cdll.LoadLibrary(library_path)
fun = lib.demons_mind_2d_so
fun.restype = None
fun.argtypes = [
ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"),
ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"),
ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"),
ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"),
ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"),
ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"),
ctypes.c_int, ctypes.c_int,
ctypes.c_int,
ndpointer(ctypes.c_int, flags="C_CONTIGUOUS"),
ctypes.c_int,
ctypes.c_double, ctypes.c_double,
ctypes.c_double, ctypes.c_double,
ctypes.c_double, ctypes.c_double,
ctypes.c_int, ctypes.c_int,
ctypes.c_double, ctypes.c_double,
ctypes.c_int, ctypes.c_double,
ctypes.c_char_p, ctypes.c_char_p,
ctypes.c_bool,
ctypes.c_bool, ctypes.c_bool,
]
def demons_mind_registration(source, target, spacing, update_mode="addition", gradient_mode="symmetric",
resolutions=1, early_stop=0, diffusion_sigma=(1.0, 1.0), fluid_sigma=(1.0, 1.0), mind_radius=(1, 1),
mind_sigma=(1.0, 1.0), max_iterations=10, tolerance=None,
initial_u_x=None, initial_u_y=None, echo=True, return_best=False, iterations=None):
if (source.shape != target.shape):
raise ValueError("Source and target must have the same shape.")
y_size, x_size = np.shape(source)
grid_x, grid_y = np.meshgrid(np.arange(x_size), np.arange(y_size))
grid_x = grid_x.astype(np.float64)
grid_y = grid_y.astype(np.float64)
es = int(early_stop)
spacing_x, spacing_y = spacing
dx, dy = diffusion_sigma
fx, fy = fluid_sigma
mx, my = mind_sigma
mrx, mry = mind_radius
mrx = int(mrx)
mry = int(mry)
u_mode = update_mode.encode('utf-8')
g_mode = gradient_mode.encode('utf-8')
if iterations is None:
iters = np.ascontiguousarray(np.repeat(max_iterations, resolutions)).astype(np.int32)
else:
iters = np.ascontiguousarray(iterations).astype(np.int32)
if tolerance == None:
tolerance = 0
if initial_u_x == None or initial_u_y == None:
i_dfx = np.ascontiguousarray((np.zeros(np.shape(source)) + grid_x).astype(np.float64))
i_dfy = np.ascontiguousarray((np.zeros(np.shape(source)) + grid_y).astype(np.float64))
use_init = False
else:
i_dfx = np.ascontiguousarray(initial_u_x.astype(np.float64) + grid_x)
i_dfy = np.ascontiguousarray(initial_u_y.astype(np.float64) + grid_y)
use_init = True
df_x = np.ascontiguousarray(np.zeros(np.shape(source), dtype=np.float64))
df_y = np.ascontiguousarray(np.zeros(np.shape(source), dtype=np.float64))
src = np.ascontiguousarray(source.astype(np.float64))
trg = np.ascontiguousarray(target.astype(np.float64))
if platform.system().lower() == "linux":
fun(
src, trg,
i_dfx, i_dfy,
df_x, df_y,
x_size, y_size,
resolutions,
iters,
es,
fx, fy,
dx, dy,
mx, my,
mrx, mry,
spacing_x, spacing_y,
max_iterations, tolerance,
u_mode, g_mode,
echo,
return_best, use_init)
elif platform.system().lower() == "windows":
raise ValueError("Windows not supported yet.")
else:
raise ValueError("Not supported OS.")
return df_x - grid_x, df_y - grid_y