Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install OpenCV 3.4.1 with CUDA 9.0 support for an Ubuntu 18.04 distro.

How to install OpenCV 3.4.1 with CUDA on Ubuntu distro

First of all install update and upgrade your system:

    $ sudo apt-get update
    $ sudo apt-get upgrade

Then, install required libraries:

  • Developers tools:

      $ sudo apt-get install build-essential cmake pkg-config unzip yasm git gfortran
    
  • Image I/O libs

    $ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
    
  • Video Libs - FFMPEG, GSTREAMER, x264 and so on.

    $ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
    $ sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
    $ sudo apt-get install libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev
    
  • Cameras programming interface libs

    $ sudo apt-get install libdc1394-22 libdc1394-22-dev libxine-dev libv4l-dev v4l-utils
    
  • GTK lib for the graphical user functionalites coming from OpenCV highghui module

    $ sudo apt-get install libgtk-3-dev
    
  • Python libraries for python2 and python3:

    $ sudo apt-get install python-dev python-pip python3-dev python3-pip
    $ sudo -H pip2 install -U pip numpy
    $ sudo -H pip3 install -U pip numpy
    
  • Parallelism library C++ for CPU

    $ sudo apt-get install libtbb-dev
    
  • Optimization libraries for OpenCV

    $ sudo apt-get install libatlas-base-dev gfortran
    
  • Optional libraries:

    $ sudo apt-get install libprotobuf-dev protobuf-compiler
    $ sudo apt-get install libgoogle-glog-dev libgflags-dev
    $ sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
    

We will now proceed with the installation (see the Qt flag that is disabled to do not have conflicts with Qt5.0).

$ git clone https://github.com/opencv/opencv.git
$ cd opencv 
$ git checkout 3.4.1 
$ cd ..
$ git clone https://github.com/opencv/opencv_contrib.git
$ cd opencv_contrib
$ git checkout 3.4.1
$ cd ..
$ cd opencv
$ mkdir build
$ cd build

$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -D WITH_CUDA=ON -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_QT=OFF ..

If you want to build the libraries statically you only have to include the -D BUILD_SHARED_LIBS=OFF

$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_QT=OFF -D BUILD_SHARED_LIBS=OFF ..

(In case you do not want to include include CUDA:)

$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=OFF -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D WITH_QT=OFF -D BUILD_SHARED_LIBS=OFF ..

Before the compilation you must check that CUDA has been enabled in the configuration summary printed on the screen.

--   NVIDIA CUDA
--     Use CUFFT:                   YES
--     Use CUBLAS:                  YES
--     USE NVCUVID:                 NO
--     NVIDIA GPU arch:             20 30 35 37 50 52 60 61
--     NVIDIA PTX archs:
--     Use fast math:               YES

If it is fine proceed with the compilation (Use nproc to know the number of cpu cores):

$ nproc
$ make -j8
$ sudo make install

Include the libs in your environment

$ sudo /bin/bash -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
$ sudo ldconfig

Verify the installation by compiling and executing the following example:

#include <iostream>
#include <ctime>
#include <cmath>
#include "bits/time.h"

//#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>

#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaarithm.hpp>
#include <opencv2/cudaimgproc.hpp>

#define TestCUDA true

int main()
{
    std::clock_t begin = std::clock();

    try {
        cv::Mat srcHost = cv::imread("image.png",CV_LOAD_IMAGE_GRAYSCALE);

        for(int i=0; i<1000; i++) {
            if(TestCUDA) {
                cv::cuda::GpuMat dst, src;
                src.upload(srcHost);

                //cv::cuda::threshold(src,dst,128.0,255.0, CV_THRESH_BINARY);
                cv::cuda::bilateralFilter(src,dst,3,1,1);

                cv::Mat resultHost;
                dst.download(resultHost);
            } else {
                cv::Mat dst;
                cv::bilateralFilter(srcHost,dst,3,1,1);
            }
        }

        //cv::imshow("Result",resultHost);
        //cv::waitKey();

    } catch(const cv::Exception& ex) {
        std::cout << "Error: " << ex.what() << std::endl;
    }

    std::clock_t end = std::clock();
    std::cout << double(end-begin) / CLOCKS_PER_SEC  << std::endl;
}

Compile and execute:

$ g++ `pkg-config opencv --cflags --libs` -o test test.cpp
$ ./test

If you have any problem try updating the nvidia drivers.

Addendum to compile 3.4.3 with CUDA 9.0 in Ubuntu 18.04

Cuda 9.0 only works with gcc & g++ vesion 6.0 and you likley have 7.0. So, you must install them and create the respective symlinks:

# CUDA 9 requires gcc 6
$ sudo apt install gcc-6
$ sudo apt install g++-6

# set up symlinks for gcc/g++
sudo ln -s /usr/bin/gcc-6 /usr/local/cuda/bin/gcc
sudo ln -s /usr/bin/g++-6 /usr/local/cuda/bin/g++

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.3/modules -D WITH_CUDA=ON -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python -D WITH_QT=OFF -D BUILD_EXAMPLES=OFF -D INSTALL_C_EXAMPLES=OFF -D INSTALL_PYTHON_EXAMPLES=OFF -D CUDA_NVCC_FLAGS=--expt-relaxed-constexpr -D CUDA_GENERATION=Maxwell ..

Source

@waylonflinn

This comment has been minimized.

Copy link

@waylonflinn waylonflinn commented Jul 9, 2019

Fixes

When compiling the test program, library includes must follow object that requires them:

g++ -o test test.cpp `pkg-config opencv --cflags --libs` 

OpenCV version 4.1.0

For OpenCV version 4.1.0, add the following to the cmake command line:

-DBUILD_opencv_cudacodec=OFF -DOPENCV_GENERATE_PKGCONFIG=ON -DOPENCV_PC_FILE_NAME=opencv.pc

this will

  • disable the deprecated cuda video codecs
  • generate a pkgconfig file

In the test program:

change CV_LOAD_IMAGE_GRAYSCALE to cv::IMREAD_GRAYSCALE

References

opencv/opencv_contrib#1946
opencv/opencv#13154

@raulqf

This comment has been minimized.

Copy link
Owner Author

@raulqf raulqf commented Jul 10, 2019

Thanks,

I've already updated your fixes but in the gist dealing with Opencv 4.1.

https://gist.github.com/raulqf/f42c718a658cddc16f9df07ecc627be7

@waylonflinn

This comment has been minimized.

Copy link

@waylonflinn waylonflinn commented Jul 10, 2019

Awesome. Thank you. 😄

@sreevenkitesh

This comment has been minimized.

Copy link

@sreevenkitesh sreevenkitesh commented Feb 4, 2020

CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: In function destroyContexts()': driver_api_multi.cpp:(.text._Z15destroyContextsv+0xc): undefined reference to cuCtxDestroy_v2'
driver_api_multi.cpp:(.text._Z15destroyContextsv+0x1c): undefined reference to cuCtxDestroy_v2' CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: In function Worker::operator()(int) const':
driver_api_multi.cpp:(.text._ZNK6WorkerclEi+0x37): undefined reference to cuCtxPushCurrent_v2' driver_api_multi.cpp:(.text._ZNK6WorkerclEi+0x8a3): undefined reference to cuCtxPopCurrent_v2'
CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: In function main': driver_api_multi.cpp:(.text.startup.main+0x11c): undefined reference to cuInit'
driver_api_multi.cpp:(.text.startup.main+0x133): undefined reference to cuDeviceGet' driver_api_multi.cpp:(.text.startup.main+0x14d): undefined reference to cuCtxCreate_v2'
driver_api_multi.cpp:(.text.startup.main+0x15d): undefined reference to cuCtxPopCurrent_v2' driver_api_multi.cpp:(.text.startup.main+0x172): undefined reference to cuDeviceGet'
driver_api_multi.cpp:(.text.startup.main+0x18c): undefined reference to cuCtxCreate_v2' driver_api_multi.cpp:(.text.startup.main+0x19c): undefined reference to cuCtxPopCurrent_v2'
collect2: error: ld returned 1 exit status
samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/build.make:123: recipe for target 'bin/example_gpu_driver_api_multi' failed
make[2]: *** [bin/example_gpu_driver_api_multi] Error 1
CMakeFiles/Makefile2:37907: recipe for target 'samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/all' failed
make[1]: *** [samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

cmake command :
cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=/home/sree/Downloads/opencv_contrib-3.4.1/modules -DWITH_CUDA=ON -DWITH_TBB=ON -DENABLE_FAST_MATH=1 -DCUDA_FAST_MATH=1 -DWITH_CUBLAS=1 -DWITH_QT=OFF -DBUILD_EXAMPLES=ON -DBUILD_opencv_python3=ON -DUSE_CUDNN=ON -DWITH_CUBLAS=ON -DWITH_CUDNN=ON -DWITH_NVCUVID=ON -DBUILD_opencv_cudacodec=ON -DENABLE_CXX11=ON ..

please let me know how to solve this

@raulqf

This comment has been minimized.

Copy link
Owner Author

@raulqf raulqf commented Feb 4, 2020

CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: In function destroyContexts()': driver_api_multi.cpp:(.text._Z15destroyContextsv+0xc): undefined reference to cuCtxDestroy_v2'
driver_api_multi.cpp:(.text._Z15destroyContextsv+0x1c): undefined reference to cuCtxDestroy_v2' CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: In function Worker::operator()(int) const':
driver_api_multi.cpp:(.text._ZNK6WorkerclEi+0x37): undefined reference to cuCtxPushCurrent_v2' driver_api_multi.cpp:(.text._ZNK6WorkerclEi+0x8a3): undefined reference to cuCtxPopCurrent_v2'
CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: In function main': driver_api_multi.cpp:(.text.startup.main+0x11c): undefined reference to cuInit'
driver_api_multi.cpp:(.text.startup.main+0x133): undefined reference to cuDeviceGet' driver_api_multi.cpp:(.text.startup.main+0x14d): undefined reference to cuCtxCreate_v2'
driver_api_multi.cpp:(.text.startup.main+0x15d): undefined reference to cuCtxPopCurrent_v2' driver_api_multi.cpp:(.text.startup.main+0x172): undefined reference to cuDeviceGet'
driver_api_multi.cpp:(.text.startup.main+0x18c): undefined reference to cuCtxCreate_v2' driver_api_multi.cpp:(.text.startup.main+0x19c): undefined reference to cuCtxPopCurrent_v2'
collect2: error: ld returned 1 exit status
samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/build.make:123: recipe for target 'bin/example_gpu_driver_api_multi' failed
make[2]: *** [bin/example_gpu_driver_api_multi] Error 1
CMakeFiles/Makefile2:37907: recipe for target 'samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/all' failed
make[1]: *** [samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

cmake command :
cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=/home/sree/Downloads/opencv_contrib-3.4.1/modules -DWITH_CUDA=ON -DWITH_TBB=ON -DENABLE_FAST_MATH=1 -DCUDA_FAST_MATH=1 -DWITH_CUBLAS=1 -DWITH_QT=OFF -DBUILD_EXAMPLES=ON -DBUILD_opencv_python3=ON -DUSE_CUDNN=ON -DWITH_CUBLAS=ON -DWITH_CUDNN=ON -DWITH_NVCUVID=ON -DBUILD_opencv_cudacodec=ON -DENABLE_CXX11=ON ..

please let me know how to solve this

You have problems loading CUDA libraries when compiling the examples. Make sure you correctly installed CUDA or the linking process is looking at the correct place to find the libraries.

@sreevenkitesh

This comment has been minimized.

Copy link

@sreevenkitesh sreevenkitesh commented Feb 4, 2020

CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: In function destroyContexts()': driver_api_multi.cpp:(.text._Z15destroyContextsv+0xc): undefined reference to cuCtxDestroy_v2'
driver_api_multi.cpp:(.text._Z15destroyContextsv+0x1c): undefined reference to cuCtxDestroy_v2' CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: In function Worker::operator()(int) const':
driver_api_multi.cpp:(.text._ZNK6WorkerclEi+0x37): undefined reference to cuCtxPushCurrent_v2' driver_api_multi.cpp:(.text._ZNK6WorkerclEi+0x8a3): undefined reference to cuCtxPopCurrent_v2'
CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: In function main': driver_api_multi.cpp:(.text.startup.main+0x11c): undefined reference to cuInit'
driver_api_multi.cpp:(.text.startup.main+0x133): undefined reference to cuDeviceGet' driver_api_multi.cpp:(.text.startup.main+0x14d): undefined reference to cuCtxCreate_v2'
driver_api_multi.cpp:(.text.startup.main+0x15d): undefined reference to cuCtxPopCurrent_v2' driver_api_multi.cpp:(.text.startup.main+0x172): undefined reference to cuDeviceGet'
driver_api_multi.cpp:(.text.startup.main+0x18c): undefined reference to cuCtxCreate_v2' driver_api_multi.cpp:(.text.startup.main+0x19c): undefined reference to cuCtxPopCurrent_v2'
collect2: error: ld returned 1 exit status
samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/build.make:123: recipe for target 'bin/example_gpu_driver_api_multi' failed
make[2]: *** [bin/example_gpu_driver_api_multi] Error 1
CMakeFiles/Makefile2:37907: recipe for target 'samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/all' failed
make[1]: *** [samples/gpu/CMakeFiles/example_gpu_driver_api_multi.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2
cmake command :
cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=/home/sree/Downloads/opencv_contrib-3.4.1/modules -DWITH_CUDA=ON -DWITH_TBB=ON -DENABLE_FAST_MATH=1 -DCUDA_FAST_MATH=1 -DWITH_CUBLAS=1 -DWITH_QT=OFF -DBUILD_EXAMPLES=ON -DBUILD_opencv_python3=ON -DUSE_CUDNN=ON -DWITH_CUBLAS=ON -DWITH_CUDNN=ON -DWITH_NVCUVID=ON -DBUILD_opencv_cudacodec=ON -DENABLE XX11=ON ..
please let me know how to solve this

You have problems loading CUDA libraries when compiling the examples. Make sure you correctly installed CUDA or the linking process is looking at the correct place to find the libraries.

@raulqf solved this by adding cuda in target link libraries in opencv-3.4.1./samples/gpu/CMakeLists.txt

@sreevenkitesh

This comment has been minimized.

Copy link

@sreevenkitesh sreevenkitesh commented Feb 5, 2020

i am getting following error:

:~/Downloads/opencv-3.4.1/build/bin$ nvcc -std=c++11 -I/usr/include/opencv4/ -I/usr/local/include/ -I/usr/local/cuda-9.0/include/ pkg-config opencv --cflags --libs -L/usr/local/lib/ -L/usr/local/cuda-9.0/lib64/ -g -o binary /home/sree/cpp/sample_cv.cpp -lm -lopencv_core -lopencv_highgui -lopencv_gpu
/tmp/tmpxft_000053f7_00000000-4_sample_cv.o: In function main': /home/sree/cpp/sample_cv.cpp:8: undefined reference to cv::imread(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int)'
/home/sree/cpp/sample_cv.cpp:17: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, cv::_InputArray const&)'
collect2: error: ld returned 1 exit status

@sreevenkitesh

This comment has been minimized.

Copy link

@sreevenkitesh sreevenkitesh commented Feb 5, 2020

this is my code
#include <opencv2/opencv.hpp>
#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaimgproc.hpp>

using namespace cv;

int main() {
Mat src = cv::imread("/home/sree/Downloads/mypic.jpg", 0);
if (!src.data) exit(1);
cuda::GpuMat d_src(src);
cuda::GpuMat d_dst;
cuda::bilateralFilter(d_src, d_dst, -1, 50, 7);
Mat dst;
d_dst.download(dst);
cv::Canny(dst, dst, 35, 200, 3);
resize(dst, dst, Size(dst.cols/3, dst.rows/3));
cv::imshow("test", dst); waitKey(0);
return 0;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment