-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopencv-import.cpp
More file actions
369 lines (298 loc) · 8.93 KB
/
opencv-import.cpp
File metadata and controls
369 lines (298 loc) · 8.93 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
Copyright (c) 2018-2019 Victor Erukhimov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "opencv-import.h"
#include "opencv2/opencv.hpp"
#include <string>
#if CV_VERSION_MAJOR > 3
#define CV_GRAY2RGB COLOR_GRAY2RGB
#define CV_RGB2GRAY COLOR_RGB2GRAY
#endif //CV_VERSION_MAJOR
#if defined(VXA_DEBUG)
#define VXA_PRINT(...) printf(__VA_ARGS__)
#pragma message("VXA_PRINT is on")
#else
#define VXA_PRINT(...)
#pragma message("VXA_PRINT is off")
#endif //VXA_PRINT
using namespace cv;
int vxa_import_opencv_remap(const char* filename, const char* nodename,
vx_context context, vx_remap* remap, int* _dst_width, int* _dst_height)
{
FileStorage fs(filename, FileStorage::READ);
Mat cv_remap;
fs[nodename] >> cv_remap;
int src_width, src_height, dst_width, dst_height;
fs[(std::string(nodename) + std::string("_src_width")).c_str()] >> src_width;
fs[(std::string(nodename) + std::string("_src_height")).c_str()] >> src_height;
fs[(std::string(nodename) + std::string("_dst_width")).c_str()] >> dst_width;
fs[(std::string(nodename) + std::string("_dst_height")).c_str()] >> dst_height;
if(cv_remap.type() != CV_32FC2)
{
return(0);
}
if(_dst_width != NULL)
{
*_dst_width = dst_width;
}
if(_dst_height != NULL)
{
*_dst_height = dst_height;
}
vx_rectangle_t roi;
roi.start_x = 0;
roi.start_y = 0;
roi.end_x = cv_remap.cols;
roi.end_y = cv_remap.rows;
*remap = vxCreateRemap(context, src_width, src_height,
dst_width, dst_height);
vxCopyRemapPatch(*remap, &roi, cv_remap.step, cv_remap.ptr(),
VX_TYPE_COORDINATES2DF, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST);
return(1);
}
int vxa_import_opencv_image(const char* filename, const char* nodename,
vx_context context, vx_image* image, int* _dst_width, int* _dst_height)
{
FileStorage fs(filename, FileStorage::READ);
Mat cv_img;
fs[nodename] >> cv_img;
*image = vxCreateImage(context, cv_img.cols, cv_img.rows,
VX_DF_IMAGE_S16);
vx_rectangle_t roi;
roi.start_x = 0;
roi.start_y = 0;
roi.end_x = cv_img.cols;
roi.end_y = cv_img.rows;
vx_imagepatch_addressing_t addr;
addr.dim_x = roi.start_x;
addr.dim_y = roi.start_y;
addr.stride_x = 2;
addr.stride_y = cv_img.step;
vx_status status = vxCopyImagePatch(*image, &roi, 0, &addr, cv_img.ptr(0),
VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST);
if(status != VX_SUCCESS)
{
VXA_PRINT("vxCopyImagePatch returned error with code %d\n", status);
return -1;
}
if(_dst_width != NULL)
{
*_dst_width = cv_img.cols;
}
if(_dst_height != NULL)
{
*_dst_height = cv_img.rows;
}
return(1);
}
int vxa_vx2cv(vx_image image, cv::Mat& cv_img)
{
int width, height;
vxQueryImage(image, VX_IMAGE_WIDTH, &width, 4);
vxQueryImage(image, VX_IMAGE_HEIGHT, &height, 4);
vx_df_image img_type;
vxQueryImage(image, VX_IMAGE_FORMAT, &img_type, 4);
int cv_type;
switch(img_type)
{
case VX_DF_IMAGE_U8:
cv_type = CV_8UC1;
break;
case VX_DF_IMAGE_RGB:
cv_type = CV_8UC3;
break;
case VX_DF_IMAGE_S16:
cv_type = CV_16SC1;
break;
default:
VXA_PRINT("Format %d not supported\n", img_type);
return(-1);
}
vx_rectangle_t roi;
roi.start_x = 0;
roi.start_y = 0;
roi.end_x = width;
roi.end_y = height;
vx_map_id map_id;
vx_imagepatch_addressing_t addr;
unsigned char* ptr;
vx_status status = vxMapImagePatch(image, &roi, 0, &map_id, &addr,
(void**)&ptr, VX_READ_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X);
if(status != VX_SUCCESS)
{
VXA_PRINT("vxMapImagePatch returned error with code %d\n", status);
return(-1);
}
if(addr.stride_x < 1 || addr.stride_x > 3)
{
VXA_PRINT("addressing structure not supported, stride_x = %d\n",
addr.stride_x);
return(0);
}
cv_img = cv::Mat(height, width, cv_type);
for(int y = 0; y < height; y++)
{
unsigned char* ptr_y = ptr + y*addr.stride_y;
memcpy(cv_img.ptr(y), ptr_y, addr.stride_y);
}
vxUnmapImagePatch(image, map_id);
return(1);
}
vx_image vxa_cv2vx(const cv::Mat& cv_img, vx_context context)
{
vx_df_image img_type;
switch(cv_img.type())
{
case CV_8UC1:
img_type = VX_DF_IMAGE_U8;
break;
case CV_8UC3:
img_type = VX_DF_IMAGE_RGB;
break;
default:
return(NULL);
}
int width = cv_img.cols, height = cv_img.rows;
vx_image image = vxCreateImage(context, width, height, img_type);
vx_rectangle_t roi;
roi.start_x = 0;
roi.start_y = 0;
roi.end_x = width;
roi.end_y = height;
vx_map_id map_id;
vx_imagepatch_addressing_t addr;
unsigned char* ptr;
vx_status status = vxMapImagePatch(image, &roi, 0, &map_id, &addr,
(void**)&ptr, VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X);
if(status != VX_SUCCESS)
{
VXA_PRINT("vxMapImagePatch returned error with code %d\n", status);
return(NULL);
}
if(addr.stride_x != 1 && addr.stride_x != 3)
{
VXA_PRINT("addressing structure not supported, stride_x = %d\n",
addr.stride_x);
return(0);
}
for(int y = 0; y < height; y++)
{
unsigned char* ptr_y = ptr + y*addr.stride_y;
memcpy(ptr_y, cv_img.ptr(y), addr.stride_y);
}
vxUnmapImagePatch(image, map_id);
return image;
}
int vxa_write_image(vx_image image, const char* filename)
{
cv::Mat cv_img;
vxa_vx2cv(image, cv_img);
imwrite(filename, cv_img);
return(1);
}
int vxa_read_image(const char* filename, vx_context context, vx_image* image)
{
Mat cv_img = imread(filename);
*image = vxa_cv2vx(cv_img, context);
return(1);
}
int draw_lines(vx_context context, vx_image image, vx_array lines, vx_size num_lines,
const vx_pixel_value_t* color, int thickness, vx_image* output)
{
/* both input and output images should be RGB */
vx_df_image img_type;
vxQueryImage(image, VX_IMAGE_FORMAT, &img_type, 4);
if(img_type != VX_DF_IMAGE_RGB && img_type != VX_DF_IMAGE_U8)
{
return(-1);
}
// convert to an opencv image
cv::Mat _cv_img, cv_img;
vxa_vx2cv(image, _cv_img);
switch(img_type)
{
case VX_DF_IMAGE_RGB:
cv_img = _cv_img;
break;
case VX_DF_IMAGE_U8:
cv::cvtColor(_cv_img, cv_img, CV_GRAY2RGB);
break;
default:
return(-1);
}
// allocate buffer for array data
vx_line2d_t* _lines = new vx_line2d_t[num_lines];
// copy data from array
vxCopyArrayRange(lines, 0, num_lines, sizeof(vx_line2d_t), _lines,
VX_READ_ONLY, VX_MEMORY_TYPE_HOST);
cv::Scalar _color = CV_RGB(color->RGB[0], color->RGB[1],
color->RGB[2]);
// draw the lines with OpenCV
for(int i = 0; i < num_lines; i++)
{
cv::Point pt1(_lines[i].start_x, _lines[i].start_y);
cv::Point pt2(_lines[i].end_x, _lines[i].end_y);
cv::line(cv_img, pt1, pt2, _color, thickness);
}
// convert back to vx_image
*output = vxa_cv2vx(cv_img, context);
delete[] _lines;
return 1;
}
int draw_circles(vx_context context, vx_image image, vx_array centers, vx_size num_circles,
int radius, const vx_pixel_value_t* color, int thickness, vx_image* output)
{
/* both input and output images should be RGB */
vx_df_image img_type;
vxQueryImage(image, VX_IMAGE_FORMAT, &img_type, 4);
if(img_type != VX_DF_IMAGE_RGB && img_type != VX_DF_IMAGE_U8)
{
return(-1);
}
// convert to an opencv image
cv::Mat _cv_img, cv_img;
vxa_vx2cv(image, _cv_img);
switch(img_type)
{
case VX_DF_IMAGE_RGB:
cv_img = _cv_img;
break;
case VX_DF_IMAGE_U8:
cv::cvtColor(_cv_img, cv_img, CV_GRAY2RGB);
break;
default:
return(-1);
}
// allocate buffer for array data
vx_coordinates2d_t* _centers = new vx_coordinates2d_t[num_circles];
// copy data from array
vxCopyArrayRange(centers, 0, num_circles, sizeof(vx_coordinates2d_t), _centers,
VX_READ_ONLY, VX_MEMORY_TYPE_HOST);
cv::Scalar _color = CV_RGB(color->RGB[0], color->RGB[1],
color->RGB[2]);
// draw the lines with OpenCV
for(int i = 0; i < num_circles; i++)
{
cv::Point pt(_centers[i].x, _centers[i].y);
cv::circle(cv_img, pt, radius, _color, thickness);
}
// convert back to vx_image
*output = vxa_cv2vx(cv_img, context);
delete[] _centers;
return 1;
}