-
Notifications
You must be signed in to change notification settings - Fork 0
/
vx_training_10.cc
328 lines (271 loc) · 10.7 KB
/
vx_training_10.cc
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* Copyright (C) 2022 RidgeRun, LLC (http://www.ridgerun.com)
* All Rights Reserved.
*
* The contents of this software are proprietary and confidential to RidgeRun,
* LLC. No part of this program may be photocopied, reproduced or translated
* into another programming language without prior written consent of
* RidgeRun, LLC. The user is free to modify the source code after obtaining
* a software license from RidgeRun. All source code changes must be provided
* back to RidgeRun without any encumbrance.
*/
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <cmath>
#include <iostream>
#include <memory>
#include <opencv2/opencv.hpp>
#include <vector>
#include <VX/vx_khr_pipelining.h>
#include <VX/vx.h>
template<typename T>
static std::shared_ptr<T>
smart_ref (T *ptr)
{
return std::shared_ptr<T> (ptr, [](T *ptr) {
vxReleaseReference ((vx_reference *)&ptr);
});
}
static int
populate_image (vx_image image, const unsigned char *img_data)
{
int ret = -1;
vx_uint32 width = 0;
vx_uint32 height = 0;
vx_int32 channels = 3;
vxQueryImage (image, VX_IMAGE_WIDTH, &width, sizeof (width));
vxQueryImage (image, VX_IMAGE_HEIGHT, &height, sizeof (height));
vx_imagepatch_addressing_t layout = { width, height, channels,
static_cast<vx_int32>(width*channels), 0, 0, 0, 0 };
const vx_rectangle_t rect = { 0, 0, width, height };
vx_status status = vxCopyImagePatch (image, &rect, 0, &layout, (void *)img_data,
VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST);
if (VX_SUCCESS != status) {
std::cerr << "vx-training: Unable to copy data into image: " << status << std::endl;
ret = -1;
} else {
ret = 0;
}
return ret;
}
static int
show_image (vx_image image)
{
int ret = -1;
vx_uint32 width = 0;
vx_uint32 height = 0;
vxQueryImage (image, VX_IMAGE_WIDTH, &width, sizeof (width));
vxQueryImage (image, VX_IMAGE_HEIGHT, &height, sizeof (height));
const vx_rectangle_t rect = { 0, 0, width, height };
vx_uint32 plane = 0;
vx_map_id map_id = 0;
vx_imagepatch_addressing_t addr = { 0 };
unsigned char *ptr = 0;
vx_enum usage = VX_READ_ONLY;
vx_enum mem_type = VX_MEMORY_TYPE_HOST;
vx_uint32 flags = VX_NOGAP_X;
vx_status status = vxMapImagePatch (image, &rect, plane, &map_id, &addr, (void **)&ptr, usage, mem_type, flags);
if (VX_SUCCESS != status) {
std::cerr << "vx-training: Unable to map image for reading:" << status << std::endl;
ret = -1;
} else {
cv::Mat mat (height, width, CV_8UC1, ptr, addr.stride_y);
cv::imshow ("Processed image", mat);
ret = 0;
}
vxUnmapImagePatch (image, map_id);
return ret;
}
static void VX_CALLBACK
context_log_callback(vx_context context, vx_reference ref, vx_status status,
const vx_char string[])
{
std::cout << "vx-training [dbg]: " << string << std::endl;
}
int
main (int argc, char *argv[])
{
const char *filename = "lena.png";
if (argc >= 2) {
filename = argv[1];
}
const char *outname = "out.mp4";
if (argc >= 3) {
outname = argv[2];
}
auto context = smart_ref (vxCreateContext ());
vx_status status = vxGetStatus ((vx_reference)context.get ());
if (VX_SUCCESS != status) {
std::cerr << "vx-training: Unable to create context:" << status << std::endl;
return -1;
}
vx_bool reentrant = vx_false_e;
vxRegisterLogCallback(context.get (), context_log_callback, reentrant);
int width = 0;
int height = 0;
int channels = 0;
auto img_data = std::shared_ptr<unsigned char>(stbi_load (filename, &width, &height, &channels, 3), free);
if (NULL == img_data) {
std::cerr << "vx-training: Unable to load image " << filename << std::endl;
return -1;
}
const int num_images = 32;
std::vector <std::shared_ptr<_vx_image>> in_images;
for (int i= 0; i < num_images; i++) {
auto in_image = smart_ref(vxCreateImage(context.get (), width, height, VX_DF_IMAGE_RGB));
status = vxGetStatus ((vx_reference)in_image.get ());
if (VX_SUCCESS != status) {
std::cerr << "vx-training: Unable to create input image: " << status << std::endl;
return -1;
}
if (0 != populate_image (in_image.get (), img_data.get ())) {
std::cerr << "vx-training: Unable to fill input image" << std::endl;
return -1;
}
in_images.push_back (in_image);
}
std::vector <std::shared_ptr<_vx_image>> out_images;
for (int i= 0; i < num_images; i++) {
auto out_image = smart_ref(vxCreateImage(context.get (), width, height, VX_DF_IMAGE_U8));
status = vxGetStatus ((vx_reference)out_image.get ());
if (VX_SUCCESS != status) {
std::cerr << "vx-training: Unable to create input image: " << status << std::endl;
return -1;
}
out_images.push_back (out_image);
}
auto graph = smart_ref (vxCreateGraph (context.get ()));
status = vxGetStatus ((vx_reference)graph.get ());
if (VX_SUCCESS != status) {
std::cerr << "vx-training: Unable to create graph: " << status << std::endl;
return -1;
}
auto intermediate = smart_ref (vxCreateVirtualImage(graph.get (), width, height, VX_DF_IMAGE_U8));
status = vxGetStatus ((vx_reference)intermediate.get ());
if (VX_SUCCESS != status) {
std::cerr << "vx-training: Unable to create virtual image: " << status << std::endl;
return -1;
}
auto matrix = smart_ref (vxCreateMatrix(context.get (), VX_TYPE_FLOAT32, 2, 3));
vx_enum interpolation = VX_INTERPOLATION_BILINEAR;
std::vector<std::shared_ptr<_vx_node>> nodes = {
// Input image will now be a parameter
smart_ref (vxChannelExtractNode (graph.get (), in_images[0].get (), VX_CHANNEL_R, intermediate.get ())),
// Ouput image will now be a parameters
smart_ref (vxWarpAffineNode (graph.get (), intermediate.get (), matrix.get (), interpolation, out_images[0].get ()))
};
for (auto &node: nodes) {
status = vxGetStatus ((vx_reference)node.get ());
if (VX_SUCCESS != status) {
std::cerr << "vx-training: Unable to create processing node: " << status << std::endl;
return -1;
}
}
vx_parameter parameter = vxGetParameterByIndex(nodes[0].get(), 0);
vxAddParameterToGraph(graph.get (), parameter);
vxReleaseParameter(¶meter);
parameter = vxGetParameterByIndex(nodes[1].get(), 3);
vxAddParameterToGraph(graph.get (), parameter);
vxReleaseParameter(¶meter);
vx_image in_refs[num_images];
for (int i=0; i<num_images; ++i) {
in_refs[i] = in_images[i].get ();
}
vx_image out_refs[num_images];
for (int i=0; i<num_images; ++i) {
out_refs[i] = out_images[i].get ();
}
std::vector<vx_graph_parameter_queue_params_t> queue_params_list(2);
queue_params_list[0].graph_parameter_index = 0;
queue_params_list[0].refs_list_size = in_images.size();
queue_params_list[0].refs_list = (vx_reference*)&in_refs[0];
queue_params_list[1].graph_parameter_index = 1;
queue_params_list[1].refs_list_size = out_images.size();
queue_params_list[1].refs_list = (vx_reference*)&out_refs[0];
vxSetGraphScheduleConfig(graph.get (), VX_GRAPH_SCHEDULE_MODE_QUEUE_AUTO,
queue_params_list.size(), queue_params_list.data());
status = vxVerifyGraph (graph.get ());
if (VX_SUCCESS != status) {
std::cerr << "vx-training: Graph validation failed: " << status << std::endl;
return -1;
}
/*
Images in OpenVX have the origin of the coordinate system in the
upper left corner. Images will rotate around the origin. To rotate
around the center we need to translate the image so that the origin
matches the center, rotate, and translate back. In linear algebra,
this is achieved by multiplying transformation matrices, were
they are applied from right to left.
[1 0 w/2] [ cos(a) -sin(a) 0 ] [1 0 -w/2]
R = [0 1 h/2] [ sin(a) sin(a) 0 ] [0 1 -h/2]
[0 0 1] [ 0 0 1 ] [0 0 1]
[ cos(a) -sin(a) -cos(a)*w/2 + sin(a)*h/2 + w/2 ]
R = [ sin(a) sin(a) -cos(a)*h/2 - sin(a)*h/2 + h/2 ]
[ 0 0 1 ]
*/
vx_float32 angle = 180;
vx_float32 rad = angle*M_PI/180.0;
/* Rotation only matrix */
//vx_float32 mat[3][2] = {
// {cos (rad), sin (rad)},
// {-sin (rad), cos (rad)},
// {0, 0},
//};
/* Translate + rotate + translate back */
vx_float32 mat[3][2] = {
{cos (rad), sin (rad)},
{-sin (rad), cos (rad)},
{-cos (rad)*width/2 + sin (rad)*height/2 + width/2, -cos (rad)*height/2 - sin (rad)*width/2 + height/2},
};
vxCopyMatrix(matrix.get (), mat, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST);
status = vxGraphParameterEnqueueReadyRef(graph.get (), 0, (vx_reference*)&in_refs[0],
in_images.size ());
if (VX_SUCCESS != status) {
std::cerr << "vx-training: Unable to enqueue input images: " << status << std::endl;
return -1;
}
vx_uint32 num_refs;
status = vxGraphParameterDequeueDoneRef(graph.get (), 1, (vx_reference*)out_refs[0],
out_images.size (), &num_refs);
if (VX_SUCCESS != status) {
std::cerr << "vx-training: Unable to dequeue output images: " << status << std::endl;
return -1;
}
/*
* wait until all previous graph executions have completed
*/
vxWaitGraph(graph.get ());
std::cout << "Processed " << num_refs << " images in a single batch!" << std::endl;
cv::namedWindow ("Processed image", cv::WINDOW_AUTOSIZE);
for (const auto &image: out_images) {
show_image (image.get ());
cv::waitKey(30);
}
vx_perf_t perf;
vxQueryGraph(graph.get (), VX_GRAPH_PERFORMANCE, &perf, sizeof(perf));
std::cout << "Graph performance:" << std::endl;
std::cout << "\tLast measurement: " << perf.tmp << std::endl;
std::cout << "\tFirst measurement of the set: " << perf.beg << std::endl;
std::cout << "\tLast measurement of the set: " << perf.end << std::endl;
std::cout << "\tAverage of durations: " << perf.avg << std::endl;
std::cout << "\tMinimum of durations: " << perf.min << std::endl;
std::cout << "\tMaximum of durations: " << perf.max << std::endl;
std::cout << "\tSum of durations: " << perf.sum << std::endl;
std::cout << "\tNumber of measurements: " << perf.num << std::endl;
std::cout << "\t---" << std::endl;
for (auto &node: nodes) {
vx_perf_t perf;
vxQueryNode(node.get (), VX_NODE_PERFORMANCE, &perf, sizeof(perf));
std::cout << "Node performance:" << std::endl;
std::cout << "\tLast measurement: " << perf.tmp << std::endl;
std::cout << "\tFirst measurement of the set: " << perf.beg << std::endl;
std::cout << "\tLast measurement of the set: " << perf.end << std::endl;
std::cout << "\tAverage of durations: " << perf.avg << std::endl;
std::cout << "\tMinimum of durations: " << perf.min << std::endl;
std::cout << "\tMaximum of durations: " << perf.max << std::endl;
std::cout << "\tSum of durations: " << perf.sum << std::endl;
std::cout << "\tNumber of measurements: " << perf.num << std::endl;
std::cout << "\t---" << std::endl;
}
cv::destroyAllWindows ();
return 0;
}