-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpism_label_components.pyx
53 lines (41 loc) · 1.87 KB
/
pism_label_components.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
# cython: language_level=3, embedsignature=True
# distutils: language = c++
cimport cython
from decl cimport label as cxx_label
def label(input_array, mark_isolated_components, foreground_threshold, attached_threshold, output_array):
"""
Label connected components in `input_array` (array of `short` integers).
In `input_array`, areas where `input_array > foreground_threshold`
are "foreground" pixels that make up components to isolate. If
`mark_isolated_components` is `false`, each component gets a
unique label (an integer).
If `mark_isolated_components` is true, components *not* reachable
from areas satisfying `input_array > attached_threshold` are
labeled with `1`, the rest with `0`.
Does *not* modify background pixels in `output_array`.
"""
cdef int nrows = input_array.shape[0]
cdef int ncols = input_array.shape[1]
cdef short [:,::1] data = input_array
cdef short [:,::1] data_out = output_array
cxx_label(&data[0,0], nrows, ncols, mark_isolated_components, foreground_threshold, attached_threshold, &data_out[0,0])
def update_max_depth(depth, mask, current_depth, max_depth):
"""
Update `max_depth`.
Each call sets `max_depth = max(max_depth, current_depth)` in all
areas where `depth > current_depth` and reachable from the "deep
ocean" according to `mask`.
"""
cdef int nrows = depth.shape[0]
cdef int ncols = depth.shape[1]
cdef short [:,::1] depth_ = depth
cdef short [:,::1] mask_ = mask
cdef short [:,::1] max_depth_ = max_depth
cdef bint reachable = False
cdef short current = current_depth
with cython.boundscheck(False):
for r in range(nrows):
for c in range(ncols):
reachable = depth_[r,c] > current and mask_[r, c] == 0
if reachable:
max_depth_[r, c] = max(max_depth_[r, c], current)