1. 安装必要软件
sudo apt update
sudo apt-get install -y build-essential
sudo apt-get install -y cmake-gui git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y python3-dev python3-numpy libtbbmalloc2 libtbb-dev libjpeg-dev libpng-dev
安装cmake和g++
sudo apt install -y cmake g++
安装项目构建工具,有两个选择,make或ninja, ninja自动支持多线程,make得自己加-j选项,这里先全安装上
sudo apt install -y make ninja-build
2. 配置网络代理
编辑/etc/environment,在末尾添加一行
sudo vi /etc/environment
all_proxy="http://192.168.2.150:10792"
3. 下载OpenCV和OpenCV Contrib(需通过代理访问github)
https://codeload.github.com/opencv/opencv/zip/refs/tags/4.10.0
https://codeload.github.com/opencv/opencv_contrib/zip/refs/tags/4.10.0
4. 将下载后的zip包存储至 /opt/opencv/
sudo mkdir /opt/opencv
sudo mv opencv-4.10.0.zip /opt/opencv/
sudo opencv_contrib-4.10.0.zip /opt/opencv/
cd /opt/opencv
sudo unzip opencv-4.10.0.zip
sudo unzip opencv_contrib-4.10.0.zip
5. 编译opencv
sudo cmake -B build -GNinja -DCMAKE_INSTALL_PREFIX=/opt/opencv4.10.0 -DOPENCV_GENERATE_PKGCONFIG=ON -DOPENCV_EXTRA_MODULES_PATH=/opt/opencv/opencv_contrib-4.10.0/modules
上面cmake命令可加上CMAKE_BUILD_TYPE参数指定编译类别
比如 CMAKE_BUILD_TYPE=RELEASE 或 CMAKE_BUILD_TYPE=DEBUG
sudo cmake --build build/
开始等待漫长的编译
6. 安装opencv
sudo cmake --install build/