forked from MWod/ANHIR_MW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mind_2d.py
53 lines (45 loc) · 1.69 KB
/
mind_2d.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
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, "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.mind_ssd
fun.restype = ctypes.c_double
fun.argtypes = [
ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"),
ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"),
ctypes.c_int, ctypes.c_int,
ctypes.c_int, ctypes.c_int,
ctypes.c_double, ctypes.c_double,
]
def mind_ssd(source, target, **args):
if np.shape(source) != np.shape(target):
raise ValueError("Arrays must have the same shape.")
radius = args['radius']
sigma = args['sigma']
src = np.ascontiguousarray(source.astype(np.float64))
trg = np.ascontiguousarray(target.astype(np.float64))
y_size, x_size = np.shape(source)
x_size, y_size = int(x_size), int(y_size)
radius_x, radius_y = int(radius[1]), int(radius[0])
sigma_x, sigma_y = float(sigma[1]), float(sigma[0])
result = fun(source, target, x_size, y_size, radius_x, radius_y, sigma_x, sigma_y)
return result