Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

CUDA::remap shared-memory

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));
    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::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!

CUDA::remap shared-memory

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));
    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::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!

CUDA::remap shared-memorywith 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::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!

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_8UC1, cv::cuda::HostMem::AllocType::SHARED );
    cv::cuda::HostMem cudamem_dst(src_bgr.size(), CV_8UC3, CV_8UC1, 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!

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_8UC1, CV_8UC3, cv::cuda::HostMem::AllocType::SHARED );
    cv::cuda::HostMem cudamem_dst(src_bgr.size(), CV_8UC1, 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!