learn opencv-Ubuntu安装opencv3

参考:
https://github.com/spmallick/learnopencv


在这篇文章中,我们将提供在Ubuntu上安装OpenCV 3(C ++和Python)的分步说明。

注意:如果网络环境不好,即使完全按照该教程执行,也可能安装失败的,建议在一个较好的网络环境下安装(因为需要在线下载)

第1步:更新软件包

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

第2步:安装OS库

步骤2.1:删除以前安装的任何x264

sudo apt-get remove x264 libx264-dev

步骤2.2:安装依赖关系

# 可以将这些命令写入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

步骤2.3:可选的依赖关系

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

第3步:安装Python库

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
######################################

步骤4:下载OpenCV和OpenCV_contrib

我们将从他们的GitHub仓库下载opencv和opencv_contrib软件包。

步骤4.1:从Github下载opencv

git clone https://github.com/opencv/opencv.git

步骤4.2:从Github下载opencv_contrib

git clone https://github.com/opencv/opencv_contrib.git

第五步:用contrib模块编译并安装OpenCV

步骤5.1:在OpenCV目录下创建一个build目录

cd opencv
mkdir build
cd build

步骤5.2:运行CMake

# 如果不支持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 ..

步骤5.3:编译并安装

# 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

步骤5.4:在虚拟环境中创建符号链接

根据你的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

第6步:测试OpenCV3

我们将测试用OpenCV编写的红眼去除器应用程序来测试我们的C ++和Python安装。 下载RedEyeRemover.zip并将其解压缩到一个文件夹中。

步骤6.1:测试C ++代码

在提取的文件夹中移动,编译并运行。

# 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

步骤6.2:测试Python代码

激活Python虚拟环境

############ 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

运行RedEyeRemover演示

python removeRedEyes.py

现在你可以退出Python的虚拟环境。

deactivate

无论何时您要运行使用OpenCV的Python脚本,您都应该使用workon命令激活我们创建的虚拟环境。

你可能感兴趣的:(opencv,opencv)