转载https://blog.csdn.net/qq_37541097/article/details/79045595
Ubuntu16.04 自带python2.7和python3.5两个版本,默认为python2.7,我使用的是3.5,所以首先将默认的python版本改为3.5.
在终端输入下列指令:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
接下来安装一系列opencv依赖的文件:
sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3.5-dev
sudo apt-get install python3-numpy
接着在opencv的官网下载opencv文件,下载完成后解压通过终端进入解压的文件夹,新建build文件夹,并进入:
mkdir build
cd ./biuld
接着执行:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D PYTHON3_EXECUTABLE=/usr/bin/python3 \
-D PYTHON_INCLUDE_DIR=/usr/include/python3.5 \
-D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/dist-packages/numpy/core/include/ .
开始make:
make -j4
make完成后开始安装:
sudo make install
最后进入python3.5环境import cv2没问题说明安装完成。
接下来讲下卸载的流程:
首先通过终端进入之前建立的build文件夹,接着开始卸载:
sudo make uninstall
接着删除一些文件:
cd ../
sudo rm -r build
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
有些文件可能提示没有,可以忽略。
最后删除一些依赖:
sudo apt-get remove opencv-doc opencv-data python-opencv