Skip to content

Commit 15c078a

Browse files
committed
FEAT: adding vision functions.
- harris - susan - dog - nearest_neighbour
1 parent d2773fb commit 15c078a

File tree

1 file changed

+154
-4
lines changed

1 file changed

+154
-4
lines changed

arrayfire/vision.py

Lines changed: 154 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ def fast(image, threshold=20.0, arc_length=9, non_max=True, feature_ratio=0.05,
4343
Returns
4444
---------
4545
features : af.Features()
46-
- x, y, and score are calculated
47-
- orientation is 0 because FAST does not compute orientation
48-
- size is 1 because FAST does not compute multiple scales
46+
Contains the location and score. Orientation and size are not computed.
4947
5048
"""
5149
out = Features()
@@ -54,6 +52,50 @@ def fast(image, threshold=20.0, arc_length=9, non_max=True, feature_ratio=0.05,
5452
ct.c_float(feature_ratio), ct.c_uint(edge)))
5553
return out
5654

55+
def harris(image, max_corners=500, min_response=1E5, sigma=1.0, block_size=0, k_thr=0.04):
56+
"""
57+
Harris corner detector.
58+
59+
Parameters
60+
----------
61+
image : af.Array
62+
A 2D array specifying an image.
63+
64+
max_corners : scalar. optional. default: 500.
65+
Specifies the maximum number of corners to be calculated.
66+
67+
min_response : scalar. optional. default: 1E5
68+
Specifies the cutoff score for a corner to be considered
69+
70+
sigma : scalar. optional. default: 1.0
71+
- Specifies the standard deviation of a circular window.
72+
- Only used when block_size == 0. Must be >= 0.5 and <= 5.0.
73+
74+
block_size : scalar. optional. default: 0
75+
Specifies the window size.
76+
77+
k_thr : scalar. optional. default: 0.04
78+
Harris constant. must be >= 0.01
79+
80+
Returns
81+
---------
82+
83+
features : af.Features()
84+
Contains the location and score. Orientation and size are not computed.
85+
86+
Note
87+
------
88+
89+
The covariation matrix will be square when `block_size` is used and circular when `sigma` is used.
90+
91+
92+
"""
93+
out = Features()
94+
safe_call(backend.get().af_harris(ct.pointer(out.feat),
95+
image.arr, ct.c_uint(max_corners), ct.c_float(min_response),
96+
ct.c_float(sigma), ct.c_uint(block_size), ct.c_float(k_thr)))
97+
return out
98+
5799
def orb(image, threshold=20.0, max_features=400, scale = 1.5, num_levels = 4, blur_image = False):
58100
"""
59101
ORB Feature descriptor.
@@ -109,7 +151,7 @@ def hamming_matcher(query, database, dim = 0, num_nearest = 1):
109151
dim : scalar. optional. default: 0.
110152
Specifies the dimension along which feature descriptor lies.
111153
112-
num_neaarest: scalar. optional. default: 1.
154+
num_nearest: scalar. optional. default: 1.
113155
Specifies the number of nearest neighbors to find.
114156
115157
Returns
@@ -126,6 +168,43 @@ def hamming_matcher(query, database, dim = 0, num_nearest = 1):
126168
ct.c_longlong(dim), ct.c_longlong(num_nearest)))
127169
return index, dist
128170

171+
def nearest_neighbour(query, database, dim = 0, num_nearest = 1, match_type=MATCH.SSD):
172+
"""
173+
Nearest Neighbour matcher.
174+
175+
Parameters
176+
-----------
177+
178+
query : af.Array
179+
A query feature descriptor
180+
181+
database : af.Array
182+
A multi dimensional array containing the feature descriptor database.
183+
184+
dim : scalar. optional. default: 0.
185+
Specifies the dimension along which feature descriptor lies.
186+
187+
num_nearest: scalar. optional. default: 1.
188+
Specifies the number of nearest neighbors to find.
189+
190+
match_type: optional: af.MATCH. default: af.MATCH.SSD
191+
Specifies the match function metric.
192+
193+
Returns
194+
---------
195+
196+
(location, distance): tuple of af.Array
197+
location and distances of closest matches.
198+
199+
"""
200+
index = Array()
201+
dist = Array()
202+
safe_call(backend.get().af_nearest_neighbour(ct.pointer(idx.arr), ct.pointer(dist.arr),
203+
query.arr, database.arr,
204+
ct.c_longlong(dim), ct.c_longlong(num_nearest),
205+
match_type))
206+
return index, dist
207+
129208
def match_template(image, template, match_type = MATCH.SAD):
130209
"""
131210
Find the closest match of a template in an image.
@@ -151,3 +230,74 @@ def match_template(image, template, match_type = MATCH.SAD):
151230
out = Array()
152231
safe_call(backend.get().af_match_template(ct.pointer(out.arr), image.arr, template.arr, match_type))
153232
return out
233+
234+
def susan(image, radius=3, diff_thr=32, geom_thr=10, feature_ratio=0.05, edge=3):
235+
"""
236+
SUSAN corner detector.
237+
238+
Parameters
239+
----------
240+
image : af.Array
241+
A 2D array specifying an image.
242+
243+
radius : scalar. optional. default: 500.
244+
Specifies the radius of each pixel neighborhood.
245+
246+
diff_thr : scalar. optional. default: 1E5
247+
Specifies the intensity difference threshold.
248+
249+
geom_thr : scalar. optional. default: 1.0
250+
Specifies the geometric threshold.
251+
252+
feature_ratio : scalar. optional. default: 0.05 (5%)
253+
Specifies the ratio of corners found to number of pixels.
254+
255+
edge : scalar. optional. default: 3
256+
Specifies the number of edge rows and columns that are ignored.
257+
258+
Returns
259+
---------
260+
261+
features : af.Features()
262+
Contains the location and score. Orientation and size are not computed.
263+
264+
"""
265+
out = Features()
266+
safe_call(backend.get().af_susan(ct.pointer(out.feat),
267+
image.arr, ct.c_uint(radius), ct.c_float(diff_thr),
268+
ct.c_float(geom_thr), ct.c_float(feature_ratio),
269+
ct.c_uint(edge)))
270+
return out
271+
272+
def dog(image, radius1, radius2):
273+
"""
274+
Difference of gaussians.
275+
276+
Parameters
277+
----------
278+
image : af.Array
279+
A 2D array specifying an image.
280+
281+
radius1 : scalar.
282+
The radius of first gaussian kernel.
283+
284+
radius2 : scalar.
285+
The radius of second gaussian kernel.
286+
287+
288+
Returns
289+
--------
290+
291+
out : af.Array
292+
A multi dimensional array containing the difference of gaussians.
293+
294+
Note
295+
------
296+
297+
The sigma values are calculated to be 0.25 * radius.
298+
"""
299+
300+
out = Array()
301+
safe_call(backend.get().af_dog(ct.pointer(out.arr),
302+
image.arr, radius1, radius2))
303+
return out

0 commit comments

Comments
 (0)