参考:
https://github.com/spmallick/learnopencv
在这篇文章中,我们将提供在Ubuntu上安装OpenCV 3(C ++和Python)的分步说明。
注意:如果网络环境不好,即使完全按照该教程执行,也可能安装失败的,建议在一个较好的网络环境下安装(因为需要在线下载)
sudo apt-get update
sudo apt-get upgrade
## 注: 如果提示 sudo apt-get update or --fix-missing
# 则需执行以下2行命令
sudo apt-get update
sudo apt-get upgrade --fix-missing
sudo apt-get remove x264 libx264-dev
# 可以将这些命令写入shell脚本中执行
sudo apt-get install build-essential checkinstall cmake pkg-config yasm gfortran git
sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev
# If you are using Ubuntu 14.04
sudo apt-get install libtiff4-dev
# If you are using Ubuntu 16.04
sudo apt-get install libtiff5-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get install libxine2-dev libv4l-dev
sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get install libqt4-dev libgtk2.0-dev libtbb-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libvorbis-dev libxvidcore-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install x264 v4l-utils
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
sudo apt-get install python-dev python-pip python3-dev python3-pip
sudo -H pip2 install -U pip numpy
sudo -H pip3 install -U pip numpy
我们将使用虚拟环境来安装Python库。 为了将您的项目环境和全球环境分开,通常是一个很好的做法。
# 不想使用虚拟环境 可以跳过该步骤
# Install virtual environment
sudo pip2 install virtualenv virtualenvwrapper
sudo pip3 install virtualenv virtualenvwrapper
echo "# Virtual Environment Wrapper" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc
############ For Python 2 ############
# create virtual environment
mkvirtualenv facecourse-py2 -p python2
workon facecourse-py2
# now install python libraries within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn ipython
# quit virtual environment
deactivate
######################################
############ For Python 3 ############
# create virtual environment
mkvirtualenv facecourse-py3 -p python3
workon facecourse-py3
# now install python libraries within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn ipython
# quit virtual environment
deactivate
######################################
我们将从他们的GitHub仓库下载opencv和opencv_contrib软件包。
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd opencv
mkdir build
cd build
# 如果不支持cuda,移除 -D WITH_CUDA=ON
#Remove the line WITH_CUDA=ON if you dont have CUDA in your system
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D WITH_CUDA=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
# find out number of CPU cores in your machine
nproc
# substitute 4 by output of nproc
# 使用 nproc输出的值代替 4
make -j4
# 如果报错 执行
make -j1
sudo make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
根据你的Python版本,路径会有所不同。 OpenCV的Python二进制文件(cv2.so)可以安装在目录site-packages或dist-packages中。 使用以下命令找出机器上的正确位置。
find /usr/local/lib/ -type f -name "cv2*.so"
它应该输出类似于其中之一的路径(或者在为Python2和Python3编译OpenCV的情况下输出两个路径:
############ For Python 2 ############
## binary installed in dist-packages
/usr/local/lib/python2.6/dist-packages/cv2.so
/usr/local/lib/python2.7/dist-packages/cv2.so
## binary installed in site-packages
/usr/local/lib/python2.6/site-packages/cv2.so
/usr/local/lib/python2.7/site-packages/cv2.so
############ For Python 3 ############
## binary installed in dist-packages
/usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so
/usr/local/lib/python3.6/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.so
## binary installed in site-packages
/usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so
/usr/local/lib/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so
运行以下命令之前,请仔细检查机器上的确切路径
############ For Python 2 ############
cd ~/.virtualenvs/facecourse-py2/lib/python2.7/site-packages
ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so
############ For Python 3 ############
cd ~/.virtualenvs/facecourse-py3/lib/python3.6/site-packages
ln -s /usr/local/lib/python3.6/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
我们将测试用OpenCV编写的红眼去除器应用程序来测试我们的C ++和Python安装。 下载RedEyeRemover.zip并将其解压缩到一个文件夹中。
在提取的文件夹中移动,编译并运行。
# compile
# There are backticks ( ` ) around pkg-config command not single quotes
g++ -std=c++11 removeRedEyes.cpp `pkg-config --libs --cflags opencv` -o removeRedEyes
# run
./removeRedEyes
############ For Python 2 ############
workon facecourse-py2
############ For Python 3 ############
workon facecourse-py3
# open ipython (run this command on terminal)
ipython
# import cv2 and print version (run following commands in ipython)
import cv2
print cv2.__version__
# If OpenCV3 is installed correctly,
above command should give output 3.3.0
# Press CTRL+D to exit ipython
python removeRedEyes.py
现在你可以退出Python的虚拟环境。
deactivate
无论何时您要运行使用OpenCV的Python脚本,您都应该使用workon命令激活我们创建的虚拟环境。