forked from scikit-image/scikit-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
corner_cy.pyx
291 lines (241 loc) · 9.98 KB
/
corner_cy.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#cython: cdivision=True
#cython: boundscheck=False
#cython: nonecheck=False
#cython: wraparound=False
import numpy as np
cimport numpy as cnp
from libc.float cimport DBL_MAX
from libc.math cimport atan2, fabs
from .._shared.fused_numerics cimport np_floats
from ..util import img_as_float64
cnp.import_array()
def _corner_moravec(image, Py_ssize_t window_size=1):
"""Compute Moravec corner measure response image.
This is one of the simplest corner detectors and is comparatively fast but
has several limitations (e.g. not rotation invariant).
Parameters
----------
image : ndarray
Input image.
window_size : int, optional (default 1)
Window size.
Returns
-------
response : ndarray
Moravec response image.
References
----------
.. [1] http://kiwi.cs.dal.ca/~dparks/CornerDetection/moravec.htm
.. [2] https://en.wikipedia.org/wiki/Corner_detection
Examples
--------
>>> from skimage.feature import corner_moravec
>>> square = np.zeros([7, 7])
>>> square[3, 3] = 1
>>> square.astype(int)
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
>>> corner_moravec(square).astype(int)
array([[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 2, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]])
"""
cdef Py_ssize_t rows = image.shape[0]
cdef Py_ssize_t cols = image.shape[1]
cdef double[:, ::1] cimage = np.ascontiguousarray(img_as_float64(image))
cdef double[:, ::1] out = np.zeros(image.shape, dtype=np.double)
cdef double msum, min_msum, t
cdef Py_ssize_t r, c, br, bc, mr, mc, a, b
with nogil:
for r in range(2 * window_size, rows - 2 * window_size):
for c in range(2 * window_size, cols - 2 * window_size):
min_msum = DBL_MAX
for br in range(r - window_size, r + window_size + 1):
for bc in range(c - window_size, c + window_size + 1):
if br != r and bc != c:
msum = 0
for mr in range(- window_size, window_size + 1):
for mc in range(- window_size, window_size + 1):
t = cimage[r + mr, c + mc] - cimage[br + mr, bc + mc]
msum += t * t
min_msum = min(msum, min_msum)
out[r, c] = min_msum
return np.asarray(out)
cdef inline np_floats _corner_fast_response(np_floats curr_pixel,
np_floats* circle_intensities,
signed char* bins, signed char
state, char n) nogil:
cdef char consecutive_count = 0
cdef np_floats curr_response
cdef Py_ssize_t l, m
for l in range(15 + n):
if bins[l % 16] == state:
consecutive_count += 1
if consecutive_count == n:
curr_response = 0
for m in range(16):
curr_response += fabs(circle_intensities[m] - curr_pixel)
return curr_response
else:
consecutive_count = 0
return 0
def _corner_fast(np_floats[:, ::1] image, signed char n, np_floats threshold):
if np_floats is cnp.float32_t:
dtype = np.float32
else:
dtype = np.float64
cdef Py_ssize_t rows = image.shape[0]
cdef Py_ssize_t cols = image.shape[1]
cdef Py_ssize_t i, j, k
cdef signed char speed_sum_b, speed_sum_d
cdef np_floats curr_pixel
cdef np_floats lower_threshold, upper_threshold
cdef np_floats[:, ::1] corner_response = np.zeros((rows, cols),
dtype=dtype)
cdef signed char *rp = [0, 1, 2, 3, 3, 3, 2, 1, 0, -1, -2, -3, -3,
-3, -2, -1]
cdef signed char *cp = [3, 3, 2, 1, 0, -1, -2, -3, -3, -3, -2, -1,
0, 1, 2, 3]
cdef signed char bins[16]
cdef np_floats circle_intensities[16]
cdef double curr_response
with nogil:
for i in range(3, rows - 3):
for j in range(3, cols - 3):
curr_pixel = image[i, j]
lower_threshold = curr_pixel - threshold
upper_threshold = curr_pixel + threshold
for k in range(16):
circle_intensities[k] = image[i + rp[k], j + cp[k]]
if circle_intensities[k] > upper_threshold:
# Brighter pixel
bins[k] = b'b'
elif circle_intensities[k] < lower_threshold:
# Darker pixel
bins[k] = b'd'
else:
# Similar pixel
bins[k] = b's'
# High speed test for n >= 12
if n >= 12:
speed_sum_b = 0
speed_sum_d = 0
for k in range(0, 16, 4):
if bins[k] == b'b':
speed_sum_b += 1
elif bins[k] == b'd':
speed_sum_d += 1
if speed_sum_d < 3 and speed_sum_b < 3:
continue
# Test for bright pixels
curr_response = _corner_fast_response[np_floats](curr_pixel,
circle_intensities, bins,
b'b', n)
# Test for dark pixels
if curr_response == 0:
curr_response = _corner_fast_response[np_floats](curr_pixel,
circle_intensities,
bins, b'd', n)
corner_response[i, j] = curr_response
return np.asarray(corner_response)
def _corner_orientations(np_floats[:, ::1] image, Py_ssize_t[:, :] corners,
mask):
"""Compute the orientation of corners.
The orientation of corners is computed using the first order central moment
i.e. the center of mass approach. The corner orientation is the angle of
the vector from the corner coordinate to the intensity centroid in the
local neighborhood around the corner calculated using first order central
moment.
Parameters
----------
image : 2D array
Input grayscale image.
corners : (N, 2) array
Corner coordinates as ``(row, col)``.
mask : 2D array
Mask defining the local neighborhood of the corner used for the
calculation of the central moment.
Returns
-------
orientations : (N, 1) array
Orientations of corners in the range [-pi, pi].
References
----------
.. [1] Ethan Rublee, Vincent Rabaud, Kurt Konolige and Gary Bradski
"ORB : An efficient alternative to SIFT and SURF"
http://www.vision.cs.chubu.ac.jp/CV-R/pdf/Rublee_iccv2011.pdf
.. [2] Paul L. Rosin, "Measuring Corner Properties"
http://users.cs.cf.ac.uk/Paul.Rosin/corner2.pdf
Examples
--------
>>> from skimage.morphology import octagon
>>> from skimage.feature import (corner_fast, corner_peaks,
... corner_orientations)
>>> square = np.zeros((12, 12))
>>> square[3:9, 3:9] = 1
>>> square.astype(int)
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
>>> corners = corner_peaks(corner_fast(square, 9), min_distance=1)
>>> corners
array([[3, 3],
[3, 8],
[8, 3],
[8, 8]])
>>> orientations = corner_orientations(square, corners, octagon(3, 2))
>>> np.rad2deg(orientations)
array([ 45., 135., -45., -135.])
"""
if np_floats is cnp.float32_t:
dtype = np.float32
else:
dtype = np.float64
if mask.shape[0] % 2 != 1 or mask.shape[1] % 2 != 1:
raise ValueError("Size of mask must be uneven.")
cdef unsigned char[:, ::1] cmask = np.ascontiguousarray(mask != 0,
dtype=np.uint8)
cdef Py_ssize_t i, r, c, r0, c0
cdef Py_ssize_t mrows = mask.shape[0]
cdef Py_ssize_t mcols = mask.shape[1]
cdef Py_ssize_t mrows2 = (mrows - 1) / 2
cdef Py_ssize_t mcols2 = (mcols - 1) / 2
cdef np_floats[:, :] cimage = np.pad(image, (mrows2, mcols2),
mode='constant',
constant_values=0)
cdef np_floats[:] orientations = np.zeros(corners.shape[0], dtype=dtype)
cdef np_floats curr_pixel, m01, m10, m01_tmp
with nogil:
for i in range(corners.shape[0]):
r0 = corners[i, 0]
c0 = corners[i, 1]
m01 = 0
m10 = 0
for r in range(mrows):
m01_tmp = 0
for c in range(mcols):
if cmask[r, c]:
curr_pixel = cimage[r0 + r, c0 + c]
m10 += curr_pixel * (c - mcols2)
m01_tmp += curr_pixel
m01 += m01_tmp * (r - mrows2)
orientations[i] = atan2(m01, m10)
return np.asarray(orientations)