image copy using OpenCV and Visionworks nvxuCopyImage function

I am using OpenCV, and want to accelerate some image processings using the GPU.
So i’ve decided to test some functions from the Visionworks SDK, and i’ve first tried to use some basic functions like ‘nvxuCopyImage’ for copying images.
The simple code below does not return any error, but the destination image is completely BLACK.
I am surely missing something, any help ? Thanks.!

// create Visionworks context
m_vxCtx = vxCreateContext();

// load SOURCE image in 8-bit grayscale format
cv::Mat matSrc;
matSrc = cv::imread("/home/ubuntu/cap.png", cv::IMREAD_GRAYSCALE);
// wrap OpenCV SOURCE image in a visionworks image
vx_imagepatch_addressing_t src_addr;
src_addr.dim_x = matSrc.cols;
src_addr.dim_y = matSrc.rows;
src_addr.stride_x = sizeof(vx_uint8);
src_addr.stride_y = matSrc.step;
void* src_ptrs = {matSrc.data};
vx_image vxiSrc = vxCreateImageFromHandle(m_vxCtx, VX_DF_IMAGE_U8, &src_addr, src_ptrs, VX_IMPORT_TYPE_HOST);

// create destination image (same size & type as SOURCE)
cv::Mat matDst;
matDst = cv::Mat(matSrc.rows, matSrc.cols, CV_8UC1);
// wrap OpenCV DEST image in a visionworks image
vx_imagepatch_addressing_t dst_addr;
dst_addr.dim_x = matDst.cols;
dst_addr.dim_y = matDst.rows;
dst_addr.stride_x = sizeof(vx_uint8);
dst_addr.stride_y = matDst.step;
void* dst_ptrs = {matDst.data};
vx_image vxiDst = vxCreateImageFromHandle(m_vxCtx, VX_DF_IMAGE_U8, &dst_addr, dst_ptrs, VX_IMPORT_TYPE_HOST);

// copy ‘src’ => ‘dst’ using GPU
// the function returns 0 (success)
vx_status status = nvxuCopyImage(m_vxCtx, vxiSrc, vxiDst);

// display images
cv::imshow(“dbg1”, m_matSrc); // matSrc is ok
cv::imshow(“dbg2”, matDst); // matDst is completely black
cv::waitKey(0);

// release context
vxReleaseContext(&m_vxCtx);

When your application creates an image from handle, it transfers the management of the memory referenced by the handle to VisionWorks. It means that the memory that was given to vxCreateImageFromHandle shouldn’t be accessed directly anymore by the application until the vx_image object is accessed in map ‘mode’ or is destructed.

If the application accesses the memory referenced by the handle directly, data may not be properly synchronized (what seems to be your case). With images created from handle, accessing them in ‘map’ mode will ensure proper memory synchronization.

For instance, you can try this:

// Access vxiDst in ‘map’ mode: will map it at its original address
void* ptr = 0; // ‘map’ mode
vx_imagepatch_addressing_t addr;
vx_rectangle_t rect = { 0u, 0u, matDst.cols, matDst.rows};
vxAccessImagePatch(vxiDst, &rect, 0, &addr, &ptr, VX_READ_ONLY);
// Here, ptr == matDst.data

// display images
cv::imshow(“dbg1”, matSrc);
cv::imshow(“dbg2”, matDst);
cv::waitKey(0);

// Commit the image for more OpenVX operations
vxCommitImagePatch(vxiDst, &rect, 0, &addr, ptr);

hi tlepley!
Thanks for the informations.
It works.