FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
arrayfire/examples/image_processing/edge.cpp at master · arrayfire/arrayfire · GitHub
arrayfire
arrayfire
Repository navigation
Code
Issues
292
(292)
Pull requests
39
(39)
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
arrayfire
/
examples
/
image_processing
/
edge.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
116 lines (91 loc) · 2.95 KB
Breadcrumbs
arrayfire
/
examples
/
image_processing
/
edge.cpp
Copy path
File metadata and controls
116 lines (91 loc) · 2.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
*******************************************************
*/
#
include
<
arrayfire.h
>
#
include
<
stdio.h
>
#
include
<
af/util.h
>
#
include
<
cstdlib
>
using
namespace
af
;
void
prewitt
(array &mag, array &dir,
const
array &in) {
static
float
h1[] = {
1
,
1
,
1
};
static
float
h2[] = {-
1
,
0
,
1
};
static
array
colf
(
3
,
1
, h1);
static
array
rowf
(
3
,
1
, h2);
//
Find the gradients
array Gy =
convolve
(rowf, colf, in);
array Gx =
convolve
(colf, rowf, in);
//
Find magnitude and direction
mag =
hypot
(Gx, Gy);
dir =
atan2
(Gy, Gx);
}
void
sobelFilter
(array &mag, array &dir,
const
array &in) {
array Gx, Gy;
sobel
(Gx, Gy, in,
3
);
//
Find magnitude and direction
mag =
hypot
(Gx, Gy);
dir =
atan2
(Gy, Gx);
}
array
normalize
(
const
array &in) {
float
mx = max<
float
>(in);
float
mn = min<
float
>(in);
return
(in - mn) / (mx - mn);
}
array
edge
(
const
array &in,
int
method =
0
) {
int
w =
5
;
if
(in.
dims
(
0
) <
512
) w =
3
;
if
(in.
dims
(
0
) >
2048
) w =
7
;
int
h =
5
;
if
(in.
dims
(
0
) <
512
) h =
3
;
if
(in.
dims
(
0
) >
2048
) h =
7
;
array ker =
gaussianKernel
(w, h);
array smooth =
convolve
(in, ker);
array mag, dir;
switch
(method) {
case
1
:
prewitt
(mag, dir, smooth);
break
;
case
2
:
sobelFilter
(mag, dir, smooth);
break
;
case
3
:
mag =
canny
(in,
AF_CANNY_THRESHOLD_AUTO_OTSU
,
0.18
,
0.54
).
as
(
f32
);
break
;
default
:
throw
af::exception
(
"
Unsupported type
"
);
}
return
normalize
(mag);
}
void
edge
() {
af::Window
myWindow
(
"
Edge Dectectors
"
);
af::Window
myWindow2
(
512
,
512
,
"
Histogram
"
);
array in =
loadImage
(
ASSETS_DIR
"
/examples/images/trees_ctm.jpg
"
,
false
);
array prewitt =
edge
(in,
1
);
array sobelFilter =
edge
(in,
2
);
array hst =
histogram
(in,
256
,
0
,
255
);
array cny =
edge
(in,
3
);
myWindow2.
setAxesTitles
(
"
Bins
"
,
"
Frequency
"
);
while
(!myWindow.
close
() && !myWindow2.
close
()) {
/*
show input, prewitt and sobel edge detectors in a grid
*/
myWindow.
grid
(
2
,
2
);
myWindow
(
0
,
0
).
image
(in /
255
,
"
Input Image
"
);
myWindow
(
0
,
1
).
image
(prewitt,
"
Prewitt
"
);
myWindow
(
1
,
0
).
image
(sobelFilter,
"
Sobel
"
);
myWindow
(
1
,
1
).
image
(cny,
"
Canny
"
);
myWindow.
show
();
/*
show histogram on input in separate window
*/
myWindow2.
hist
(hst,
0
,
255
);
}
}
int
main
(
int
argc,
char
*argv[]) {
int
device = argc >
1
?
atoi
(argv[
1
]) :
0
;
try
{
af::setDevice
(device);
af::info
();
printf
(
"
** ArrayFire Edge Detection Demo **
\n
"
);
edge
();
}
catch
(af::exception &e) {
fprintf
(stderr,
"
%s
\n
"
, e.
what
());
throw
;
}
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL