This OpenCV project automatically detects if CUDA is installed, then compiles accordingly. Preprocessor directives are used to govern compilation such that the cv::cuda:: namespace is used if possible. The result is a project that compiles, builds, and runs flawlessly with or without an available CUDA GPU.
Such structure provides the benefit of allowing the development of a single code base that can be used for various implementations, each with unique hardware constraints.
You can skip this section if
- You already have the
CUDAlibrary installed andCUDA-enabledOpenCVlibrary installed (the latter needs to be compiled by your machine from source) OR - Your machine doesn't have a
CUDAGPU and will only be running the software on CPU. (this assumes you have ROS installed, providing the needed OpenCV library)
Otherwise, the following guide will install the required libraries.
- Install
CUDA. Download the appropriate.debfile for your hardware and distribution. Follow the installation instructions for your selection. - Add the following to your
.bashrc(and remember to source it afterwards):
# CUDA Toolkit
export PATH="/usr/local/cuda-8.0/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH"- Download the
OpenCVrelease and "contrib" source (below uses 3.2.0). This can be done anywhere; many people use the Downloads folder.
wget -O opencv.zip https://github.com/opencv/opencv/archive/3.2.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.2.0.zip
unzip opencv.zip
unzip opencv_contrib.zip- Configure CMake to enable
CUDA. (adjust the last line to match the location of the previous step):
cd opencv-3.2.0
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local \
-DCMAKE_BUILD_TYPE=RELEASE \
-DWITH_CUDA=ON \
-DENABLE_FAST_MATH=1 \
-DCUDA_FAST_MATH=1 \
-DWITH_CUBLAS=1 \
-DINSTALL_PYTHON_EXAMPLES=ON \
-DWITH_OPENMP=ON \
-DWITH_NVCUVID=ON \
-DOPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv_contrib-3.2.0/modules ..- Build and install. This can take nearly an hour, so enter the 3 the commands together then find something else to do:
make -j8
sudo make install
sudo ldconfig # update shared library cacheIf CUDA is detected, the default OpenCV installation path will be used. There is no need to set an environment variable to specify the path unless you've installed it in a non-default location (see the next section for more details).
# cmake will echo helpful information about which libraries are being used
cd build/
cmake ..
make
./opencv-cudaIf CUDA is detected, it is assumed that OpenCV was compiled from source using the default library installation directory, /usr/local/share/OpenCV/. If your library was installed elsewhere, simply create the following environment variable before running cmake as such:
# specify non-default library installation directory
# (not required for standard installations)
export OpenCV_DIR=/your/custom/path/OpenCV/At some point, it may be beneficial for a CUDA-enabled machine to ignore CUDA in order to compile and run using only the CPU for the sake of benchmarking. Alternatively, one might want to make sure that recent changes don't break the application for the CPU-only case. This is easily done by creating the appropriate environment variable:
# tell cmake to not use CUDA libraries
# (only for testing or benchmarking)
export IGNORE_CUDA=1This project has been confirmed to work with CUDA 8.0 and OpenCV 3.2.0.