需要的有两个部分:opencv 和opencv_contrib 这两个部分选择相同的版本,opencv_contrib是opencv的扩充.
在opencv3.4.1
中创建一个build
文件夹,并将opencv_contrib-3.4.1
(扩展库)添加到opencv3.4.1
文件夹.
切换到cmake-gui,点击reload cache,把上面通过命令行配置的信息在cmake-gui中进行更新
在编译之前,需要安装OpenCV的依赖项: 依赖项参考: https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html https://www.linuxidc.com/Linux/2017-02/141157.htm https://www.cnblogs.com/tlnshuju/p/6767337.html
# 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 qt5-default 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
# 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
# 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
配置完成后,点击Generate
,会在build文件夹中生成一系列文件.
接下来使用四个线程进行编译:
make -j4
然后进行安装:
cd opencv3.4.0/build/
sudo make install
在配置中opencv的安装目录可以用默认的也可以自己设定,笔者设定是:
/usr/opencv34
编译完成后,把编译出来的cv2.cpython-36m-x86_64-linux-gnu.so库(opencv-python)拷到anaconda的python库包下
然后可以使用相应的python3 import cv2