Skip to content

Commit ee3deff

Browse files
committed
adds computer vision example
1 parent 599c737 commit ee3deff

File tree

4 files changed

+386
-0
lines changed

4 files changed

+386
-0
lines changed

examples/computer_vision/fast.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/python
2+
3+
#######################################################
4+
# Copyright (c) 2018, ArrayFire
5+
# All rights reserved.
6+
#
7+
# This file is distributed under 3-clause BSD license.
8+
# The complete license agreement can be obtained at:
9+
# http://arrayfire.com/licenses/BSD-3-Clause
10+
########################################################
11+
12+
from time import time
13+
import arrayfire as af
14+
import sys
15+
16+
def draw_corners(img, x, y, draw_len):
17+
# Draw vertical line of (draw_len * 2 + 1) pixels centered on the corner
18+
# Set only the first channel to 1 (green lines)
19+
xmin = max(0, x - draw_len)
20+
xmax = min(img.dims()[1], x + draw_len)
21+
22+
img[y, xmin : xmax, 0] = 0.0
23+
img[y, xmin : xmax, 1] = 1.0
24+
img[y, xmin : xmax, 2] = 0.0
25+
26+
# Draw vertical line of (draw_len * 2 + 1) pixels centered on the corner
27+
# Set only the first channel to 1 (green lines)
28+
ymin = max(0, y - draw_len)
29+
ymax = min(img.dims()[0], y + draw_len)
30+
31+
img[ymin : ymax, x, 0] = 0.0
32+
img[ymin : ymax, x, 1] = 1.0
33+
img[ymin : ymax, x, 2] = 0.0
34+
return img
35+
36+
def fast_demo(console):
37+
38+
if console:
39+
img_color = af.load_image("../../assets/examples/images/square.png", True);
40+
else:
41+
img_color = af.load_image("../../assets/examples/images/man.jpg", True);
42+
43+
img_color /= 255.0
44+
img = af.color_space(img_color, af.CSPACE.GRAY, af.CSPACE.RGB)
45+
46+
features = af.fast(img)
47+
48+
xs = features.get_xpos()
49+
ys = features.get_ypos()
50+
51+
draw_len = 3;
52+
num_features = features.num_features().value
53+
for f in range(num_features):
54+
print(f)
55+
x = xs[f]
56+
y = ys[f]
57+
58+
img_color = draw_corners(img_color, x, y, draw_len)
59+
60+
61+
print("Features found: {}".format(num_features))
62+
if not console:
63+
# Previews color image with green crosshairs
64+
wnd = af.Window(512, 512, "FAST Feature Detector")
65+
66+
while not wnd.close():
67+
wnd.image(img_color)
68+
else:
69+
print(xs);
70+
print(ys);
71+
72+
73+
if __name__ == "__main__":
74+
if (len(sys.argv) > 1):
75+
af.set_device(int(sys.argv[1]))
76+
console = (sys.argv[2] == '-') if len(sys.argv) > 2 else False
77+
78+
af.info()
79+
print("** ArrayFire FAST Feature Detector Demo **\n")
80+
fast_demo(console)
81+

examples/computer_vision/harris.py

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/usr/bin/python
2+
3+
#######################################################
4+
# Copyright (c) 2018, ArrayFire
5+
# All rights reserved.
6+
#
7+
# This file is distributed under 3-clause BSD license.
8+
# The complete license agreement can be obtained at:
9+
# http://arrayfire.com/licenses/BSD-3-Clause
10+
########################################################
11+
12+
from time import time
13+
import arrayfire as af
14+
import sys
15+
16+
def draw_corners(img, x, y, draw_len):
17+
# Draw vertical line of (draw_len * 2 + 1) pixels centered on the corner
18+
# Set only the first channel to 1 (green lines)
19+
xmin = max(0, x - draw_len)
20+
xmax = min(img.dims()[1], x + draw_len)
21+
22+
img[y, xmin : xmax, 0] = 0.0
23+
img[y, xmin : xmax, 1] = 1.0
24+
img[y, xmin : xmax, 2] = 0.0
25+
26+
# Draw vertical line of (draw_len * 2 + 1) pixels centered on the corner
27+
# Set only the first channel to 1 (green lines)
28+
ymin = max(0, y - draw_len)
29+
ymax = min(img.dims()[0], y + draw_len)
30+
31+
img[ymin : ymax, x, 0] = 0.0
32+
img[ymin : ymax, x, 1] = 1.0
33+
img[ymin : ymax, x, 2] = 0.0
34+
return img
35+
36+
def harris_demo(console):
37+
38+
if console:
39+
img_color = af.load_image("../../assets/examples/images/square.png", True);
40+
else:
41+
img_color = af.load_image("../../assets/examples/images/man.jpg", True);
42+
43+
img_color /= 255.0
44+
img = af.color_space(img_color, af.CSPACE.GRAY, af.CSPACE.RGB)
45+
46+
ix, iy = af.gradient(img)
47+
ixx = ix * ix
48+
ixy = ix * iy
49+
iyy = iy * iy
50+
51+
# Compute a Gaussian kernel with standard deviation of 1.0 and length of 5 pixels
52+
# These values can be changed to use a smaller or larger window
53+
gauss_filt = af.gaussian_kernel(5, 5, 1.0, 1.0)
54+
55+
# Filter second order derivatives
56+
ixx = af.convolve(ixx, gauss_filt)
57+
ixy = af.convolve(ixy, gauss_filt)
58+
iyy = af.convolve(iyy, gauss_filt)
59+
60+
# Calculate trace
61+
itr = ixx + iyy
62+
63+
# Calculate determinant
64+
idet = ixx * iyy - ixy * ixy
65+
66+
# Calculate Harris response
67+
response = idet - 0.04 * (itr * itr)
68+
69+
# Get maximum response for each 3x3 neighborhood
70+
mask = af.constant(1, 3, 3)
71+
max_resp = af.dilate(response, mask)
72+
73+
# Discard responses that are not greater than threshold
74+
corners = response > 1e5
75+
corners = corners * response
76+
77+
# Discard responses that are not equal to maximum neighborhood response,
78+
# scale them to original value
79+
corners = (corners == max_resp) * corners
80+
81+
# Copy device array to python list on host
82+
corners_list = corners.to_list()
83+
84+
draw_len = 3
85+
good_corners = 0
86+
for x in range(img_color.dims()[1]):
87+
for y in range(img_color.dims()[0]):
88+
if corners_list[x][y] > 1e5:
89+
img_color = draw_corners(img_color, x, y, draw_len)
90+
good_corners += 1
91+
92+
93+
print("Corners found: {}".format(good_corners))
94+
if not console:
95+
# Previews color image with green crosshairs
96+
wnd = af.Window(512, 512, "FAST Feature Detector")
97+
98+
while not wnd.close():
99+
wnd.image(img_color)
100+
else:
101+
idx = af.where(corners)
102+
103+
corners_x = idx / corners.dims()[0]
104+
corners_y = idx % corners.dims()[0]
105+
106+
print(corners_x)
107+
print(corners_y)
108+
109+
110+
if __name__ == "__main__":
111+
if (len(sys.argv) > 1):
112+
af.set_device(int(sys.argv[1]))
113+
console = (sys.argv[2] == '-') if len(sys.argv) > 2 else False
114+
115+
af.info()
116+
print("** ArrayFire Harris Corner Detector Demo **\n")
117+
118+
harris_demo(console)
119+

examples/computer_vision/matching.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/python
2+
3+
#######################################################
4+
# Copyright (c) 2018, ArrayFire
5+
# All rights reserved.
6+
#
7+
# This file is distributed under 3-clause BSD license.
8+
# The complete license agreement can be obtained at:
9+
# http://arrayfire.com/licenses/BSD-3-Clause
10+
########################################################
11+
12+
from time import time
13+
import arrayfire as af
14+
import sys
15+
16+
def normalize(a):
17+
max_ = float(af.max(a))
18+
min_ = float(af.min(a))
19+
return (a - min_) / (max_ - min_)
20+
21+
def draw_rectangle(img, x, y, wx, wy):
22+
print("\nMatching patch origin = ({}, {})\n".format(x, y))
23+
24+
# top edge
25+
img[y, x : x + wx, 0] = 0.0
26+
img[y, x : x + wx, 1] = 0.0
27+
img[y, x : x + wx, 2] = 1.0
28+
29+
# bottom edge
30+
img[y + wy, x : x + wx, 0] = 0.0
31+
img[y + wy, x : x + wx, 1] = 0.0
32+
img[y + wy, x : x + wx, 2] = 1.0
33+
34+
# left edge
35+
img[y : y + wy, x, 0] = 0.0
36+
img[y : y + wy, x, 1] = 0.0
37+
img[y : y + wy, x, 2] = 1.0
38+
39+
# left edge
40+
img[y : y + wy, x + wx, 0] = 0.0
41+
img[y : y + wy, x + wx, 1] = 0.0
42+
img[y : y + wy, x + wx, 2] = 1.0
43+
44+
return img
45+
46+
def templateMatchingDemo(console):
47+
48+
if console:
49+
img_color = af.load_image("../../assets/examples/images/square.png", True);
50+
else:
51+
img_color = af.load_image("../../assets/examples/images/man.jpg", True);
52+
53+
# Convert the image from RGB to gray-scale
54+
img = af.color_space(img_color, af.CSPACE.GRAY, af.CSPACE.RGB)
55+
iDims = img.dims()
56+
print("Input image dimensions: ", iDims)
57+
58+
# Extract a patch from the input image
59+
patch_size = 100
60+
tmp_img = img[100 : 100+patch_size, 100 : 100+patch_size]
61+
62+
result = af.match_template(img, tmp_img) # Default disparity metric is
63+
# Sum of Absolute differences (SAD)
64+
# Currently supported metrics are
65+
# AF_SAD, AF_ZSAD, AF_LSAD, AF_SSD,
66+
# AF_ZSSD, AF_LSSD
67+
68+
disp_img = img / 255.0
69+
disp_tmp = tmp_img / 255.0
70+
disp_res = normalize(result)
71+
72+
minval, minloc = af.imin(disp_res)
73+
print("Location(linear index) of minimum disparity value = {}".format(minloc))
74+
75+
if not console:
76+
marked_res = af.tile(disp_img, 1, 1, 3)
77+
marked_res = draw_rectangle(marked_res, minloc%iDims[0], minloc/iDims[0],\
78+
patch_size, patch_size)
79+
80+
print("Note: Based on the disparity metric option provided to matchTemplate function")
81+
print("either minimum or maximum disparity location is the starting corner")
82+
print("of our best matching patch to template image in the search image")
83+
84+
wnd = af.Window(512, 512, "Template Matching Demo")
85+
86+
while not wnd.close():
87+
wnd.set_colormap(af.COLORMAP.DEFAULT)
88+
wnd.grid(2, 2)
89+
wnd[0, 0].image(disp_img, "Search Image" )
90+
wnd[0, 1].image(disp_tmp, "Template Patch" )
91+
wnd[1, 0].image(marked_res, "Best Match" )
92+
wnd.set_colormap(af.COLORMAP.HEAT)
93+
wnd[1, 1].image(disp_res, "Disparity Values")
94+
wnd.show()
95+
96+
97+
if __name__ == "__main__":
98+
if (len(sys.argv) > 1):
99+
af.set_device(int(sys.argv[1]))
100+
console = (sys.argv[2] == '-') if len(sys.argv) > 2 else False
101+
102+
af.info()
103+
print("** ArrayFire template matching Demo **\n")
104+
templateMatchingDemo(console)
105+

examples/computer_vision/susan.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/python
2+
3+
#######################################################
4+
# Copyright (c) 2018, ArrayFire
5+
# All rights reserved.
6+
#
7+
# This file is distributed under 3-clause BSD license.
8+
# The complete license agreement can be obtained at:
9+
# http://arrayfire.com/licenses/BSD-3-Clause
10+
########################################################
11+
12+
from time import time
13+
import arrayfire as af
14+
import sys
15+
16+
def draw_corners(img, x, y, draw_len):
17+
# Draw vertical line of (draw_len * 2 + 1) pixels centered on the corner
18+
# Set only the first channel to 1 (green lines)
19+
xmin = max(0, x - draw_len)
20+
xmax = min(img.dims()[1], x + draw_len)
21+
22+
img[y, xmin : xmax, 0] = 0.0
23+
img[y, xmin : xmax, 1] = 1.0
24+
img[y, xmin : xmax, 2] = 0.0
25+
26+
# Draw vertical line of (draw_len * 2 + 1) pixels centered on the corner
27+
# Set only the first channel to 1 (green lines)
28+
ymin = max(0, y - draw_len)
29+
ymax = min(img.dims()[0], y + draw_len)
30+
31+
img[ymin : ymax, x, 0] = 0.0
32+
img[ymin : ymax, x, 1] = 1.0
33+
img[ymin : ymax, x, 2] = 0.0
34+
return img
35+
36+
def susan_demo(console):
37+
38+
if console:
39+
img_color = af.load_image("../../assets/examples/images/square.png", True);
40+
else:
41+
img_color = af.load_image("../../assets/examples/images/man.jpg", True);
42+
43+
img_color /= 255.0
44+
img = af.color_space(img_color, af.CSPACE.GRAY, af.CSPACE.RGB)
45+
46+
features = af.susan(img)
47+
48+
xs = features.get_xpos()
49+
ys = features.get_ypos()
50+
51+
draw_len = 3;
52+
num_features = features.num_features().value
53+
for f in range(num_features):
54+
print(f)
55+
x = xs[f]
56+
y = ys[f]
57+
58+
img_color = draw_corners(img_color, x, y, draw_len)
59+
60+
61+
print("Features found: {}".format(num_features))
62+
if not console:
63+
# Previews color image with green crosshairs
64+
wnd = af.Window(512, 512, "SUSAN Feature Detector")
65+
66+
while not wnd.close():
67+
wnd.image(img_color)
68+
else:
69+
print(xs);
70+
print(ys);
71+
72+
73+
if __name__ == "__main__":
74+
if (len(sys.argv) > 1):
75+
af.set_device(int(sys.argv[1]))
76+
console = (sys.argv[2] == '-') if len(sys.argv) > 2 else False
77+
78+
af.info()
79+
print("** ArrayFire SUSAN Feature Detector Demo **\n")
80+
susan_demo(console)
81+

0 commit comments

Comments
 (0)