摘自 https://github.com/opencv/opencv/issues/22132
好东西,收一下。
bashinstallOCV.sh #! /bin/bash VER=4.6.0 PYTHON_VERSION=3.8 CORES=2 echo "Script for installing the OpenCV $VER on Ubuntu 18.04 LTS" echo "Updating the OS..." sudo apt update sudo apt upgrade -y echo "Installing dependencies..." sudo apt install build-essential cmake unzip pkg-config -y sudo apt install libjpeg-dev libpng-dev libtiff-dev -y sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y sudo apt install libxvidcore-dev libx264-dev -y sudo apt install libgtk-3-dev -y sudo apt install libatlas-base-dev gfortran -y sudo apt install python3-dev libtbb-dev -y sudo apt install libavcodec-dev libavformat-dev libswscale-dev libavresample-dev -y sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev -y sudo apt install libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev -y sudo apt install libfaac-dev libmp3lame-dev libvorbis-dev -y sudo apt install libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils -y sudo apt install libprotobuf-dev protobuf-compiler -y sudo apt install libgoogle-glog-dev libgflags-dev -y sudo apt install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen -y sudo apt install python-is-python3 echo "Fetching and unpacking OpenCV $VER..." mkdir -p $HOME/repositories cd $HOME/repositories wget -O opencv.zip https://github.com/opencv/opencv/archive/${VER}.zip wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/${VER}.zip #wget https://bootstrap.pypa.io/get-pip.py #sudo python3 get-pip.py sudo -H pip3 install numpy matplotlib imutils -U unzip opencv.zip unzip opencv_contrib.zip rm opencv.zip rm opencv_contrib.zip cd opencv-${VER} mkdir -p build cd build echo "Compiling OpenCV $VER... this will take several minutes..." rm CMakeCache.txt hostname=$(sudo cat /etc/hostname) cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D INSTALL_C_EXAMPLES=OFF \ -D OPENCV_ENABLE_NONFREE=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/repositories/opencv_contrib-${VER}/modules \ -D BUILD_NEW_PYTHON_SUPPORT=ON \ -D PYTHON3_PACKAGES_PATH=/usr/local/lib/python${PYTHON_VERSION}/dist-packages \ -D BUILD_opencv_python3=ON \ -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D OPENCV_PC_FILE_NAME=opencv4.pc \ -D WITH_TBB=ON \ -D ENABLE_FAST_MATH=1 \ -D CUDA_FAST_MATH=1 \ -D WITH_CUBLAS=1 \ -D WITH_CUDA=ON \ -D BUILD_opencv_cudacodec=OFF \ -D WITH_CUDNN=ON \ -D OPENCV_DNN_CUDA=ON \ -D CUDA_ARCH_BIN=7.5 \ -D WITH_V4L=ON \ -D WITH_QT=OFF \ -D WITH_OPENGL=ON \ -D WITH_QT=OFF \ -D WITH_GSTREAMER=ON \ -D WITH_FFMPEG=ON \ -D WITH_OPENCL=ON \ -D OPENCV_ENABLE_NONFREE=ON \ -D ENABLE_PRECOMPILED_HEADERS=YES \ -D EIGEN_INCLUDE_PATH=/usr/include/eigen3 \ -D BUILD_EXAMPLES=ON .. echo "Compilation has started ..." make -j$CORES echo "Installing OpenCV $VER ..." sudo make -j$CORES install sudo cp unix-install/opencv4.pc /usr/lib/x86_64-linux-gnu/pkgconfig/ sudo cp unix-install/opencv4.pc /usr/local/lib/pkgconfig sudo ldconfig source $HOME/.bashrc echo "Installation complete"