Run Cuda


I am trying to run a sample code of opencv in my Jetson Nano that uses the cudaarithm contribution module. The code is simple, just do a threshold in the gpu:

/* OpenCV: Thresholding using GPU */
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/cuda.hpp"
#include "opencv2/cudaarithm.hpp"

using namespace cv;

int main (int argc, char* argv[])
        cv::Mat src_host = cv::imread("Oak_Tree.png", IMREAD_GRAYSCALE);
        cv::cuda::GpuMat dst, src;

        cv::cuda::threshold(src, dst, 128.0, 255.0, THRESH_BINARY);

        cv::Mat result_host;;
        cv::imshow("Result", result_host);
    catch(const cv::Exception& ex)
        std::cout << "Error: " << ex.what() << std::endl;
    return 0;

I have installed OpenCV and build other project previously, I just can’t find how to link in cmake the cuda:

cmake_minimum_required(VERSION 3.0)
project( opencvgpu )

find_package( OpenCV 4.3 REQUIRED)
find_package(CUDA REQUIRED)

include_directories( ${OpenCV_INCLUDE_DIRS} ${CUDA_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} ${CUDA_LIBRARIES} )

And I always get the same error:

felipevw@felipevw-desktop:~/opencvgpu$ cmake .
-- Found CUDA: /usr/local/cuda (found suitable exact version "10.0") 
-- Found OpenCV: /usr/local (found suitable version "4.3.0", minimum required is "4.3") 
-- Found CUDA: /usr/local/cuda (found version "10.0") 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/felipevw/opencvgpu
felipevw@felipevw-desktop:~/opencvgpu$ make
Scanning dependencies of target DisplayImage
[ 50%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
/home/felipevw/opencvgpu/DisplayImage.cpp:5:10: fatal error: opencv2/cuda.hpp: No such file or directory
 #include "opencv2/cuda.hpp"
compilation terminated.
CMakeFiles/DisplayImage.dir/build.make:62: recipe for target 'CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o' failed
make[2]: *** [CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/DisplayImage.dir/all' failed
make[1]: *** [CMakeFiles/DisplayImage.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

If someone could help me find out what I am missing I would be very grateful. I run in:

  • Ubuntu 18.04
  • Opencv 4.3.0
  • CUDA 10.0

Thank you

CUDA modules have moved to opencv_contrib:

You would set extra_modules_path to modules directory of opencv_contrib.

Have a look to this script.

Also note that when compiling an application for opencv, the include path with opencv4 would be something like:
cuda.hpp is in opencv2/core, so try:

#include <core/cuda.hpp>

but you may not need it explicitly.

Be also aware that pkg-config uses opencv4.pc instead of opencv.pc in opencv3. You may use a symbolic link.

1 Like