CUDA::remap with shared-memory -> black output
I am trying to undistort images by using CUDA following the 'aged' (opencv2) approach from here. I managed to get all 3 approaches (CPU, CUDA w/o memory mapping, CUDA with memory mapping) running - so I assume my code is more or less ok. When I try to change the 3rd approach to color-output, all I get is a black-image.
cv::Mat K = (cv::Mat_<float>(3,3) << 3.11260842e+03, 0.00000000e+00, 2.06650405e+03, 0.00000000e+00, 3.11421108e+03, 1.52851715e+03, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00 );
cv::Mat D = (cv::Mat_<float>(5,1) << 1.07797852e-01, -2.83055545e-01, -3.52214618e-05, 3.60758721e-04, 2.30318706e-01);
cv::Mat src_bgr = cv::imread("left01.jpg");
cv::Mat map1, map2;
cv::Mat Knew = (cv::Mat_<float>(3,3));
Knew = cv::getOptimalNewCameraMatrix(K, D, src_bgr.size() , 1, src_bgr.size());
cv::initUndistortRectifyMap(K, D, cv::Mat::eye(3, 3, CV_32FC1), Knew, src_bgr.size(), CV_32FC1, map1, map2);
cv::cuda::GpuMat gpu_map1, gpu_map2;
gpu_map1.upload(map1);
gpu_map2.upload(map2);
enum AllocType { PAGE_LOCKED = 1, SHARED = 2, WRITE_COMBINED = 4 };
cv::cuda::HostMem cudamem_src(src_bgr.size(), CV_8UC3, cv::cuda::HostMem::AllocType::SHARED );
cv::cuda::HostMem cudamem_dst(src_bgr.size(), CV_8UC3, cv::cuda::HostMem::AllocType::SHARED);
cv::cuda::GpuMat gpu_src = cudamem_src.createGpuMatHeader();
cv::cuda::GpuMat gpu_dst = cudamem_dst.createGpuMatHeader();
cv::Mat src = cudamem_src.createMatHeader();
cv::Mat dst = cudamem_dst.createMatHeader();
src = src_bgr;
// cv::cvtColor(src_bgr, src, cv::COLOR_BGR2GRAY);
cv::cuda::remap(gpu_src, gpu_dst, gpu_map1, gpu_map2, cv::INTER_LINEAR);
cv::imwrite("imgout.jpg", dst);
If I change AllocType::SHARED
to AllocType::PAGE_LOCKED
or WRITE_COMBINED
I get a runtime error.
I completely inexperienced with C++ and using a Jetson Nano with Opencv4.2.0 installed.
Thank you for your kind help!
I am not sure what you are trying to do? If you use
AllocType::PAGE_LOCKED
then the memory is not mapped to the GPU so I would expect the CUDA functions to fail?AllocType::PAGE_LOCKED
is normally for host memory which you use to transfer from the device to, that is page_locked_mat =GPUMat.download()
.Thanks for your reply! I want to use SHARED as it should be the fastest option, but the image I receive is black. So I wonder what I am doing wrong here.
If I set
src
to a grey-scale image by uncommenting// cv::cvtColor(src_bgr, src, cv::COLOR_BGR2GRAY);
, I receive a gray-scale output. But it does not work by settingsrc
to a color-image withsrc = src_bgr;
- then I only get a black-output.I am aware that in 'PAGE_LOCKED' mode I have to download the Gpu-data.