Convert vx_image to cv::Mat

Hi all,

I am using VisionWorks as a part of Jetpack 3.2. I want to convert a color vx_image into cv::Mat. I have searched on the Internet, but most of the codes are giving either segmentation fault or runtime error.

I am just looking for a C++ function that can convert vx_image to Mat in latest VisionWorks included in Jetpack 3.2. Your help would be highly appreciated.

Thanks in advance!

P.S. I have used the following function, but giving me interlacing error:

Mat VxImage2Mat(vx_image input)
{
    vx_uint32 width = 0, height = 0;
    vxQueryImage(input, VX_IMAGE_WIDTH, &width, sizeof(width));
    vxQueryImage(input, VX_IMAGE_HEIGHT, &height, sizeof(height));
    vx_rectangle_t rect = { 0u, 0u, width, height };
    vx_imagepatch_addressing_t input_addr = {};
    void *input_ptr = nullptr;
    vx_map_id map_id_in;
    vxMapImagePatch(input, &rect, 0, &map_id_in, &input_addr, &input_ptr, VX_READ_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X);
    Mat mat_input(input_addr.dim_y * VX_SCALE_UNITY / input_addr.scale_y,
                      input_addr.dim_x * VX_SCALE_UNITY / input_addr.scale_x,
                      CV_8UC3,
                      input_ptr,
                      input_addr.stride_y);	
    vxUnmapImagePatch(input, map_id_in);
    return mat_input;
}

The result when I imshow() the converted Mat: https://imgur.com/a/twKGO

Best
Debanga

Hi,

It’s recommended to create an OpenCV mat pointer and initial a vx_image from it.
Check this topic for details:
https://devtalk.nvidia.com/default/topic/906396/jetson-tk1/image-copy-using-opencv-and-visionworks-nvxucopyimage-function/post/4773075/#4773075

Thanks.

Thanks @AastaLLL!

For the benefit of all, I am attaching the code that worked for me.
Single channel conversions:

void VX2MatC1(vx_context m_vxCtx, vx_image& vxiSrc, Mat& matDst)
{
	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);

	vx_status status = nvxuCopyImage(m_vxCtx, vxiSrc, vxiDst);

	vxReleaseImage(&vxiDst);
}

void Mat2VXC1 (vx_context m_vxCtx, Mat& matSrc, vx_image& vxiSrc) {
	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};
	vxiSrc = vxCreateImageFromHandle(m_vxCtx, VX_DF_IMAGE_U8, &src_addr, src_ptrs, VX_IMPORT_TYPE_HOST);
}

RGB conversions:

void VX2Mat(vx_context m_vxCtx, vx_image& vxiSrc, Mat& matDst) {
	vx_image vxr = vxCreateImage(m_vxCtx, matDst.cols, matDst.rows, VX_DF_IMAGE_U8);
	vx_image vxg = vxCreateImage(m_vxCtx, matDst.cols, matDst.rows, VX_DF_IMAGE_U8);
	vx_image vxb = vxCreateImage(m_vxCtx, matDst.cols, matDst.rows, VX_DF_IMAGE_U8);
	
	Mat cvr = Mat(matDst.rows, matDst.cols, CV_8UC1);
	Mat cvg = Mat(matDst.rows, matDst.cols, CV_8UC1);;
	Mat cvb = Mat(matDst.rows, matDst.cols, CV_8UC1);;
	
	vx_status status1 = vxuChannelExtract(m_vxCtx, vxiSrc, VX_CHANNEL_R, vxr);
	vx_status status2 = vxuChannelExtract(m_vxCtx, vxiSrc, VX_CHANNEL_G, vxg);
	vx_status status3 = vxuChannelExtract(m_vxCtx, vxiSrc, VX_CHANNEL_B, vxb);
	VX2MatC1(m_vxCtx, vxr, cvr);
	VX2MatC1(m_vxCtx, vxg, cvg);
	VX2MatC1(m_vxCtx, vxb, cvb);

	vector<Mat> channels;
	channels.push_back(cvb);
        channels.push_back(cvg);
        channels.push_back(cvr);
	merge(channels, matDst);
	vxReleaseImage(&vxr);
	vxReleaseImage(&vxg);
	vxReleaseImage(&vxb);
}

void Mat2VX (vx_context m_vxCtx, Mat& matSrc, vx_image& vxiSrc) {
	Mat bgr[3];  
	split(matSrc,bgr);
	vx_image vxr, vxg, vxb;
	Mat2VXC1(m_vxCtx, bgr[0], vxb);
	Mat2VXC1(m_vxCtx, bgr[1], vxg);
	Mat2VXC1(m_vxCtx, bgr[2], vxr);
	vxiSrc = vxCreateImage(m_vxCtx, matSrc.cols, matSrc.rows, VX_DF_IMAGE_RGB);
	vx_status status = vxuChannelCombine(m_vxCtx, vxb, vxg, vxr, NULL, vxiSrc);
	vxReleaseImage(&vxr);
	vxReleaseImage(&vxg);
	vxReleaseImage(&vxb);
}

Best,
Debanga

2 Likes

Thanks for the sharing! : )