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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <iostream>
#include <stdexcept>
//wrappers
#include "ivx.hpp"
//OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
enum UserMemoryMode
{
COPY, USER_MEM, MAP
};
ivx::Graph createProcessingGraph(ivx::Image& inputImage, ivx::Image& outputImage);
int ovxDemo(std::string inputPath, UserMemoryMode mode);
ivx::Graph createProcessingGraph(ivx::Image& inputImage, ivx::Image& outputImage)
{
using namespace ivx;
Context context = inputImage.get<Context>();
Graph graph = Graph::create(context);
vx_uint32 width = inputImage.width();
vx_uint32 height = inputImage.height();
// Intermediate images
Image
yuv = Image::createVirtual(graph, 0, 0, VX_DF_IMAGE_YUV4),
gray = Image::createVirtual(graph),
smoothed = Image::createVirtual(graph),
cannied = Image::createVirtual(graph),
halfImg = Image::create(context, width, height, VX_DF_IMAGE_U8),
halfCanny = Image::create(context, width, height, VX_DF_IMAGE_U8);
// Constants
vx_uint32 threshCannyMin = 127;
vx_uint32 threshCannyMax = 192;
Threshold threshCanny = Threshold::createRange(context, VX_TYPE_UINT8, threshCannyMin, threshCannyMax);
ivx::Scalar alpha = ivx::Scalar::create<VX_TYPE_FLOAT32>(context, 0.5);
// Sequence of some image operations
Node::create(graph, VX_KERNEL_COLOR_CONVERT, inputImage, yuv);
Node::create(graph, VX_KERNEL_CHANNEL_EXTRACT, yuv,
ivx::Scalar::create<VX_TYPE_ENUM>(context, VX_CHANNEL_Y), gray);
//node can also be added in function-like style
nodes::gaussian3x3(graph, gray, smoothed);
Node::create(graph, VX_KERNEL_CANNY_EDGE_DETECTOR, smoothed, threshCanny,
ivx::Scalar::create<VX_TYPE_INT32>(context, 3),
ivx::Scalar::create<VX_TYPE_ENUM>(context, VX_NORM_L2), cannied);
Node::create(graph, VX_KERNEL_ACCUMULATE_WEIGHTED, gray, alpha, halfImg);
Node::create(graph, VX_KERNEL_ACCUMULATE_WEIGHTED, cannied, alpha, halfCanny);
Node::create(graph, VX_KERNEL_ADD, halfImg, halfCanny,
ivx::Scalar::create<VX_TYPE_ENUM>(context, VX_CONVERT_POLICY_SATURATE), outputImage);
graph.verify();
return graph;
}
int ovxDemo(std::string inputPath, UserMemoryMode mode)
{
using namespace cv;
using namespace ivx;
Mat frame;
VideoCapture vc(inputPath);
if (!vc.isOpened())
return -1;
vc >> frame;
if (frame.empty()) return -1;
//check frame format
if (frame.type() != CV_8UC3) return -1;
try
{
Context context = Context::create();
//put user data from cv::Mat to vx_image
vx_df_image color = Image::matTypeToFormat(frame.type());
vx_uint32 width = frame.cols, height = frame.rows;
Image ivxImage;
if (mode == COPY)
{
ivxImage = Image::create(context, width, height, color);
}
else
{
ivxImage = Image::createFromHandle(context, color, Image::createAddressing(frame), frame.data);
}
Image ivxResult;
Mat output;
if (mode == COPY || mode == MAP)
{
//we will copy or map data from vx_image to cv::Mat
ivxResult = ivx::Image::create(context, width, height, VX_DF_IMAGE_U8);
}
else // if (mode == MAP_TO_VX)
{
//create vx_image based on user data, no copying required
output = cv::Mat(height, width, CV_8U, cv::Scalar(0));
ivxResult = Image::createFromHandle(context, Image::matTypeToFormat(CV_8U),
Image::createAddressing(output), output.data);
}
Graph graph = createProcessingGraph(ivxImage, ivxResult);
bool stop = false;
while (!stop)
{
if (mode == COPY) ivxImage.copyFrom(0, frame);
// Graph execution
graph.process();
//getting resulting image in cv::Mat
Image::Patch resultPatch;
std::vector<void*> ptrs;
std::vector<void*> prevPtrs(ivxResult.planes());
if (mode == COPY)
{
ivxResult.copyTo(0, output);
}
else if (mode == MAP)
{
//create cv::Mat based on vx_image mapped data
resultPatch.map(ivxResult, 0, ivxResult.getValidRegion(), VX_READ_AND_WRITE);
//generally this is very bad idea!
//but in our case unmap() won't happen until output is in use
output = resultPatch.getMat();
}
else // if(mode == MAP_TO_VX)
{
#ifdef VX_VERSION_1_1
//we should take user memory back from vx_image before using it (even before reading)
ivxResult.swapHandle(ptrs, prevPtrs);
#endif
}
//here output goes
imshow("press q to quit", output);
if ((char)waitKey(1) == 'q') stop = true;
#ifdef VX_VERSION_1_1
//restore handle
if (mode == USER_MEM)
{
ivxResult.swapHandle(prevPtrs, ptrs);
}
#endif
//this line is unnecessary since unmapping is done on destruction of patch
//resultPatch.unmap();
//grab next frame
Mat temp = frame;
vc >> frame;
if (frame.empty()) stop = true;
if (mode != COPY && frame.data != temp.data)
{
//frame was reallocated, pointer to data changed
frame.copyTo(temp);
}
}
destroyAllWindows();
#ifdef VX_VERSION_1_1
if (mode != COPY)
{
//we should take user memory back before release
//(it's not done automatically according to standard)
ivxImage.swapHandle();
if (mode == USER_MEM) ivxResult.swapHandle();
}
#endif
}
catch (const ivx::RuntimeError& e)
{
std::cerr << "Error: code = " << e.status() << ", message = " << e.what() << std::endl;
return e.status();
}
catch (const ivx::WrapperError& e)
{
std::cerr << "Error: message = " << e.what() << std::endl;
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
const std::string keys =
"{help h usage ? | | }"
"{video | <none> | video file to be processed}"
"{mode | copy | user memory interaction mode: \n"
"copy: create VX images and copy data to/from them\n"
"user_mem: use handles to user-allocated memory\n"
"map: map resulting VX image to user memory}"
;
cv::CommandLineParser parser(argc, argv, keys);
parser.about("OpenVX interoperability sample demonstrating OpenVX wrappers usage."
"The application opens a video and processes it with OpenVX graph while outputting result in a window");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
std::string videoPath = parser.get<std::string>("video");
std::string modeString = parser.get<std::string>("mode");
UserMemoryMode mode;
if(modeString == "copy")
{
mode = COPY;
}
else if(modeString == "user_mem")
{
mode = USER_MEM;
}
else if(modeString == "map")
{
mode = MAP;
}
else
{
std::cerr << modeString << ": unknown memory mode" << std::endl;
return -1;
}
if (!parser.check())
{
parser.printErrors();
return -1;
}
return ovxDemo(videoPath, mode);
}