Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0570570

Browse files
syurkevi9prady9
authored andcommitted
adds computer vision example
1 parente85cefc commit0570570

File tree

4 files changed

+386
-0
lines changed

4 files changed

+386
-0
lines changed

‎examples/computer_vision/fast.py

Lines changed: 81 additions & 0 deletions
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+
fromtimeimporttime
13+
importarrayfireasaf
14+
importsys
15+
16+
defdraw_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+
returnimg
35+
36+
deffast_demo(console):
37+
38+
ifconsole:
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+
forfinrange(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+
ifnotconsole:
63+
# Previews color image with green crosshairs
64+
wnd=af.Window(512,512,"FAST Feature Detector")
65+
66+
whilenotwnd.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]=='-')iflen(sys.argv)>2elseFalse
77+
78+
af.info()
79+
print("** ArrayFire FAST Feature Detector Demo **\n")
80+
fast_demo(console)
81+

‎examples/computer_vision/harris.py

Lines changed: 119 additions & 0 deletions
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+
fromtimeimporttime
13+
importarrayfireasaf
14+
importsys
15+
16+
defdraw_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+
returnimg
35+
36+
defharris_demo(console):
37+
38+
ifconsole:
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+
forxinrange(img_color.dims()[1]):
87+
foryinrange(img_color.dims()[0]):
88+
ifcorners_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+
ifnotconsole:
95+
# Previews color image with green crosshairs
96+
wnd=af.Window(512,512,"FAST Feature Detector")
97+
98+
whilenotwnd.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]=='-')iflen(sys.argv)>2elseFalse
114+
115+
af.info()
116+
print("** ArrayFire Harris Corner Detector Demo **\n")
117+
118+
harris_demo(console)
119+

‎examples/computer_vision/matching.py

Lines changed: 105 additions & 0 deletions
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+
fromtimeimporttime
13+
importarrayfireasaf
14+
importsys
15+
16+
defnormalize(a):
17+
max_=float(af.max(a))
18+
min_=float(af.min(a))
19+
return (a-min_)/ (max_-min_)
20+
21+
defdraw_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+
returnimg
45+
46+
deftemplateMatchingDemo(console):
47+
48+
ifconsole:
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+
ifnotconsole:
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+
whilenotwnd.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]=='-')iflen(sys.argv)>2elseFalse
101+
102+
af.info()
103+
print("** ArrayFire template matching Demo **\n")
104+
templateMatchingDemo(console)
105+

‎examples/computer_vision/susan.py

Lines changed: 81 additions & 0 deletions
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+
fromtimeimporttime
13+
importarrayfireasaf
14+
importsys
15+
16+
defdraw_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+
returnimg
35+
36+
defsusan_demo(console):
37+
38+
ifconsole:
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+
forfinrange(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+
ifnotconsole:
63+
# Previews color image with green crosshairs
64+
wnd=af.Window(512,512,"SUSAN Feature Detector")
65+
66+
whilenotwnd.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]=='-')iflen(sys.argv)>2elseFalse
77+
78+
af.info()
79+
print("** ArrayFire SUSAN Feature Detector Demo **\n")
80+
susan_demo(console)
81+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp