-
Notifications
You must be signed in to change notification settings - Fork 2
/
interpolation.pyx
65 lines (49 loc) · 2.49 KB
/
interpolation.pyx
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
cimport numpy as cnp
import numpy as np
cdef extern from "_bilinear.h":
void _interpolation_simd(
const float *image, const int image_width,
const float *coordinates, const int n_coordinates,
float *intensities);
void _interpolation_normal(
const float *image, const int image_width,
const float *coordinates, const int n_coordinates,
float *intensities);
def interpolation_simd(cnp.ndarray[cnp.float32_t, ndim=2] image,
cnp.ndarray[cnp.float32_t, ndim=2] coordinates):
cdef int N = coordinates.shape[0]
cdef float[:] image_view = image.flatten()
cdef int width = image.shape[1]
cdef float[:] coordinates_view = coordinates.flatten()
intensities = np.empty(N, dtype=np.float32)
cdef float[:] intensities_view = intensities
_interpolation_simd(&image_view[0], width, &coordinates_view[0], N,
&intensities_view[0])
return intensities
def interpolation_normal(cnp.ndarray[cnp.float32_t, ndim=2] image,
cnp.ndarray[cnp.float32_t, ndim=2] coordinates):
cdef int N = coordinates.shape[0]
cdef float[:] image_view = image.flatten()
cdef int width = image.shape[1]
cdef float[:] coordinates_view = coordinates.flatten()
intensities = np.empty(N, dtype=np.float32)
cdef float[:] intensities_view = intensities
_interpolation_normal(&image_view[0], width, &coordinates_view[0], N,
&intensities_view[0])
return intensities
def interpolation_cython(cnp.ndarray[cnp.float32_t, ndim=2] image,
cnp.ndarray[cnp.float32_t, ndim=2] coordinates):
cdef cnp.ndarray[cnp.float32_t, ndim=1] cx = coordinates[:, 0]
cdef cnp.ndarray[cnp.float32_t, ndim=1] cy = coordinates[:, 1]
cdef cnp.ndarray[cnp.float32_t, ndim=1] lx = np.floor(cx)
cdef cnp.ndarray[cnp.float32_t, ndim=1] ly = np.floor(cy)
cdef cnp.ndarray[cnp.float32_t, ndim=1] ux = lx + 1.0
cdef cnp.ndarray[cnp.float32_t, ndim=1] uy = ly + 1.0
cdef cnp.ndarray[cnp.int64_t, ndim=1] lxi = lx.astype(np.int64)
cdef cnp.ndarray[cnp.int64_t, ndim=1] lyi = ly.astype(np.int64)
cdef cnp.ndarray[cnp.int64_t, ndim=1] uxi = ux.astype(np.int64)
cdef cnp.ndarray[cnp.int64_t, ndim=1] uyi = uy.astype(np.int64)
return (image[lyi, lxi] * (ux - cx) * (uy - cy) +
image[lyi, uxi] * (cx - lx) * (uy - cy) +
image[uyi, lxi] * (ux - cx) * (cy - ly) +
image[uyi, uxi] * (cx - lx) * (cy - ly))