8000 MAINT: Declare flag values as constants · scikit-image/scikit-image@08da763 · GitHub
[go: up one dir, main page]

Skip to content

Commit 08da763

Browse files
committed
MAINT: Declare flag values as constants
Unfortunately Cython doesn't seem to support the import of variables declared in the global space of the PYX file.
1 parent 5ca5a17 commit 08da763

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

skimage/morphology/_extrema_cy.pyx

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ ctypedef fused dtype_t:
2020
cnp.float64_t
2121

2222

23+
# Definition of flag values used for `flags` in _local_maxima & _fill_plateau
24+
cdef:
25+
# First or last index in a dimension
26+
unsigned char BORDER_INDEX = 3
27+
# Potentially part of a maximum
28+
unsigned char MAYBE_MAXIMUM = 2
29+
# Index was queued (flood-fill) and might still be part of maximum
30+
unsigned char QUEUED_MAYBE_MAXIMUM = 1
31+
# None of the above is true
32+
unsigned char NOT_MAXIMUM = 0
33+
34+
2335
def _local_maxima(dtype_t[::1] image not None,
2436
unsigned char[::1] flags,
2537
Py_ssize_t[::1] neighbor_offsets not None):
@@ -46,12 +58,6 @@ def _local_maxima(dtype_t[::1] image not None,
4658
Py_ssize_t i, i_max, i_ahead
4759
unsigned char prefilter
4860

49-
# Current flag meanings:
50-
# 3 - first or last index in a dimension
51-
# 2 - potentially part of a maximum
52-
# 1 - not used in first loop
53-
# 0 - not evaluated or none of the above is true
54-
5561
# Prefilter candidates only if neighbors in last dimension are part of
5662
# the structuring element
5763
prefilter = -1 in neighbor_offsets and 1 in neighbor_offsets
@@ -61,32 +67,35 @@ def _local_maxima(dtype_t[::1] image not None,
6167

6268
if prefilter:
6369
while i < i_max:
64-
if image[i - 1] < image[i] and flags[i] != 3:
70+
if image[i - 1] < image[i] and flags[i] != BORDER_INDEX:
6571
# Potential maximum (in last dimension) is found, find
6672
# other edge of current plateau or "edge of dimension"
6773
i_ahead = i + 1
68-
while image[i] == image[i_ahead] and flags[i_ahead] != 3:
74+
while (
75+
image[i] == image[i_ahead] and
76+
flags[i_ahead] != BORDER_INDEX
77+
):
6978
i_ahead += 1
7079
if image[i] > image[i_ahead]:
7180
# Found local maximum (in one dimension), mark all
7281
# parts of the plateau as potential maximum
73-
flags[i:i_ahead] = 2
82+
flags[i:i_ahead] = MAYBE_MAXIMUM
7483
i = i_ahead
7584
else:
7685
i += 1
7786

7887
else: # Skip prefiltering and flag entire array as potential maximum
7988
while i < i_max:
80-
if flags[i] != 3:
81-
flags[i] = 2
89+
if flags[i] != BORDER_INDEX:
90+
flags[i] = MAYBE_MAXIMUM
8291
i += 1
8392

8493
# Initialize a buffer used to queue positions while evaluating each
8594
# potential maximum (flagged with 2)
8695
queue_init(&queue, 64)
8796
try:
8897
for i in range(image.shape[0]):
89-
if flags[i] == 2:
98+
if flags[i] == MAYBE_MAXIMUM:
9099
# Index is potentially part of a maximum:
91100
# Find all samples part of the plateau and fill with 0
92101
# or 1 depending on whether it's a true maximum
@@ -124,13 +133,7 @@ cdef inline void _fill_plateau(
124133
h = image[start_index]
125134
true_maximum = 1 # Boolean flag
126135

127-
# Current / new flag meanings:
128-
# 3 - first or last value in a dimension
129-
# 2 - not used here
130-
# 1 - index was queued and might still be part of maximum
131-
# 0 - none of the above is true
132-
# Therefore mark current index as true maximum as an initial guess
133-
flags[start_index] = 1
136+
flags[start_index] = QUEUED_MAYBE_MAXIMUM
134137

135138
# And queue start position after clearing the buffer
136139
queue_clear(queue_ptr)
@@ -144,21 +147,21 @@ cdef inline void _fill_plateau(
144147

145148
if image[neighbor] == h:
146149
# Value is part of plateau
147-
if flags[neighbor] == 3:
150+
if flags[neighbor] == BORDER_INDEX:
148151
# Plateau touches border and can't be maximum
149-
true_maximum = 0
150-
elif flags[neighbor] != 1:
152+
true_maximum = NOT_MAXIMUM
153+
elif flags[neighbor] != QUEUED_MAYBE_MAXIMUM:
151154
# Index wasn't queued already, do so now
152155
queue_push(queue_ptr, &neighbor)
153-
flags[neighbor] = 1
156+
flags[neighbor] = QUEUED_MAYBE_MAXIMUM
154157

155158
elif image[neighbor] > h:
156159
# Current plateau can't be maximum because it borders a
157160
# larger one
158-
true_maximum = 0
161+
true_maximum = NOT_MAXIMUM
159162

160163
if not true_maximum:
161164
queue_restore(queue_ptr)
162165
# Initial guess was wrong -> replace 1 with 0 for plateau
163166
while queue_pop(queue_ptr, &neighbor):
164-
flags[neighbor] = 0
167+
flags[neighbor] = NOT_MAXIMUM

skimage/morphology/extrema.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,7 @@ def local_maxima(image, selem=None, indices=False, include_border=True):
441441
neighbor_offsets = _offset_to_raveled_neighbours(image.shape, selem)
442442

443443
# Array of flags used to store the state of each pixel during evaluation.
444-
# Possible states are:
445-
# 3 - first or last value in a dimension
446-
# 2 - potentially part of a maximum
447-
# 1 - evaluated and part of a maximum
444+
# See _extrema_cy.pyx for their meaning
448445
flags = np.zeros(image.shape, dtype=np.uint8)
449446
_set_edge_values_inplace(flags, value=3)
450447

0 commit comments

Comments
 (0)
0