VisionWorks Toolkit Reference

April 8, 2016 | 1.4 Release

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nvx_opencv_interop.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, NVIDIA CORPORATION. All rights reserved.
3  *
4  * NVIDIA Corporation and its licensors retain all intellectual property
5  * and proprietary rights in and to this software, related documentation
6  * and any modifications thereto. Any use, reproduction, disclosure or
7  * distribution of this software and related documentation without an express
8  * license agreement from NVIDIA Corporation is strictly prohibited.
9  */
10 
16 #ifndef NVX_OPENCV_INTEROP_HPP
17 #define NVX_OPENCV_INTEROP_HPP
18 
19 #ifndef __cplusplus
20 # error nvx_opencv_interop.hpp header must be compiled as C++
21 #endif
22 
23 #include <limits>
24 
25 #include <NVX/nvx.h>
26 #include <opencv2/core/core.hpp>
27 #include <opencv2/core/gpumat.hpp>
28 
32 namespace nvx_cv {
33 
42 inline int convertVXMatrixTypeToCVMatType(vx_enum matrix_type)
43 {
44  switch(matrix_type)
45  {
46  case VX_TYPE_UINT8:
47  return CV_8UC1;
48  case VX_TYPE_INT8:
49  return CV_8SC1;
50  case VX_TYPE_UINT16:
51  return CV_16UC1;
52  case VX_TYPE_INT16:
53  return CV_16SC1;
54  case VX_TYPE_UINT32:
55  case VX_TYPE_INT32:
56  return CV_32SC1;
57  case VX_TYPE_FLOAT32:
58  return CV_32FC1;
59  case VX_TYPE_FLOAT64:
60  return CV_64FC1;
61  case NVX_TYPE_POINT2F:
62  return CV_32FC2;
63  case NVX_TYPE_POINT3F:
64  return CV_32FC3;
65  }
66  CV_Error(CV_StsUnsupportedFormat, "Unsupported format");
67  return 0;
68 }
69 
79 {
80  switch(mat_type)
81  {
82  case CV_8UC1:
83  return VX_TYPE_UINT8;
84  case CV_8SC1:
85  return VX_TYPE_INT8;
86  case CV_16UC1:
87  return VX_TYPE_UINT16;
88  case CV_16SC1:
89  return VX_TYPE_INT16;
90  case CV_32SC1:
91  return VX_TYPE_INT32;
92  case CV_32FC1:
93  return VX_TYPE_FLOAT32;
94  case CV_64FC1:
95  return VX_TYPE_FLOAT64;
96  case CV_32FC2:
97  return NVX_TYPE_POINT2F;
98  case CV_32FC3:
99  return NVX_TYPE_POINT3F;
100  }
101  CV_Error(CV_StsUnsupportedFormat, "Unsupported format");
102  return 0;
103 }
104 
114 inline int convertVXImageFormatToCVMatType(vx_df_image format, vx_uint32 plane_index = 0)
115 {
116  switch (format)
117  {
118  case VX_DF_IMAGE_U8:
119  case VX_DF_IMAGE_YUV4:
120  case VX_DF_IMAGE_IYUV:
121  return CV_8UC1;
122  case VX_DF_IMAGE_U16:
123  return CV_16UC1;
124  case VX_DF_IMAGE_S16:
125  return CV_16SC1;
126  case VX_DF_IMAGE_U32:
127  case VX_DF_IMAGE_S32:
128  return CV_32SC1;
129  case NVX_DF_IMAGE_F32:
130  return CV_32FC1;
131  case NVX_DF_IMAGE_2F32:
132  return CV_32FC2;
133  case NVX_DF_IMAGE_2S16:
134  return CV_16SC2;
135  case VX_DF_IMAGE_UYVY:
136  case VX_DF_IMAGE_YUYV:
137  return CV_8UC2;
138  case VX_DF_IMAGE_RGB:
139  return CV_8UC3;
140  case NVX_DF_IMAGE_RGB16:
141  return CV_16SC3;
142  case VX_DF_IMAGE_RGBX:
143  return CV_8UC4;
144  case VX_DF_IMAGE_NV12:
145  case VX_DF_IMAGE_NV21:
146  return plane_index == 0 ? CV_8UC1 : CV_8UC2;
147  }
148  CV_Error(CV_StsUnsupportedFormat, "Unsupported format");
149  return 0;
150 }
151 
161 {
162  switch (mat_type)
163  {
164  case CV_8UC1:
165  return VX_DF_IMAGE_U8;
166  case CV_16UC1:
167  return VX_DF_IMAGE_U16;
168  case CV_16SC1:
169  return VX_DF_IMAGE_S16;
170  case CV_32SC1:
171  return VX_DF_IMAGE_S32;
172  case CV_32FC1:
173  return NVX_DF_IMAGE_F32;
174  case CV_32FC2:
175  return NVX_DF_IMAGE_2F32;
176  case CV_16SC2:
177  return NVX_DF_IMAGE_2S16;
178  case CV_8UC3:
179  return VX_DF_IMAGE_RGB;
180  case CV_8UC4:
181  return VX_DF_IMAGE_RGBX;
182  case CV_16SC3:
183  return NVX_DF_IMAGE_RGB16;
184  }
185  CV_Error(CV_StsUnsupportedFormat, "Unsupported format");
186  return 0;
187 }
188 
196 inline void copyVXMatrixToCVMat(vx_matrix src_mat, cv::Mat& dst_mat)
197 {
198  vx_status status = VX_SUCCESS;
199  vx_size rows_num = 0, cols_num = 0;
200  vx_enum elem_type = 0;
201 
202  status = vxQueryMatrix(src_mat, VX_MATRIX_ATTRIBUTE_TYPE, &elem_type, sizeof(elem_type) );
203  CV_Assert(status == VX_SUCCESS);
204 
205  status = vxQueryMatrix(src_mat, VX_MATRIX_ATTRIBUTE_ROWS, &rows_num, sizeof(rows_num) );
206  CV_Assert(status == VX_SUCCESS);
207  CV_Assert(rows_num <= static_cast<vx_size>(std::numeric_limits<int>::max()));
208 
209  status = vxQueryMatrix(src_mat, VX_MATRIX_ATTRIBUTE_COLUMNS, &cols_num, sizeof(cols_num) );
210  CV_Assert(status == VX_SUCCESS);
211  CV_Assert(cols_num <= static_cast<vx_size>(std::numeric_limits<int>::max()));
212 
213  dst_mat.create(static_cast<int>(rows_num), static_cast<int>(cols_num), convertVXMatrixTypeToCVMatType(elem_type));
214  CV_Assert(dst_mat.isContinuous());
215 
216  status = vxReadMatrix(src_mat, dst_mat.data);
217  CV_Assert(status == VX_SUCCESS);
218 }
219 
227 inline void copyCVMatToVXMatrix(const cv::Mat& src_mat, vx_matrix dst_mat)
228 {
229  vx_status status = VX_SUCCESS;
230  vx_size dst_rows_num = 0, dst_cols_num = 0;
231  vx_enum dst_elem_type = 0;
232 
233  status = vxQueryMatrix(dst_mat, VX_MATRIX_ATTRIBUTE_TYPE, &dst_elem_type, sizeof(dst_elem_type) );
234  CV_Assert(status == VX_SUCCESS);
235 
236  status = vxQueryMatrix(dst_mat, VX_MATRIX_ATTRIBUTE_ROWS, &dst_rows_num, sizeof(dst_rows_num) );
237  CV_Assert(status == VX_SUCCESS);
238 
239  status = vxQueryMatrix(dst_mat, VX_MATRIX_ATTRIBUTE_COLUMNS, &dst_cols_num, sizeof(dst_cols_num) );
240  CV_Assert(status == VX_SUCCESS);
241 
242  CV_Assert(src_mat.isContinuous());
243  CV_Assert(static_cast<vx_size>(src_mat.cols) == dst_cols_num && static_cast<vx_size>(src_mat.rows) == dst_rows_num);
244  CV_Assert(src_mat.type() == convertVXMatrixTypeToCVMatType(dst_elem_type));
245 
246  status = vxWriteMatrix(dst_mat, src_mat.data);
247  CV_Assert(status == VX_SUCCESS);
248 }
249 
259 inline vx_matrix cloneCVMatToVXMatrix(vx_context context, const cv::Mat& src_mat)
260 {
261  vx_matrix res_matrix = vxCreateMatrix(context, convertCVMatTypeToVXMatrixType(src_mat.type()),
262  src_mat.cols, src_mat.rows);
263  CV_Assert(vxGetStatus((vx_reference)res_matrix) == VX_SUCCESS);
264 
265  copyCVMatToVXMatrix(src_mat, res_matrix);
266 
267  return res_matrix;
268 }
269 
283 inline vx_image createVXImageFromCVMat(vx_context context, const cv::Mat& mat)
284 {
285  vx_df_image format = convertCVMatTypeToVXImageFormat(mat.type());
286 
288  addrs[0].dim_x = mat.cols;
289  addrs[0].dim_y = mat.rows;
290  addrs[0].stride_x = static_cast<vx_int32>(mat.elemSize());
291  addrs[0].stride_y = static_cast<vx_int32>(mat.step);
292 
293  void *ptrs[1] = { const_cast<uchar *>(mat.ptr()) };
294 
295  vx_image img = vxCreateImageFromHandle(context, format, addrs, ptrs, VX_IMPORT_TYPE_HOST);
296  CV_Assert(vxGetStatus((vx_reference)img) == VX_SUCCESS);
297 
298  return img;
299 }
300 
314 inline vx_image createVXImageFromCVGpuMat(vx_context context, const cv::gpu::GpuMat& mat)
315 {
316  vx_df_image format = convertCVMatTypeToVXImageFormat(mat.type());
317 
319  addrs[0].dim_x = mat.cols;
320  addrs[0].dim_y = mat.rows;
321  addrs[0].stride_x = static_cast<vx_int32>(mat.elemSize());
322  addrs[0].stride_y = static_cast<vx_int32>(mat.step);
323 
324  void *ptrs[1] = { const_cast<uchar *>(mat.ptr()) };
325 
326  vx_image img = vxCreateImageFromHandle(context, format, addrs, ptrs, NVX_IMPORT_TYPE_CUDA);
327  CV_Assert(vxGetStatus((vx_reference)img) == VX_SUCCESS);
328 
329  return img;
330 }
331 
342 {
343 public:
356  vx_uint32 plane_index = 0,
357  const vx_rectangle_t *rect = NULL,
358  vx_enum usage = VX_READ_AND_WRITE,
359  vx_enum memory_type = VX_IMPORT_TYPE_HOST) :
360  image_(image), plane_index_(plane_index), memory_type_(memory_type), ptr_(NULL)
361  {
362  vx_status status = VX_SUCCESS;
363 
364  if (rect)
365  {
366  rect_.start_x = rect->start_x;
367  rect_.start_y = rect->start_y;
368  rect_.end_x = rect->end_x;
369  rect_.end_y = rect->end_y;
370  }
371  else
372  {
373  rect_.start_x = rect_.start_y = 0;
374 
375  status = vxQueryImage(image_, VX_IMAGE_ATTRIBUTE_WIDTH, &rect_.end_x, sizeof(rect_.end_x));
376  CV_Assert(status == VX_SUCCESS);
377 
378  status = vxQueryImage(image_, VX_IMAGE_ATTRIBUTE_HEIGHT, &rect_.end_y, sizeof(rect_.end_y));
379  CV_Assert(status == VX_SUCCESS);
380  }
381 
382  vx_df_image format = VX_DF_IMAGE_VIRT;
383  status = vxQueryImage(image_, VX_IMAGE_ATTRIBUTE_FORMAT, &format, sizeof(format));
384  CV_Assert(status == VX_SUCCESS);
385 
386  int type = convertVXImageFormatToCVMatType(format, plane_index_);
387 
388  status = nvxMapImagePatch(image_, &rect_, plane_index_, &addr_, &ptr_, usage, memory_type_, NVX_MAP_NO_PIXEL_GAPS);
389  CV_Assert(status == VX_SUCCESS);
390 
391  int cols = addr_.dim_x * addr_.scale_x / VX_SCALE_UNITY;
392  int rows = addr_.dim_y * addr_.scale_y / VX_SCALE_UNITY;
393  int step = addr_.stride_y;
394 
395  if (memory_type_ == VX_IMPORT_TYPE_HOST)
396  {
397  cv_mat_ = cv::Mat(rows, cols, type, ptr_, step);
398  }
399  else if (memory_type_ == NVX_IMPORT_TYPE_CUDA)
400  {
401  cv_gpumat_ = cv::gpu::GpuMat(rows, cols, type, ptr_, step);
402  }
403  }
404 
409  {
410  nvxUnmapImagePatch(image_, &rect_, plane_index_, ptr_, memory_type_);
411  }
412 
418  cv::Mat getMat()
419  {
420  CV_Assert(!cv_mat_.empty());
421  return cv_mat_;
422  }
423 
429  cv::gpu::GpuMat getGpuMat()
430  {
431  CV_Assert(!cv_gpumat_.empty());
432  return cv_gpumat_;
433  }
434 
435 private:
436  vx_image image_;
437  vx_uint32 plane_index_;
438  vx_enum memory_type_;
439 
440  vx_rectangle_t rect_;
442  void* ptr_;
443 
444  cv::Mat cv_mat_;
445  cv::gpu::GpuMat cv_gpumat_;
446 };
447 
448 }
449 
450 #endif
struct _vx_image * vx_image
An opaque reference to an image.
Definition: vx_types.h:186
VXImageToCVMatMapper(vx_image image, vx_uint32 plane_index=0, const vx_rectangle_t *rect=NULL, vx_enum usage=VX_READ_AND_WRITE, vx_enum memory_type=VX_IMPORT_TYPE_HOST)
Maps input OpenVX Image into OpenCV Mat or GpuMat without memory copy.
vx_status VX_API_CALL vxQueryImage(vx_image image, vx_enum attribute, void *ptr, vx_size size)
Retrieves various attributes of an image.
vx_status VX_API_CALL vxWriteMatrix(vx_matrix mat, const void *array)
Sets the matrix data (copy)
A vx_int16.
Definition: vx_types.h:319
int convertVXMatrixTypeToCVMatType(vx_enum matrix_type)
Converts from OpenVX Matrix' type to OpenCV Mat's type.
vx_uint32 scale_y
Scale of Y dimension. For sub-sampled planes this is the scaling factor of the dimension of the plane...
Definition: vx_types.h:1335
#define VX_SCALE_UNITY
Definition: vx_types.h:1307
vx_status nvxUnmapImagePatch(vx_image image, const vx_rectangle_t *rect, vx_uint32 plane_index, void *ptr, vx_enum memory_type)
Unmaps a mapped patch (subset) of an image.
size_t vx_size
A wrapper of size_t to keep the naming convention uniform.
Definition: vx_types.h:166
vx_df_image convertCVMatTypeToVXImageFormat(int mat_type)
Converts from OpenCV Mat's type to OpenVX Image's format.
A 2-lane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling. This uses the BT709 full r...
Definition: vx_types.h:649
int32_t vx_enum
Sets the standard enumeration type size to be a fixed quantity.
Definition: vx_types.h:161
No error.
Definition: vx_types.h:414
vx_status VX_API_CALL vxQueryMatrix(vx_matrix mat, vx_enum attribute, void *ptr, vx_size size)
Queries an attribute on the matrix object.
int32_t vx_int32
A 32-bit signed value.
Definition: vx_types.h:120
NVIDIA VisionWorks Framework and Primitives API.
vx_image VX_API_CALL vxCreateImageFromHandle(vx_context context, vx_df_image color, vx_imagepatch_addressing_t addrs[], void *ptrs[], vx_enum import_type)
Creates a reference to an image object that was externally allocated.
A single plane of unsigned 32-bit data. The range of data is not specified, as it may be extracted fr...
Definition: vx_types.h:685
A single plane of unsigned 8-bit data. The range of data is not specified, as it may be extracted fro...
Definition: vx_types.h:670
vx_status nvxMapImagePatch(vx_image image, const vx_rectangle_t *rect, vx_uint32 plane_index, vx_imagepatch_addressing_t *addr, void **ptr, vx_enum usage, vx_enum memory_type, vx_uint32 flags)
Maps a rectangular patch (subset) of an image from a single plane.
A single plane of 48 bit pixels as 3 interleaved 16 bit signed integers of R then G then B data...
Definition: nvx.h:257
A vx_float64.
Definition: vx_types.h:326
vx_status VX_API_CALL vxReadMatrix(vx_matrix mat, void *array)
Gets the matrix data (copy).
A vx_float32.
Definition: vx_types.h:325
vx_enum vx_status
A formal status type with known fixed size.
Definition: vx_types.h:421
vx_image createVXImageFromCVMat(vx_context context, const cv::Mat &mat)
Creates OpenVX Image from OpenCV Mat without data copy.
struct _vx_context * vx_context
An opaque reference to the implementation context.
Definition: vx_types.h:222
vx_enum convertCVMatTypeToVXMatrixType(int mat_type)
Converts from OpenCV Mat's type to OpenVX Matrix' type.
~VXImageToCVMatMapper()
Unmaps a mapped region of an OpenVX Image.
#define NVX_MAP_NO_PIXEL_GAPS
Maps into a buffer without gaps between pixels in a row.
Definition: nvx.h:287
vx_uint32 start_x
The Start X coordinate.
Definition: vx_types.h:1423
struct _vx_reference * vx_reference
A generic opaque reference to any object within OpenVX.
Definition: vx_types.h:154
A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes. This uses the BT709 full range by defau...
Definition: vx_types.h:657
Queries an image for its width. Use a vx_uint32 parameter.
Definition: vx_types.h:824
void copyVXMatrixToCVMat(vx_matrix src_mat, cv::Mat &dst_mat)
Copies data from OpenVX Matrix to OpenCV Mat.
uint32_t vx_df_image
Used to hold a VX_DF_IMAGE code to describe the pixel format and color space.
Definition: vx_types.h:171
A single plane of 24-bit pixel as 3 interleaved 8-bit units of R then G then B data. This uses the BT709 full range by default.
Definition: vx_types.h:636
A virtual image of no defined type.
Definition: vx_types.h:632
vx_uint32 dim_y
Height of patch in Y dimension in pixels.
Definition: vx_types.h:1331
A nvx_point3f_t.
Definition: nvx.h:115
void copyCVMatToVXMatrix(const cv::Mat &src_mat, vx_matrix dst_mat)
Copies data from OpenCV Mat to OpenVX Matrix.
The addressing image patch structure is used by the Host only to address pixels in an image patch...
Definition: vx_types.h:1329
A single plane of 32-bit pixel as 4 interleaved 8-bit units of R then G then B data, then a don't care byte. This uses the BT709 full range by default.
Definition: vx_types.h:641
The memory shall be treated by the system as if it were readable and writeable.
Definition: vx_types.h:1190
The M dimension of the matrix. Use a vx_size parameter.
Definition: vx_types.h:926
Queries an image for its format. Use a vx_df_image parameter.
Definition: vx_types.h:826
The default memory type to import from the Host.
Definition: vx_types.h:1043
Queries an image for its height. Use a vx_uint32 parameter.
Definition: vx_types.h:822
Utility class to map OpenVX Image into OpenCV Mat or GpuMat.
vx_uint32 start_y
The Start Y coordinate.
Definition: vx_types.h:1424
A single plane of vx_int16[2] data (eg.
Definition: nvx.h:251
vx_uint32 dim_x
Width of patch in X dimension in pixels.
Definition: vx_types.h:1330
The CUDA import memory type.
Definition: nvx.h:369
vx_matrix cloneCVMatToVXMatrix(vx_context context, const cv::Mat &src_mat)
Creates OpenVX Matrix object and copies data from OpenCV Mat.
A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 bytes. This uses the BT709 full range by defau...
Definition: vx_types.h:653
A vx_int8.
Definition: vx_types.h:317
The rectangle data structure that is shared with the users.
Definition: vx_types.h:1422
vx_int32 stride_y
Stride in Y dimension in bytes.
Definition: vx_types.h:1333
A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes. This uses the BT709 full range by default...
Definition: vx_types.h:665
vx_uint32 end_x
The End X coordinate.
Definition: vx_types.h:1425
The N dimension of the matrix. Use a vx_size parameter.
Definition: vx_types.h:928
A single plane of vx_float32 data.
Definition: nvx.h:245
vx_image createVXImageFromCVGpuMat(vx_context context, const cv::gpu::GpuMat &mat)
Creates OpenVX Image from OpenCV GpuMat without data copy.
cv::Mat getMat()
Gets mapped OpenCV Mat.
A single plane of signed 16-bit data. The range of data is not specified, as it may be extracted from...
Definition: vx_types.h:680
uint32_t vx_uint32
A 32-bit unsigned value.
Definition: vx_types.h:100
A 2-plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling. This uses the BT709 full ...
Definition: vx_types.h:645
The value type of the matrix. Use a vx_enum parameter.
Definition: vx_types.h:924
A nvx_point2f_t.
Definition: nvx.h:114
cv::gpu::GpuMat getGpuMat()
Gets mapped OpenCV GpuMat.
A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes. This uses the BT709 full range by default...
Definition: vx_types.h:661
A single plane of vx_float32[2] data (eg.
Definition: nvx.h:248
A vx_int32.
Definition: vx_types.h:321
A vx_uint32.
Definition: vx_types.h:322
vx_uint32 end_y
The End Y coordinate.
Definition: vx_types.h:1426
struct _vx_matrix * vx_matrix
The Matrix Object. An MxN matrix of some unit type.
Definition: vx_types.h:249
int convertVXImageFormatToCVMatType(vx_df_image format, vx_uint32 plane_index=0)
Converts from OpenVX Image's format to OpenCV Mat's type.
A single plane of unsigned 16-bit data. The range of data is not specified, as it may be extracted fr...
Definition: vx_types.h:675
A single plane of unsigned 32-bit data. The range of data is not specified, as it may be extracted fr...
Definition: vx_types.h:690
vx_uint32 scale_x
Scale of X dimension. For sub-sampled planes this is the scaling factor of the dimension of the plane...
Definition: vx_types.h:1334
A vx_uint16.
Definition: vx_types.h:320
vx_status VX_API_CALL vxGetStatus(vx_reference reference)
Provides a generic API to return status values from Object constructors if they fail.
A vx_uint8.
Definition: vx_types.h:318
vx_matrix VX_API_CALL vxCreateMatrix(vx_context c, vx_enum data_type, vx_size columns, vx_size rows)
Creates a reference to a matrix object.