FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
visioncpp/examples/anisotropic_diffusion.cpp at master · codeplaysoftware/visioncpp · GitHub
This repository was archived by the owner on Apr 29, 2024. It is now read-only.
codeplaysoftware
visioncpp
Repository navigation
Code
Issues
Pull requests
1
(1)
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
visioncpp
/
examples
/
anisotropic_diffusion.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
211 lines (170 loc) · 6.69 KB
Breadcrumbs
visioncpp
/
examples
/
anisotropic_diffusion.cpp
Copy path
File metadata and controls
211 lines (170 loc) · 6.69 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
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
//
This file is part of VisionCPP, a lightweight C++ template library
//
for computer vision and image processing.
//
//
Copyright (C) 2016 Codeplay Software Limited. All Rights Reserved.
//
//
Contact: visioncpp@codeplay.com
//
//
Licensed under the Apache License, Version 2.0 (the "License");
//
you may not use this file except in compliance with the License.
//
You may obtain a copy of the License at
//
//
http://www.apache.org/licenses/LICENSE-2.0
//
//
Unless required by applicable law or agreed to in writing, software
//
distributed under the License is distributed on an "AS IS" BASIS,
//
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
See the License for the specific language governing permissions and
//
limitations under the License.
//
/ \file anisotropic_diffusion.cpp
//
/
//
/ ---------Example---------
//
/ Simplified Anisotropic Diffusion
//
/
//
/ \brief This example implements a simplified version of the Perona-Malik
//
/ Anisotropic Diffusion.
//
/ This technique removes noise from an image preserving the edges.
//
/ \param k - the edge preserving parameter [the smaller is the k, less things
//
/ are considered edges, the bigger, more things are considered as edges]
//
/ \param iters - number of iterations [it controls how blurry the image will
//
/ become, a higher number means a more blurry image]
//
include OpenCV for camera display
#
include
<
opencv2/opencv.hpp
>
//
include VisionCpp
#
include
<
visioncpp.hpp
>
//
tunable parameters
constexpr
float
k{
15
.
0f
};
//
edge preserving parameter
constexpr
size_t
iters{
15
};
//
controls the blur
//
operator which implements the simplified anisotropic diffusion
struct
AniDiff
{
template
<
typename
T>
visioncpp::pixel::
F32C3
operator
()(T nbr) {
//
init output pixel
cl::sycl::float4
out
(
0
,
0
,
0
,
0
);
//
init sum variable, which is used to normalize
cl::sycl::float4
sum_w
(
0
,
0
,
0
,
0
);
//
get center pixel
cl::sycl::float4
p1
(nbr.
at
(nbr.
I_c
, nbr.
I_r
)[
0
],
nbr.
at
(nbr.
I_c
, nbr.
I_r
)[
1
],
nbr.
at
(nbr.
I_c
, nbr.
I_r
)[
2
],
0
);
//
iterate over a 3x3 neighbourhood
for
(
int
i = -
1
; i <=
1
; i++) {
for
(
int
j = -
1
; j <=
1
; j++) {
//
get neighbour pixel
cl::sycl::float4
p2
(nbr.
at
(nbr.
I_c
+ i, nbr.
I_r
+ j)[
0
],
nbr.
at
(nbr.
I_c
+ i, nbr.
I_r
+ j)[
1
],
nbr.
at
(nbr.
I_c
+ i, nbr.
I_r
+ j)[
2
],
0
);
//
computes the weight which basically is the difference between pixels
cl::sycl::float4 w =
cl::sycl::exp
((-k) *
cl::sycl::fabs
(p1 - p2));
//
sum the weights for normalization
sum_w += w;
//
store the output
out += w * p2;
}
}
//
normalize output and return
out = out / sum_w;
return
visioncpp::pixel::F32C3
(out.
x
(), out.
y
(), out.
z
());
}
};
//
main program
int
main
(
int
argc,
char
**argv) {
//
open video or camera
cv::VideoCapture cap;
if
(argc ==
1
) {
cap.
open
(
0
);
std::cout <<
"
To use video
"
<< std::endl;
std::cout <<
"
example>: ./example path/to/video.avi
"
<< std::endl;
}
else
if
(argc >
1
) {
cap.
open
(argv[
1
]);
}
//
check if we succeeded
if
(!cap.
isOpened
()) {
std::cout <<
"
Opening Camera/Video Failed.
"
<< std::endl;
return
-
1
;
}
//
selecting device using sycl as backend
auto
dev = visioncpp::make_device<visioncpp::backend::sycl,
visioncpp::device::cpu>();
//
defining size constants
constexpr
size_t
COLS
=
640
;
constexpr
size_t
ROWS
=
480
;
constexpr
size_t
CHNS
=
3
;
//
initialising output pointer
std::shared_ptr<uchar>
output
(
new
uchar[
COLS
*
ROWS
*
CHNS
],
[](uchar *dataMem) {
delete[]
dataMem; });
//
initializing input and output image
cv::Mat input;
cv::Mat
outImage
(
ROWS
,
COLS
,
CV_8UC
(
CHNS
), output.
get
());
/*
This example contains a small expression tree
but it uses a device memory which stores a temporary computation.
So it is possible to create a loop during the computation
and the data is always in the device. It just comes to the host in the last
execute.
Below is the expression tree used for this example
(in_node)
|
(frgb) [OP_U8C3ToF32C3] (convert uchar to float)
|
---->(anidiff) [AniDiff] (It iterates several times in the same node
| | applying the anisotropic diffusion serveral times)
-------
|
(urgb) [OP_F32C3ToU8C3] (convert float to uchar to display)
*/
for
(;;) {
//
Starting building the tree (use {} during the creation of the tree)
{
//
read frame
cap.
read
(input);
//
check if image was loaded
if
(!input.
data
) {
break
;
}
//
resize image to the desirable size
cv::resize
(input, input,
cv::Size
(
COLS
,
ROWS
),
0
,
0
, cv::
INTER_CUBIC
);
auto
in_node =
visioncpp::terminal<visioncpp::pixel::
U8C3
,
COLS
,
ROWS
,
visioncpp::memory_type::Buffer2D>(input.
data
);
auto
out_node =
visioncpp::terminal<visioncpp::pixel::
U8C3
,
COLS
,
ROWS
,
visioncpp::memory_type::Buffer2D>(output.
get
());
//
device only memory
auto
device_memory =
visioncpp::terminal<visioncpp::pixel::
F32C3
,
COLS
,
ROWS
,
visioncpp::memory_type::Buffer2D>();
//
convert to float
auto
frgb =
visioncpp::point_operation<visioncpp::OP_U8C3ToF32C3>(in_node);
//
assign to temporary device memory
auto
exec1 =
visioncpp::assign
(device_memory, frgb);
//
apply anisotropic diffusion
auto
anidiff =
visioncpp::neighbour_operation<AniDiff,
1
,
1
,
1
,
1
>(device_memory);
//
assign to the temporary device memory
auto
exec2 =
visioncpp::assign
(device_memory, anidiff);
//
convert to uchar
auto
urgb =
visioncpp::point_operation<visioncpp::OP_F32C3ToU8C3>(device_memory);
//
assign to the host memory
auto
exec3 =
visioncpp::assign
(out_node, urgb);
//
execution (convert to float)
visioncpp::execute<visioncpp::policy::Fuse,
32
,
32
,
16
,
16
>(exec1, dev);
//
apply anisotropic diffusion several times
for
(
int
i =
0
; i < iters; i++) {
visioncpp::execute<visioncpp::policy::Fuse,
32
,
32
,
16
,
16
>(exec2, dev);
}
//
return image to host memory
visioncpp::execute<visioncpp::policy::Fuse,
32
,
32
,
16
,
16
>(exec3, dev);
}
//
display results
cv::imshow
(
"
Reference Image
"
, input);
cv::imshow
(
"
Simplified Anisotropic Diffusion
"
, outImage);
//
wait for key to finalize program
if
(
cv::waitKey
(
1
) >=
0
)
break
;
}
//
release video/camera
cap.
release
();
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL