sudo apt install cmake qt5-default gcc g++ libxslt1-dev libffi-dev \
at-spi2-core libavresample-dev libavresample4 python3 \
python3-dev python3-numpy python3-pip -y
sudo ln -s .
# 方法1:不推荐
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev -y
# 方法2:手动编译
git clone https://github.com/jasper-software/jasper.git -b version-3.0.6
cd jasper/build
sudo ln -s /usr/bin/python3 /usr/bin/python
./build --release --install-dir /usr/local
cd ../tmp_cmake/build
# 然后将/usr/local/lib添加到系统环境,例如
sudo vim /etc/ld.so.conf.d/jasper.conf
# 内容如下:
/usr/local/lib
sudo apt install build-essential libncursesw5-dev libgdbm-dev libc6-dev \
zlib1g-dev libsqlite3-dev tk-dev \
libssl-dev openssl libffi-dev \
libblas-dev liblapack-dev gfortran \
build-essential git libgtk2.0-dev pkg-config libavcodec-dev \
libavformat-dev libswscale-dev libtbb2 libtbb-dev libjpeg-dev \
libpng-dev libtiff-dev libdc1394-22-dev -y
sudo apt install nasm libtool libgtk-3-dev libgtk2.0-dev \
libavutil-dev libswscale-dev libasound2-dev libspeex-dev \
v4l-utils libopenexr-dev libavcodec-dev libavformat-dev \
libswscale-dev libgstreamer-plugins-base1.0-dev \
libgstreamer1.0-dev -y
git clone https://github.com/uclouvain/openjpeg
cd openjpeg
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make
sudo make install
下载源码(直接下载git是预览版,去releases下载是正式版)。opencv,opencv_contrib
解压到你想要的任意文件夹,但是建议两个文件夹解压后放同级路径。
- 修改一下opencv下面的CMakeLists.txt
文件,开头加入下面这句,防止可能发生的找不到系统so文件。
shell~~ ~~set(CMAKE_PREFIX_PATH "/usr/lib/x86_64-linux-gnu")~~ ~~
sudo apt install libpython3-dev python3-dev python3 python3-pip python3-numpy -y
- 由于此pip没有添加到系统环境,所以在运行的时候需要用python -m pip的方式才能安装
- 并且我的默认python环境为conda环境,所以这里需要把系统环境的python所有路径都指出来。
- 正式使用时,还是请直接安装到系统的python3环境就行了,这里只是演示。
/usr/bin/python3 -c "import numpy as np;import os; print(os.path.dirname(np.__file__)+'/core/include')"
# 结果如下:
/usr/lib/python3/dist-packages/numpy/core/include
cd xxx
mkdir build # cmake都是在build路径正式执行编译,所以需要手动创建
cd build
cmake -D CMAKE_BUILD_TYPE=Release \
-D OPENCV_EXTRA_MODULES_PATH=/home/tlntin/Documents/opencv/opencv_contrib-4.6.0/modules \
-D WITH_QT=ON \
-D PYTHON3_EXECUTABLE=/usr/bin/python3 \
-D PYTHON3_INCLUDE_DIR=$(/usr/bin/python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON3_PACKAGES_PATH=$(/usr/bin/python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
-D PYTHON3_NUMPY_INCLUDE_DIRS=$(/usr/bin/python3 -c "import numpy as np;import os; print(os.path.dirname(np.__file__)+'/core/include')") \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 \
..
# 注意:1.第二行opencv_contrib改成你的路径。2.最后一行的..不可省略,因为需要编译的其实是上一个路径。
cmake -D CMAKE_BUILD_TYPE=Release \
-D OPENCV_EXTRA_MODULES_PATH=/home/tlntin/Documents/opencv/opencv_contrib-4.6.0/modules \
-D OPENCV_ENABLE_NONFREE=ON \
-D WITH_OPENMP=ON \
-D WITH_TBB=ON \
-D WITH_QT=ON \
-D CUDA_NVCC_FLAGS="--Wno-deprecated-gpu-targets" \
-D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUBLAS=ON \
-D PYTHON3_EXECUTABLE=/usr/bin/python3 \
-D PYTHON3_INCLUDE_DIR=$(/usr/bin/python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON3_PACKAGES_PATH=$(/usr/bin/python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
-D PYTHON3_NUMPY_INCLUDE_DIRS=$(/usr/bin/python3 -c "import numpy as np;import os; print(os.path.dirname(np.__file__)+'/core/include')") \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 \
..
make -j8
sudo make install
sudo vim /etc/ld.so.conf.d/opencv.conf
# 填入下面的信息
/usr/local/opencv4/lib
# 保存配置文件
sudo ldconfig
sudo vim /etc/bash.bashrc # 如果是zsh终端,建议在/etc/profile与/etc/.zshrc也写一下
# 写入下面的信息
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv4/lib/pkgconfig
export PATH=$PATH:/usr/local/opencv4/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv4/lib
source /etc/bash.bashrc
pkg-config --modversion opencv4
# 输出
4.6.0
/bin/python3 -c "import cv2; print(cv2.__version__)"
# 输出
'4.6.0'
conda create -n opencv python==3.9
conda activate opencv
conda install numpy
cd xxx
cd build
rm install_manifest.txt # 删除配置路径,待会重写生成配置文件
make clean # 清空编译缓存
cd ..
rm -r build # 清空编译文件夹
mkdir build # 重新创建编译文件夹
cd build # 再次进入编译文件夹
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D OPENCV_EXTRA_MODULES_PATH=/home/tlntin/Downloads/opencv_contrib/modules \
-D OPENCV_ENABLE_NONFREE=ON \
-D WITH_OPENMP=ON \
-D WITH_TBB=ON \
-D WITH_QT=ON \
-D CUDA_NVCC_FLAGS=--Wno-deprecated-gpu-targets \
-D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D CMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") \
-D PYTHON3_EXECUTABLE=$(which python) \
-D PYTHON3_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON3_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
..
# 同样,记得修改一下第二行的路径就行了,其它由于已经激活了anaconda环境,所有都会自动配置
# 最后..不要漏掉。
# 这里参考了知乎大佬的,加了闭源驱动和cuda gpu支持,不过编译时间会变长一些。
# 知乎原贴:https://zhuanlan.zhihu.com/p/147595002
make install
python
Python 3.9.0 | packaged by conda-forge | (default, Nov 3 2022, 07:57:39)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.6.0'
xargs rm < install_manifest.txt