//ptr used to download result image from device
    uint8_t *imgPtr;
    //ptr hold resized image in device memory
    uint8_t *resized_gpu_ptr;
    cv::Mat left, downloadedLeft;
    cv::cuda::GpuMat gpuLeft;
    //resize image to 257x257 using cuda::resize
    const int net_row = 257;
    const int net_col = 257;
    //malloc 257x257x3 in device memory
    cudaMalloc(&resized_gpu_ptr, net_row*net_col*3);

    //construct GpuMat and set net step to 257x3
    cv::cuda::GpuMat gpu_resized(net_row, net_col, CV_8UC3, resized_gpu_ptr, net_col*3);

    //read image and resize
    left = cv::imread("leftview.jpg");
    gpuLeft.upload(left);
    cv::cuda::resize(gpuLeft, gpu_resized, cv::Size(257, 257));

    //do process in tensorrt
    cudaMalloc((void **)&imgPtr, net_row*net_col*3);
    cudaMemcpy(imgPtr, resized_gpu_ptr,
            net_row*net_col*3, cudaMemcpyDeviceToDevice);

// following code is just for testing and visualization...
    //auto my_type_is = left.type();
    //construct resize gpu mat with step 257x3
    cv::cuda::GpuMat gpuImg(net_row, net_col, CV_8UC3, imgPtr, net_col*3);
    //download 257x257x3 image to cpu memory
    gpuImg.download(downloadedLeft);
    imshow ("test", downloadedLeft);
    cv::waitKey(0);

f4ef5287-c612-4c02-9836-95a44c5675cb-image.png

这段代码为什么出来的图片是黑色的?

81d0d28a-14ca-4c6d-84e4-1e7833d71180-image.png

uint8_t *resized_gpu_ptr;
    cv::Mat left, downloadedLeft;
    cv::cuda::GpuMat gpuLeft;
    //resize image to 257x257 using cuda::resize
    const int net_row = 257;
    const int net_col = 257;
    
    float *deviceInp;
    cudaMalloc((float **)&deviceInp, net_row*net_col*3);
    //malloc 257x257x3 in device memory
    cudaMalloc(&resized_gpu_ptr, net_row*net_col*3);

    cv::cuda::GpuMat gpu_resized(net_row, net_col, CV_8UC3, resized_gpu_ptr, net_col*3);

    //read image and resize
    left = cv::imread("../images/lanes/town01191.jpg");
    gpuLeft.upload(left);
    cv::cuda::resize(gpuLeft, gpu_resized, cv::Size(257, 257));
    gpu_resized.convertTo(gpu_resized, CV_32FC3);

    //do process in tensorrt
    cudaMemcpy(deviceInp, resized_gpu_ptr,
            net_row*net_col*3, cudaMemcpyDeviceToDevice);
            
    // cv::cuda::GpuMat gpuImg(net_row, net_col, CV_8UC3, deviceInp, net_col*3);
    cv::cuda::GpuMat gpuImg(net_row, net_col, CV_32FC3, deviceInp, net_col*3);
    gpuImg.convertTo(gpuImg, CV_8UC3);
    //download 257x257x3 image to cpu memory
    gpuImg.download(downloadedLeft);
    imshow ("test", downloadedLeft);
    cv::waitKey(0);