Convert vx_image (or cv::cuda::GpuMat) to nvxcu_image_t (object_tracker_nvxcu example)

Hi,

I want to adapt the source code for the object tracker example in the VisionWorks library (VisionWorks-1.6-Samples/samples/object_tracker_nvxcu) to use it in my own code.

Therefore, i am trying to call the tracker using my own images in cv::cuda::GpuMat, i can convert them to vx_image but i do not know how to convert it to nvxcu_image_t in order to follow the same structure as the one defined in main_object_tracker_nvxcu.cpp.

i found a similar post Need help to convert nvxcu_pitch_linear_image_t image to vx_image, but it is not exactlly the same and i could not find a solution.

Many thanks,

After some research i solve the problem…i posted here the solution in case is useful for someone else.

In order to go from cv::mat or cv:cuda:GpuMat to nvxcu_pitch_linear_image_t i have modified the function createImage that is included in the example.

Here the code for the modified functions:


size_t getPixelBytes(int type)
{
    switch(type)
    {
        case CV_8UC1:
        case CV_8UC3:
            return sizeof(uint8_t);
            break;
        case CV_16UC1:
        case CV_16UC3:
            return sizeof(uint16_t);
            break;
        case CV_32FC1:
        case CV_32FC3:
            return sizeof(float);
            break;
        case CV_64FC1:
        case CV_64FC3:
            return sizeof(double);
            break;
        default:
            return 0;
    }
}


nvxcu_pitch_linear_image_t createImageFromGpuMat(uint32_t width, uint32_t height, nvxcu_df_image_e format, cv::cuda::GpuMat frame)
{
    NVXIO_ASSERT(format == NVXCU_DF_IMAGE_U8 ||
                 format == NVXCU_DF_IMAGE_RGB ||
                 format == NVXCU_DF_IMAGE_RGBX);

    nvxcu_pitch_linear_image_t image = { };

    image.base.format = format;
    image.base.image_type = NVXCU_PITCH_LINEAR_IMAGE;
    image.base.width = width;
    image.base.height = height;

    size_t pitch = 0ul;
    uint32_t cn = format == NVXCU_DF_IMAGE_U8 ? 1u :
                  format == NVXCU_DF_IMAGE_RGB ? 3u : 4u;

    NVXIO_CUDA_SAFE_CALL( cudaMallocPitch(&image.planes[0].dev_ptr, &pitch,image.base.width * cn, image.base.height) );
    image.planes[0].pitch_in_bytes = static_cast<uint32_t>(pitch);

    
    //Get number of bytes occupied by a single pixel. Although VideoCapture mostly returns CV_8UC3 type frame thus pixelBytes is 1 , but just in case.
    size_t pixelBytes = getPixelBytes(frame.type());
    //Number of actual data bytes occupied by a row.
    size_t frameRowBytes = frame.cols * frame.channels() * pixelBytes;
    cudaMemcpy2D(image.planes[0].dev_ptr, pitch, frame.ptr(), frame.step, frameRowBytes, frame.rows, cudaMemcpyHostToDevice);
    

    return image;
}



nvxcu_pitch_linear_image_t createImageFromCvMat(uint32_t width, uint32_t height, nvxcu_df_image_e format, cv::Mat frame)
{
    NVXIO_ASSERT(format == NVXCU_DF_IMAGE_U8 ||
                 format == NVXCU_DF_IMAGE_RGB ||
                 format == NVXCU_DF_IMAGE_RGBX);

    nvxcu_pitch_linear_image_t image = { };

    image.base.format = format;
    image.base.image_type = NVXCU_PITCH_LINEAR_IMAGE;
    image.base.width = width;
    image.base.height = height;

    size_t pitch = 0ul;
    uint32_t cn = format == NVXCU_DF_IMAGE_U8 ? 1u :
                  format == NVXCU_DF_IMAGE_RGB ? 3u : 4u;

    NVXIO_CUDA_SAFE_CALL( cudaMallocPitch(&image.planes[0].dev_ptr, &pitch,image.base.width * cn, image.base.height) );
    image.planes[0].pitch_in_bytes = static_cast<uint32_t>(pitch);

    
    //Get number of bytes occupied by a single pixel. Although VideoCapture mostly returns CV_8UC3 type frame thus pixelBytes is 1 , but just in case.
    size_t pixelBytes = getPixelBytes(frame.type());
    //Number of actual data bytes occupied by a row.
    size_t frameRowBytes = frame.cols * frame.channels() * pixelBytes;
    cudaMemcpy2D(image.planes[0].dev_ptr, pitch, frame.ptr(), frame.step, frameRowBytes, frame.rows, cudaMemcpyHostToDevice);
    

    return image;
}


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.