为了创造一个相对干净的环境,我是在conda虚拟环境中安装opencv的
1、创建conda虚拟环境并进入虚拟环境
conda create -n r21d python=2.7
source activate r21d
2、安装opencv所需的依赖
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake pkg-config libatlas-base-dev gfortran unzip
sudo apt-get install python2.7-dev
sudo apt install python-pip
pip install numpy
3、编译安装opencv
wget https://github.com/opencv/opencv/archive/3.4.0.zip -O opencv-3.4.0.zip
unzip opencv-3.4.0.zip
cd opencv-3.4.0
mkdir build && cd build
#这步中的CMAKE_INSTALL_PREFIX是安装路径,默认的可以是/usr/local,也可以像我一样装在个人目录下,PYTHON_EXECUTABLE是用到的python,可以在命令行中输入which python查看用到的是哪个python
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/home/xiongcx/anaconda2/envs/r21d -D PYTHON_EXECUTABLE=/home/xiongcx/anaconda2/envs/r21d/bin/python -D BUILD_EXAMPLES=ON -D BUILD_SHARED_LIBS=ON ..
make -j20
#如果前面CMAKE_INSTALL_PREFIX选择的是/usr/local,这里需改为sudo make install
make install
4、验证安装是否成功
python
>>> import cv2
>>> cv2.__version__
'3.4.0'