-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
_convex_hull.pyx
74 lines (56 loc) · 2.1 KB
/
_convex_hull.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
66
67
68
69
70
71
72
73
74
#cython: cdivision=True
#cython: boundscheck=False
#cython: nonecheck=False
#cython: wraparound=False
import numpy as np
cimport numpy as cnp
cnp.import_array()
def possible_hull(cnp.uint8_t[:, ::1] img):
"""Return positions of pixels that possibly belong to the convex hull.
Parameters
----------
img : ndarray of bool
Binary input image.
Returns
-------
coords : ndarray (cols, 2)
The ``(row, column)`` coordinates of all pixels that possibly belong to
the convex hull.
"""
cdef Py_ssize_t r, c
cdef Py_ssize_t rows = img.shape[0]
cdef Py_ssize_t cols = img.shape[1]
# Output: rows storage slots for left boundary pixels
# cols storage slots for top boundary pixels
# rows storage slots for right boundary pixels
# cols storage slots for bottom boundary pixels
coords = np.ones((2 * (rows + cols), 2), dtype=np.intp)
coords *= -1
cdef Py_ssize_t[:, ::1] nonzero = coords
cdef Py_ssize_t rows_cols = rows + cols
cdef Py_ssize_t rows_2_cols = 2 * rows + cols
cdef Py_ssize_t rows_cols_r, rows_c
with nogil:
for r in range(rows):
rows_cols_r = rows_cols + r
for c in range(cols):
if img[r, c] != 0:
rows_c = rows + c
rows_2_cols_c = rows_2_cols + c
# Left check
if nonzero[r, 1] == -1:
nonzero[r, 0] = r
nonzero[r, 1] = c
# Right check
elif nonzero[rows_cols_r, 1] < c:
nonzero[rows_cols_r, 0] = r
nonzero[rows_cols_r, 1] = c
# Top check
if nonzero[rows_c, 1] == -1:
nonzero[rows_c, 0] = r
nonzero[rows_c, 1] = c
# Bottom check
elif nonzero[rows_2_cols_c, 0] < r:
nonzero[rows_2_cols_c, 0] = r
nonzero[rows_2_cols_c, 1] = c
return coords[coords[:, 0] != -1]