nathzi1505 / opencv-4.2.0-cuda-10.1-Ubuntu-20.04.md
We will now proceed with the installation (see the Qt flag that is disabled to do not have conflicts with Qt5.0).
$ cd ~ $ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip $ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.2.0.zip $ unzip opencv.zip $ unzip opencv_contrib.zip $ echo "Create a virtual environment for the python binding module" $ sudo pip install virtualenv virtualenvwrapper $ sudo rm -rf ~/.cache/pip $ echo "Edit ~/.bashrc" $ export WORKON_HOME=$HOME/.virtualenvs $ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 $ source /usr/local/bin/virtualenvwrapper.sh $ mkvirtualenv cv -p python3 $ pip install numpy $ echo "Procced with the installation" $ cd opencv-4.2.0 $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_C_COMPILER=/usr/bin/gcc-8 \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D INSTALL_C_EXAMPLES=OFF \ -D WITH_TBB=ON \ -D WITH_CUDA=ON \ -D BUILD_opencv_cudacodec=OFF \ -D ENABLE_FAST_MATH=1 \ -D CUDA_FAST_MATH=1 \ -D WITH_CUBLAS=1 \ -D WITH_V4L=ON \ -D WITH_QT=OFF \ -D WITH_OPENGL=ON \ -D WITH_GSTREAMER=ON \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D OPENCV_PC_FILE_NAME=opencv.pc \ -D OPENCV_ENABLE_NONFREE=ON \ -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.2.0/modules \ -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \ -D BUILD_EXAMPLES=ON \ -D WITH_CUDNN=ON \ -D OPENCV_DNN_CUDA=ON \ -D CUDA_ARCH_BIN=7.5 ..
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_C_COMPILER=/usr/bin/gcc-8 -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D WITH_TBB=ON -D WITH_CUDA=ON -D BUILD_opencv_cudacodec=OFF -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_V4L=ON -D WITH_QT=OFF -D WITH_OPENGL=ON -D WITH_GSTREAMER=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_PC_FILE_NAME=opencv.pc -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages -D OPENCV_EXTRA_MODULES_PATH=~/downloads/opencv/opencv_contrib-4.2.0/modules -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python -D BUILD_EXAMPLES=ON -D BUILD_SHARED_LIBS=OFF ..
In case you do not want to include include CUDA set -D WITH_CUDA=OFF
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_C_COMPILER=/usr/bin/gcc-8 -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D WITH_TBB=ON -D WITH_CUDA=OFF -D BUILD_opencv_cudacodec=OFF -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_V4L=ON -D WITH_QT=OFF -D WITH_OPENGL=ON -D WITH_GSTREAMER=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_PC_FILE_NAME=opencv.pc -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages -D OPENCV_EXTRA_MODULES_PATH=~/downloads/opencv/opencv_contrib-4.2.0/modules -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python -D BUILD_EXAMPLES=ON ..
If you want also to use CUDNN you must include those flags (to set the correct value of CUDA_ARCH_BIN you must visit https://developer.nvidia.com/cuda-gpus and find the Compute Capability CC of your graphic card) and also specify the CUDNN location:
-D WITH_CUDNN=ON \ -D OPENCV_DNN_CUDA=ON \ -D CUDA_ARCH_BIN=7.5 \ -D CUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so.7.6.5 \ -D CUDNN_INCLUDE_DIR=/usr/local/cuda/include \
Before the compilation you must check that CUDA has been enabled in the configuration summary printed on the screen. (If you have problems with the CUDA Architecture go to the end of the document).
-- NVIDIA CUDA: YES (ver 10.1, CUFFT CUBLAS NVCUVID FAST_MATH) -- NVIDIA GPU arch: 30 35 37 50 52 60 61 70 75 -- NVIDIA PTX archs:
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
If you want to have available opencv python bindings in the system environment you should copy the created folder during the installation of OpenCV ( -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages ) into the dist-packages folder of the target python interpreter:
$ sudo cp -r ~/.virtualenvs/cv/lib/python3.8/site-packages/cv2 /usr/local/lib/python3.8/dist-packages $ echo "Modify config-3.8.py to point to the target directory" $ sudo nano /usr/local/lib/python3.8/dist-packages/cv2/config-3.8.py ``` PYTHON_EXTENSIONS_PATHS = [ os.path.join('/usr/local/lib/python3.8/dist-packages/cv2', 'python-3.8') ] + PYTHON_EXTENSIONS_PATHS ```
EXAMPLE TO TEST OPENCV 4.2.0 with GPU in C++
Verify the installation by compiling and executing the following example:
#include #include #include #include "bits/time.h" #include #include #include #include #include #include #include #define TestCUDA true int main() < std::clock_t begin = std::clock(); try < cv::String filename = "image.png"; // Enter your image path cv::Mat srcHost = cv::imread(filename, cv::IMREAD_GRAYSCALE); for(int i=0; ielse < cv::Mat dst; cv::bilateralFilter(srcHost,dst,3,1,1); >> //cv::imshow("Result",resultHost); //cv::waitKey(); > catch(const cv::Exception& ex) < std::cout std::clock_t end = std::clock(); std::cout$ g++ -o test test.cpp `pkg-config opencv --cflags --libs` $ ./test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
sudo apt update |
sudo apt upgrade |
sudo apt install build-essential cmake pkg-config unzip yasm git checkinstall |
sudo apt install libjpeg-dev libpng-dev libtiff-dev |
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libavresample-dev |
sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev |
sudo apt install libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev |
sudo apt install libfaac-dev libmp3lame-dev libvorbis-dev |
sudo apt install libopencore-amrnb-dev libopencore-amrwb-dev |
sudo apt-get install libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils |
cd /usr/include/linux |
sudo ln -s -f ../libv4l1-videodev.h videodev.h |
cd ~ |
sudo apt-get install libgtk-3-dev |
sudo apt-get install python3-dev python3-pip |
sudo -H pip3 install -U pip numpy |
sudo apt install python3-testresources |
sudo apt-get install libtbb-dev |
sudo apt-get install libatlas-base-dev gfortran |
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 |
cd ~ |
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip |
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.2.0.zip |
unzip opencv.zip |
unzip opencv_contrib.zip |
echo » Create a virtual environment for the python binding module « |
sudo pip install virtualenv virtualenvwrapper |
sudo rm -rf ~ /.cache/pip |
echo » Edit ~/.bashrc « |
export WORKON_HOME= $HOME /.virtualenvs |
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 |
source /usr/local/bin/virtualenvwrapper.sh |
mkvirtualenv cv -p python3 |
pip install numpy |
echo » Procced with the installation « |
cd opencv-4.2.0 |
mkdir build |
cd build |
cmake -D CMAKE_BUILD_TYPE=RELEASE \ |
-D CMAKE_C_COMPILER=/usr/bin/gcc-8 \ |
-D CMAKE_INSTALL_PREFIX=/usr/local \ |
-D INSTALL_PYTHON_EXAMPLES=ON \ |
-D INSTALL_C_EXAMPLES=OFF \ |
-D WITH_TBB=ON \ |
-D WITH_CUDA=ON \ |
-D BUILD_opencv_cudacodec=OFF \ |
-D ENABLE_FAST_MATH=1 \ |
-D CUDA_FAST_MATH=1 \ |
-D WITH_CUBLAS=1 \ |
-D WITH_V4L=ON \ |
-D WITH_QT=OFF \ |
-D WITH_OPENGL=ON \ |
-D WITH_GSTREAMER=ON \ |
-D OPENCV_GENERATE_PKGCONFIG=ON \ |
-D OPENCV_PC_FILE_NAME=opencv.pc \ |
-D OPENCV_ENABLE_NONFREE=ON \ |
-D OPENCV_PYTHON3_INSTALL_PATH= ~ /.virtualenvs/cv/lib/python3.8/site-packages \ |
-D OPENCV_EXTRA_MODULES_PATH= ~ /opencv_contrib-4.2.0/modules \ |
-D PYTHON_EXECUTABLE= ~ /.virtualenvs/cv/bin/python \ |
-D BUILD_EXAMPLES=ON \ |
-D WITH_CUDNN=ON \ |
-D OPENCV_DNN_CUDA=ON \ |
-D CUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so.7.6.5 \ |
-D CUDNN_INCLUDE_DIR=/usr/local/cuda/include \ |
-D CUDA_ARCH_BIN=7.5 .. |
nproc |
make -j8 |
sudo make install |
sudo /bin/bash -c ‘ echo «/usr/local/lib» >> /etc/ld.so.conf.d/opencv.conf ‘ |
sudo ldconfig |
# For Each Environment |
sudo cp -r ~ /.virtualenvs/cv/lib/python3.8/site-packages/cv2 /usr/local/lib/python3.8/dist-packages |
echo » Modify config-3.8.py to point to the target directory « |
sudo nano /usr/local/lib/python3.8/dist-packages/cv2/config-3.8.py |
` ` ` |
PYTHON_EXTENSIONS_PATHS = [ |
os.path.join( ‘ /usr/local/lib/python3.8/dist-packages/cv2 ‘ , ‘ python-3.8 ‘ ) |
] + PYTHON_EXTENSIONS_PATHS |
` ` ` |