|
| 1 | +#cython: cdivision=True |
| 2 | +#cython: boundscheck=False |
| 3 | +#cython: nonecheck=False |
| 4 | +#cython: wraparound=False |
| 5 | + |
| 6 | +"""Cython code used in extrema.py.""" |
| 7 | + |
| 8 | +cimport numpy as cnp |
| 9 | + |
| 10 | + |
| 11 | +# Must be defined to use QueueWithHistory |
| 12 | +ctypedef Py_ssize_t QueueItem |
| 13 | + |
| 14 | +include "_queue_with_history.pxi" |
| 15 | + |
| 16 | + |
| 17 | +ctypedef fused dtype_t: |
| 18 | + cnp.uint8_t |
| 19 | + cnp.uint16_t |
| 20 | + cnp.uint32_t |
| 21 | + cnp.uint64_t |
| 22 | + cnp.int8_t |
| 23 | + cnp.int16_t |
| 24 | + cnp.int32_t |
| 25 | + cnp.int64_t |
| 26 | + cnp.float32_t
F438
|
| 27 | + cnp.float64_t |
| 28 | + |
| 29 | + |
| 30 | +# Definition of flag values used for `flags` in _local_maxima & _fill_plateau |
| 31 | +cdef: |
| 32 | + # First or last index in a dimension |
| 33 | + unsigned char BORDER_INDEX = 3 |
| 34 | + # Potentially part of a maximum |
| 35 | + unsigned char CANDIDATE = 2 |
| 36 | + # Index was queued (flood-fill) and might still be part of maximum OR |
| 37 | + # when evaluation is complete this flag value marks definite local maxima |
| 38 | + unsigned char QUEUED_CANDIDATE = 1 |
| 39 | + # None of the above is true |
| 40 | + unsigned char NOT_MAXIMUM = 0 |
| 41 | + |
| 42 | + |
| 43 | +def _local_maxima(dtype_t[::1] image not None, |
| 44 | + unsigned char[::1] flags, |
| 45 | + Py_ssize_t[::1] neighbor_offsets not None): |
| 46 | + """Detect local maxima in n-dimensional array. |
| 47 | +
|
| 48 | + Inner function to `local_maxima` that detects all local maxima (including |
| 49 | + plateaus) in the image. The result is stored inplace inside `flags` with |
| 50 | + the value of "QUEUED_CANDIDATE". |
| 51 | +
|
| 52 | + Parameters |
| 53 | + ---------- |
| 54 | + image : ndarray, one-dimensional |
| 55 | + The raveled view of a n-dimensional array. |
| 56 | + flags : ndarray |
| 57 | + An array of flags that is used to store the state of each pixel during |
| 58 | + evaluation and is MODIFIED INPLACE. Initially, pixels that border the |
| 59 | + image edge must be marked as "BORDER_INDEX" while all other pixels |
| 60 | + should be marked with "NOT_MAXIMUM". |
| 61 | + neighbor_offsets : ndarray |
| 62 | + A one-dimensional array that contains the offsets to find the |
| 63 | + connected neighbors for any index in `image`. |
| 64 | + """ |
| 65 | + cdef: |
| 66 | + QueueWithHistory queue |
| 67 | + unsigned char last_dim_in_neighbors |
| 68 | + |
| 69 | + # This needs the GIL |
| 70 | + last_dim_in_neighbors = -1 in neighbor_offsets and 1 in neighbor_offsets |
| 71 | + with nogil: |
| 72 | + if last_dim_in_neighbors: |
| 73 | + # If adjacent pixels in the last dimension are part of the |
| 74 | + # neighborhood, the number of candidates which have to be evaluated |
| 75 | + # in the second algorithmic step `_fill_plateau` can be reduced |
| 76 | + # ahead of time with this function |
| 77 | + _mark_candidates_in_last_dimension(image, flags) |
| 78 | + else: |
| 79 | + # Otherwise simply mark all pixels (except border) as candidates |
| 80 | + _mark_candidates_all(flags) |
| 81 | + |
| 82 | + # Initialize a buffer used to queue positions while evaluating each |
| 83 | + # potential maximum (flagged with 2) |
| 84 | + queue_init(&queue, 64) |
| 85 | + try: |
| 86 | + for i in range(image.shape[0]): |
| 87 | + if flags[i] == CANDIDATE: |
| 88 | + # Index is potentially part of a maximum: |
| 89 | + # Find all samples which are part of the plateau and fill |
| 90 | + # with 0 or 1 depending on whether it's a true maximum |
| 91 | + _fill_plateau(image, flags, neighbor_offsets, &queue, i) |
| 92 | + finally: |
| 93 | + # Ensure that memory is released again |
| 94 | + queue_exit(&queue) |
| 95 | + |
| 96 | + |
| 97 | +cdef inline void _mark_candidates_in_last_dimension( |
| 98 | + dtype_t[::1] image, unsigned char[::1] flags) nogil: |
| 99 | + """Mark local maxima in last dimension. |
| 100 | + |
| 101 | + This function considers only the last dimension of the image and marks |
| 102 | + pixels with the "CANDIDATE" flag if it is a local maximum. |
| 103 | + |
| 104 | + Parameters |
| 105 | + ---------- |
| 106 | + image : |
| 107 | + The raveled view of a n-dimensional array. |
| 108 | + flags : |
| 109 | + An array of flags that is used to store the state of each pixel during |
| 110 | + evaluation. |
| 111 | + |
| 112 | + Notes |
| 113 | + ----- |
| 114 | + By evaluating this necessary but not sufficient condition first, usually a |
| 115 | + significant amount of pixels can be rejected without having to evaluate the |
| 116 | + entire neighborhood of their plateau. This can reduces the number of |
| 117 | + candidates that need to be evaluated with the more expensive flood-fill |
| 118 | + performed in `_fill_plateaus`. |
| 119 | + |
| 120 | + However this is only possible if the adjacent pixels in the last dimension |
| 121 | + are part of the defined neighborhood (see argument `neighbor_offsets` |
| 122 | + in `_local_maxima`). |
| 123 | + """ |
| 124 | + cdef Py_ssize_t i, i_ahead |
| 125 | + i = 1 |
| 126 | + while i < image.shape[0]: |
| 127 | + if image[i - 1] < image[i] and flags[i] != BORDER_INDEX: |
| 128 | + # Potential maximum (in last dimension) is found, find |
| 129 | + # other edge of current plateau or "edge of dimension" |
| 130 | + i_ahead = i + 1 |
| 131 | + while ( |
| 132 | + image[i] == image[i_ahead] and |
| 133 | + flags[i_ahead] != BORDER_INDEX |
| 134 | + ): |
| 135 | + i_ahead += 1 |
| 136 | + if image[i] > image[i_ahead]: |
| 137 | + # Found local maximum (in one dimension), mark all |
| 138 | + # parts of the plateau as potential maximum |
| 139 | + flags[i:i_ahead] = CANDIDATE |
| 140 | + i = i_ahead |
| 141 | + else: |
| 142 | + i += 1 |
| 143 | + |
| 144 | + |
| 145 | +cdef inline void _mark_candidates_all(unsigned char[::1] flags) nogil: |
| 146 | + """Mark all pixels as potential maxima, exclude border pixels. |
| 147 | + |
| 148 | + This function marks pixels with the "CANDIDATE" flag if they aren't the |
| 149 | + first or last index in any dimension (not flagged with "BORDER_INDEX"). |
| 150 | + |
| 151 | + Parameters |
| 152 | + ---------- |
| 153 | + flags : |
| 154 | + An array of flags that is used to store the state of each pixel during |
| 155 | + evaluation. |
| 156 | + """ |
| 157 | + cdef Py_ssize_t i = 1 |
| 158 | + while i < flags.shape[0]: |
| 159 | + if flags[i] != BORDER_INDEX: |
| 160 | + flags[i] = CANDIDATE |
| 161 | + i += 1 |
| 162 | + |
| 163 | + |
| 164 | +cdef inline void _fill_plateau( |
| 165 | + dtype_t[::1] image, unsigned char[::1] flags, |
| 166 | + Py_ssize_t[::1] neighbor_offsets, QueueWithHistory* queue_ptr, |
| 167 | + Py_ssize_t start_index) nogil: |
| 168 | + """Fill with 1 if plateau is local maximum else with 0. |
| 169 | + |
| 170 | + Parameters |
| 171 | + ---------- |
| 172 | + image : |
| 173 | + The raveled view of a n-dimensional array. |
| 174 | + flags : |
| 175 | + An array of flags that is used to store the state of each pixel during |
| 176 | + evaluation. |
| 177 | + neighbor_offsets : |
| 178 | + A one-dimensional array that contains the offsets to find the |
| 179 | + connected neighbors for any index in `image`. |
| 180 | + queue_ptr : |
| 181 | + Pointer to initialized queue. |
| 182 | + start_index : |
| 183 | + Start position for the flood-fill. |
| 184 | + """ |
| 185 | + cdef: |
| 186 | + dtype_t height |
| 187 | + unsigned char is_maximum |
| 188 | + QueueItem current_index, neighbor |
| 189 | + |
| 190 | + height = image[start_index] # Height of the evaluated plateau |
| 191 | + is_maximum = 1 # Boolean flag, initially assume true |
| 192 | + |
| 193 | + # Queue start position after clearing the buffer |
| 194 | + # which might have been used already |
| 195 | + queue_clear(queue_ptr) |
| 196 | + queue_push(queue_ptr, &start_index) |
| 197 | + flags[start_index] = QUEUED_CANDIDATE |
| 198 | + |
| 199 | + # Break loop if all queued positions were evaluated |
| 200 | + while queue_pop(queue_ptr, ¤t_index): |
| 201 | + # Look at all neighboring samples |
| 202 | + for i in range(neighbor_offsets.shape[0]): |
| 203 | + neighbor = current_index + neighbor_offsets[i] |
| 204 | + |
| 205 | + if image[neighbor] == height: |
| 206 | + # Value is part of plateau |
| 207 | + if flags[neighbor] == BORDER_INDEX: |
| 208 | + # Plateau touches border and can't be maximum |
| 209 | + is_maximum = 0 |
| 210 | + elif flags[neighbor] != QUEUED_CANDIDATE: |
| 211 | + # Index wasn't queued already, do so now |
| 212 | + queue_push(queue_ptr, &neighbor) |
| 213 | + flags[neighbor] = QUEUED_CANDIDATE |
| 214 | + |
| 215 | + elif image[neighbor] > height: |
| 216 | + # Current plateau can't be maximum because it borders a |
| 217 | + # larger one |
| 218 | + is_maximum = 0 |
| 219 | + |
| 220 | + if not is_maximum: |
| 221 | + queue_restore(queue_ptr) |
| 222 | + # Initial guess was wrong -> flag as NOT_MAXIMUM |
| 223 | + while queue_pop(queue_ptr, &neighbor): |
| 224 | + flags[neighbor] = NOT_MAXIMUM |
0 commit comments