Skip to content

Instantly share code, notes, and snippets.

@syneart
Last active January 22, 2025 11:06
Show Gist options
  • Save syneart/3e6bb68de8b6390d2eb18bff67767dcb to your computer and use it in GitHub Desktop.
Save syneart/3e6bb68de8b6390d2eb18bff67767dcb to your computer and use it in GitHub Desktop.
OpenCV 3.2 with Contrib install script [CUDA supported]
######################################
# INSTALL OPENCV ON UBUNTU OR DEBIAN #
######################################
# Use below command to install OpenCV 3.2 with Contrib at Ubuntu or Debian on your own operating system.
# wget -O - https://gist.githubusercontent.com/syneart/3e6bb68de8b6390d2eb18bff67767dcb/raw/OpenCV3.2withContrib.sh | bash
# | THIS SCRIPT IS TESTED CORRECTLY ON |
# |-----------------------------------------------------------|
# | OS | OpenCV | CUDA | Test | Last test |
# |----------------|--------------|------|------|-------------|
# | Ubuntu 22.04.4 | OpenCV 3.2.0 | OK | OK | 02 Mar 2024 |
# | Ubuntu 22.04.1 | OpenCV 3.2.0 | OK | OK | 17 Aug 2022 |
# | Ubuntu 20.04.4 | OpenCV 3.2.0 | ? | OK | 17 Aug 2022 |
# | Ubuntu 18.04.6 | OpenCV 3.2.0 | ? | OK | 17 Aug 2022 |
# | Ubuntu 16.04.2 | OpenCV 3.2.0 | ? | OK | 20 May 2017 |
# | Debian 8.8 | OpenCV 3.2.0 | - | OK | 20 May 2017 |
# | Debian 9.0 | OpenCV 3.2.0 | - | OK | 25 Jun 2017 |
# 1. KEEP UBUNTU OR DEBIAN UP TO DATE
sudo apt-get -y update
#sudo apt-get -y upgrade
sudo apt-get -y autoremove
export DEBIAN_FRONTEND=noninteractive
ln -fs /usr/share/zoneinfo/$(curl http://ip-api.com/line?fields=timezone) /etc/localtime
sudo apt-get install -y tzdata
# 2. INSTALL THE DEPENDENCIES
# Build tools:
sudo apt-get install -y build-essential cmake
# GUI (if you want to use GTK instead of Qt, replace 'qt5-default' with 'libgtkglext1-dev' and remove '-DWITH_QT=ON' option in CMake):
sudo apt-get install -y libgtkglext1-dev libvtk6-dev
# Media I/O:
sudo apt-get install -y zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev libjasper-dev libopenexr-dev libgdal-dev libgphoto2-dev
# Video I/O:
sudo apt-get install -y libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev v4l-utils
# Parallelism and linear algebra libraries:
sudo apt-get install -y libtbb-dev libeigen3-dev
# Python:
sudo apt-get install -y python-dev python-tk python-numpy python3-dev python3-tk python3-numpy
# Java:
# sudo apt-get install -y ant default-jdk
# Documentation:
# sudo apt-get install -y doxygen
# 3. INSTALL THE LIBRARY (YOU CAN CHANGE '3.2.0' FOR THE LAST STABLE VERSION)
sudo apt-get install -y unzip wget
wget https://github.com/opencv/opencv/archive/3.2.0.zip -O opencv320.zip
unzip opencv320.zip
rm opencv320.zip
mv opencv-3.2.0 OpenCV
cd OpenCV
touch OpenCV3.2withContrib
# 4. INSTALL THE OPENCV_CONTRIB LIBRARY (YOU CAN CHANGE '3.2.0' FOR THE LAST STABLE VERSION)
wget https://github.com/opencv/opencv_contrib/archive/3.2.0.zip -O opencv_contrib320.zip
unzip opencv_contrib320.zip
rm opencv_contrib320.zip
mv opencv_contrib-3.2.0 OpenCV_contrib
# 5. Build OpenCV with contrib
## Fix Flow control statements are not properly nested issue
sed -i '21,22 s/^/#/' cmake/OpenCVCompilerOptions.cmake
## Fix ffmpeg version issue
sed -i '1 i #define AV_CODEC_FLAG_GLOBAL_HEADER (1 << 22)\n#define CODEC_FLAG_GLOBAL_HEADER AV_CODEC_FLAG_GLOBAL_HEADER\n#define AVFMT_RAWPICTURE 0x0020' modules/videoio/src/cap_ffmpeg_impl.hpp
## Fix build.make:56 Error
sed -i 's/PyString_AsString(obj);/(char*)PyString_AsString(obj);/' modules/python/src2/cv2.cpp
## Requires version 10 or lower
if [[ `gcc -dumpversion` -ge 11 ]]; then
sudo apt-get install -y gcc-10 g++-10 libtbb2-dev
CC="gcc-10"
CXX="g++-10"
fi
## If the system supports CUDA, and version LESS then 12 (REQUIRE)
(nvcc --version) >/dev/null 2>&1 && {
if [[ $(echo "`nvcc --version | sed -n 's/^.*release \([0-9]\+\.[0-9]\+\).*$/\1/p'` < 12" | bc) -eq 1 ]]; then
CUDA_LIBRARY="-DCUDA_nppicom_LIBRARY=stdc++"
wget -O - https://gist.githubusercontent.com/syneart/3e6bb68de8b6390d2eb18bff67767dcb/raw/WITH_CUDA.patch | patch -p1
else
DO_CUDA_OFF="-DWITH_CUDA=OFF"
echo "[SCRIPT] CUDA OFF # CUDA Version REQUIRE LESS then 12 # OpenCV not Support !!"
fi
} || {
echo "[SCRIPT] CUDA NOT FOUND !!"
}
mkdir build
cd build
CC=${CC} CXX=${CXX} cmake -DOPENCV_EXTRA_MODULES_PATH=../OpenCV_contrib/modules -DWITH_QT=OFF -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DINSTALL_C_EXAMPLES=OFF -DWITH_GDAL=ON -DWITH_XINE=ON -DBUILD_EXAMPLES=OFF -DENABLE_PRECOMPILED_HEADERS=OFF ${CUDA_LIBRARY} ${DO_CUDA_OFF} ..
CC=${CC} CXX=${CXX} make -j`nproc`
sudo make install
sudo ldconfig
diff -bur OpenCV/cmake/FindCUDA.cmake OpenCV/cmake/FindCUDA.cmake
--- OpenCV/cmake/FindCUDA.cmake 2016-12-23 20:54:44.000000000 +0800
+++ OpenCV/cmake/FindCUDA.cmake 2024-03-02 12:50:22.322402528 +0800
@@ -521,7 +521,16 @@
unset(CUDA_cusparse_LIBRARY CACHE)
unset(CUDA_npp_LIBRARY CACHE)
unset(CUDA_nppc_LIBRARY CACHE)
- unset(CUDA_nppi_LIBRARY CACHE)
+ unset(CUDA_nppial_LIBRARY CACHE)
+ unset(CUDA_nppicc_LIBRARY CACHE)
+ unset(CUDA_nppicom_LIBRARY CACHE)
+ unset(CUDA_nppidei_LIBRARY CACHE)
+ unset(CUDA_nppif_LIBRARY CACHE)
+ unset(CUDA_nppig_LIBRARY CACHE)
+ unset(CUDA_nppim_LIBRARY CACHE)
+ unset(CUDA_nppist_LIBRARY CACHE)
+ unset(CUDA_nppisu_LIBRARY CACHE)
+ unset(CUDA_nppitc_LIBRARY CACHE)
unset(CUDA_npps_LIBRARY CACHE)
unset(CUDA_nvcuvenc_LIBRARY CACHE)
unset(CUDA_nvcuvid_LIBRARY CACHE)
@@ -787,7 +796,22 @@
find_cuda_helper_libs(nvcuvid)
endif()
endif()
-if(CUDA_VERSION VERSION_GREATER "5.0")
+if(CUDA_VERSION VERSION_GREATER "9.0")
+ # In CUDA 9.0 NPPI was splitted onto 10 separate libraries.
+ find_cuda_helper_libs(nppc)
+ find_cuda_helper_libs(nppial)
+ find_cuda_helper_libs(nppicc)
+ find_cuda_helper_libs(nppicom)
+ find_cuda_helper_libs(nppidei)
+ find_cuda_helper_libs(nppif)
+ find_cuda_helper_libs(nppig)
+ find_cuda_helper_libs(nppim)
+ find_cuda_helper_libs(nppist)
+ find_cuda_helper_libs(nppisu)
+ find_cuda_helper_libs(nppitc)
+ find_cuda_helper_libs(npps)
+ set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")
+elseif(CUDA_VERSION VERSION_GREATER "5.0")
# In CUDA 5.5 NPP was splitted onto 3 separate libraries.
find_cuda_helper_libs(nppc)
find_cuda_helper_libs(nppi)
diff -bur OpenCV/cmake/OpenCVDetectCUDA.cmake OpenCV/cmake/OpenCVDetectCUDA.cmake
--- OpenCV/cmake/OpenCVDetectCUDA.cmake 2016-12-23 20:54:44.000000000 +0800
+++ OpenCV/cmake/OpenCVDetectCUDA.cmake 2024-03-02 13:39:33.145229097 +0800
@@ -62,9 +62,7 @@
endif()
set(__cuda_arch_ptx "")
- if(CUDA_GENERATION STREQUAL "Fermi")
- set(__cuda_arch_bin "2.0")
- elseif(CUDA_GENERATION STREQUAL "Kepler")
+ if(CUDA_GENERATION STREQUAL "Kepler")
set(__cuda_arch_bin "3.0 3.5 3.7")
elseif(CUDA_GENERATION STREQUAL "Maxwell")
set(__cuda_arch_bin "5.0 5.2")
@@ -93,8 +91,14 @@
else()
if(${CUDA_VERSION} VERSION_LESS "8.0")
set(__cuda_arch_bin "2.0 3.0 3.5 3.7 5.0 5.2")
- else()
+ elseif(${CUDA_VERSION} VERSION_LESS "9.0")
set(__cuda_arch_bin "2.0 3.0 3.5 3.7 5.0 5.2 6.0 6.1")
+ elseif(${CUDA_VERSION} VERSION_LESS "10.0")
+ set(__cuda_arch_bin "3.0 3.5 3.7 5.0 5.2 6.0 6.1")
+ elseif(${CUDA_VERSION} VERSION_LESS "11.0")
+ set(__cuda_arch_bin "3.7 5.0 5.2 6.0 6.1")
+ else()
+ set(__cuda_arch_bin "5.2 5.3 6.0 6.1 6.2")
endif()
set(__cuda_arch_ptx "")
endif()
diff -bur OpenCV/modules/cudev/include/opencv2/cudev/common.hpp OpenCV/modules/cudev/include/opencv2/cudev/common.hpp
--- OpenCV/modules/cudev/include/opencv2/cudev/common.hpp 2016-12-23 20:54:44.000000000 +0800
+++ OpenCV/modules/cudev/include/opencv2/cudev/common.hpp 2024-03-02 12:56:45.444013121 +0800
@@ -49,6 +49,7 @@
#include <cuda_runtime.h>
#include "opencv2/core/cuda.hpp"
#include "opencv2/core/cuda_stream_accessor.hpp"
+#include <cuda_fp16.h>
namespace cv { namespace cudev {
@syneart
Copy link
Author

syneart commented Mar 2, 2024

Update: this was my error Ubuntu 20.04LTS

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_nppi_LIBRARY (ADVANCED)

this is how i solved it: bad cuda libraries Uninstalling them solved the issue

Thank you for your feedback. :)
The script already supports CUDA now.

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