VisionWorks Toolkit Reference

April 8, 2016 | 1.4 Release

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Conversion Between OpenCV and OpenVX Data Types

This section demonstrates how to convert between OpenCV and OpenVX data types.

To simplify interaction with OpenCV, VisionWorks provides auxiliary header nvx_opencv_interop.hpp, which contains utility function to convert between OpenCV and OpenVX data types.

#include <NVX/nvx.h>
#include <opencv2/opencv.hpp>

Convert from OpenCV Data Types to OpenVX Data Types

OpenCV uses cv::Mat data type as the main data type for all operations. The cv::Mat object can be converted to vx_matrix and vx_image objects.

Copy cv::Mat to vx_matrix

The following code example shows how to copy cv::Mat object into vx_matrix object. Since it is a copy, vx_matrix object is fully independent from the source cv::Mat object.

cv::Mat fundamMat = cv::findFundamentalMat(points1, points2);
vx_matrix vx_fundamMat_1 = vxCreateMatrix(context, VX_TYPE_FLOAT64, 3, 3);
nvx_cv::copyCVMatToVXMatrix(fundamMat, vx_fundamMat_1);
// or
vx_matrix vx_fundamMat_2 = nvx_cv::cloneCVMatToVXMatrix(context, fundamMat);

Import cv::Mat or cv::gpu::GpuMat to vx_image

The following code example shows how to import cv::Mat object into vx_image object without memory copy. The vx_image object points to the same pixels as the cv::Mat. The code internally uses the vxCreateImageFromHandle function and has the same limitations (see Image Data Object Extensions):

cv::Mat cv_src = cv::imread(fileNameSrc, cv::IMREAD_GRAYSCALE);
cv::Mat cv_dst(cv_src.size(), cv_src.type());
vx_image vx_src = nvx_cv::createVXImageFromCVMat(context, cv_src);
vx_image vx_dst = nvx_cv::createVXImageFromCVMat(context, cv_dst);
vxuBox3x3(context, vx_src, vx_dst);
vxReleaseImage(&vx_src);
vxReleaseImage(&vx_dst);
cv::imwrite(fileNameDst, cv_dst);
cv_src.release();
cv_dst.release();

cv::gpu::GpuMat can be imported as well with the same limitations:

cv::gpu::GpuMat cv_gpumat(480, 640, CV_8UC1);
vx_image vx_img = nvx_cv::createVXImageFromCVGpuMat(context, cv_gpumat);
// use vx_img
vxReleaseImage(&vx_img);

Convert from OpenVX Data Types to OpenCV Data Types

vx_matrix and vx_image objects can be converted in cv::Mat object.

Copy vx_matrix to cv::Mat

The following code example shows how to copy vx_matrix object into cv::Mat object. Since it is a copy, cv::Mat object is fully independent from the source vx_matrix object.

vx_matrix vx_H = vxCreateMatrix(context, VX_TYPE_FLOAT32, 3, 3);
nvxuFindHomography(context, srcPoints, dstPoints, vx_H,
NVX_FIND_HOMOGRAPHY_METHOD_RANSAC, 3, 2000, 10, 0.995, 0.45, NULL);
cv::Mat cv_H;

Map vx_image into cv::Mat or cv::gpu::GpuMat

The following code example shows how to map vx_image object into cv::Mat object without memory copy. The cv::Mat object points to the same pixels as the vx_image object. The nvx_cv::VXImageToCVMatMapper class internally uses the nvxMapImagePatch method and has the same limitations:

vx_image vx_img = vxCreateImage(context, 640, 480, VX_DF_IMAGE_U8);
{
vx_uint32 plane_index = 0;
vx_rectangle_t rect = {
0u, 0u,
640u, 480u
};
cv::Mat cv_img = map.getMat();
cv::randu(cv_img, 0, 255);
// nvx_cv::ImageToMatMapper destructor will call unmap internally
}
// use vx_img
vxReleaseImage(&vx_img);

vx_image can be mapped to cv::gpu::GpuMat object as well:

vx_image vx_img = vxCreateImage(context, 640, 480, VX_DF_IMAGE_U8);
{
vx_uint32 plane_index = 0;
vx_rectangle_t rect = {
0u, 0u,
640u, 480u
};
cv::gpu::GpuMat cv_gpuimg = map.getGpuMat();
cv_gpuimg.setTo(cv::Scalar::all(0));
// nvx_cv::ImageToMatMapper destructor will call unmap internally
}
// use vx_img
vxReleaseImage(&vx_img);