方法1推荐
从预编译文件安装
#更新配置
sudo apt update
sudo apt upgrade
lsj@lsj-pc:~$ pip3 install opencv-python
Collecting opencv-python
Downloading opencv_python-4.5.2.52-cp38-cp38-manylinux2014_x86_64.whl (51.0 MB)
|████████████████████████████████| 51.0 MB 33 kB/s
Requirement already satisfied: numpy>=1.17.3 in /usr/lib/python3/dist-packages (from opencv-python) (1.17.4)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.5.2.52
lsj@lsj-pc:~$ python3
Python 3.8.5 (default, Jan 27 2021, 15:41:15)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2 as cv
>>> print(cv.__version__)
4.5.2
方法2
源码方式安装
1.下载opencv
mkdir opencv_build
cd opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib
#如果克隆太慢可以进入链接下载
2.安装OpenCV4.5.2
- 安装依赖
新建窗口
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
sudo apt install python3-pip
sudo apt install cmake
sudo apt install python3-dev python3-numpy
sudo apt install gcc g++
sudo apt install libxslt1-dev libffi-dev
sudo apt install at-spi2-core
sudo apt install build-essential libncursesw5-dev libgdbm-dev libc6-dev
sudo apt install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt install zlib1g-dev libsqlite3-dev tk-dev
sudo apt install libssl-dev openssl libffi-dev
sudo apt install libblas-dev liblapack-dev gfortran
sudo apt install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
sudo apt install nasm libtool
sudo apt install libgtk-3-dev libgtk2.0-dev libavutil-dev libswscale-dev libasound2-dev libspeex-dev
sudo apt install v4l-utils libopenexr-dev
sudo apt install libavcodec-dev libavformat-dev libswscale-dev
sudo apt install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
- 进入opencv文件夹
cd opencv #下载后的路径
mkdir build # cmake都是在build路径正式执行编译,所以需要手动创建
cd build
-
编写cmkae配置命令
cmake一下,注意:如果已经在新的文件夹中编译,但是还会出现之前的报错,把cmakecache.txt删了再编译就可。
cmake -D WITH_CUDA=OFF \
-D WITH_VTK=OFF \
-D WITH_MATLAB=OFF \
-D BUILD_DOCS=ON \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_PYTHON3_VERSION=3.8 \
-D PYTHON3_INCLUDE_PATH=/usr/include/python3.8 \
-D PYTHON3_LIBRARIES=/usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.so \
-D PYTHON3_EXECUTABLE=/usr/bin/python3.8 \
-D OPENCV_EXTRA_MODULES_PATH=/home/lsj/opencv_build/opencv_contrib/modules \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include \
-D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local \
..
成功后出现下面界面
- 执行编译及安装
make -j4
sudo make install
- 配置opencv
sudo gedit /etc/ld.so.conf.d/opencv.conf
打开空白文件之后把代码添加到后面保存退出
/usr/local/lib
再更新bash.bashrc
sudo ldconfig
sudo gedit /etc/bash.bashrc
# 把下面内容填写到最下面
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
- 更新配置
source /etc/bash.bashrc
sudo updatedb
- 测试一下
pkg-config --modversion opencv4
# 输出
4.5.2