2. 安装OpenCV3.1
OpenCV参考资料
OpenCV安装手册:http://www.learnopencv.com/install-opencv3-on-ubuntu/
OpenCV安装参考: http://blog.youkuaiyun.com/yangdashi888/article/details/52088264
Learn OpenCV : C++ and Python Examples
https://github.com/spmallick/learnopencv
Step 1: Update packages
sudo apt-get update
sudo apt-get upgrade
Step 2: Install OS libraries
Step 2.1: 删除以前安装的任何 x264
sudo apt-get remove x264 libx264-dev
Step 2.2: Install dependencies
sudo apt-get install build-essential checkinstall cmakepkg-config yasm gfortran git
sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev
sudo apt-get install libtiff4-dev
sudo apt-get install libavcodec-dev libavformat-devlibswscale-dev libdc1394-22-dev
sudo apt-get install libxine2-dev libv4l-dev
sudo apt-get install libgstreamer0.10-devlibgstreamer-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-devlibtheora-dev
sudo apt-get install libvorbis-dev libxvidcore-dev
sudo apt-get install libopencore-amrnb-devlibopencore-amrwb-dev
sudo apt-get install x264 v4l-utils
Step 2.3: Optional dependencies
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-devlibhdf5-dev doxygen
Step2.4给用户moses设定root权限
su - root
gedit /etc/sudoers
加入:
# User privilege specification
root ALL=(ALL:ALL)ALL
moses ALL=(ALL:ALL)ALL
Step2.5安装ffmpeg
sudoadd-apt-repository ppa:mc3man/trusty-media[s1]
$ sudo apt-get install ffmpeg gstreamer0.10-ffmpeg
$ sudo apt-get install libx264-dev
Step2.6安装Java、ant
1. 下载jdk
http://www.Oracle.com/technetwork/Java/javase/downloads/jdk8-downloads-2133151.html
并把下载文件解压缩到/usr/local/jdk1.8.0_131
apache-ant-1.10.1
2. sudo gedit /etc/profile,最后加入:
export ANT_HOME=/usr/local/apache-ant
export JAVA_HOME=/usr/local/jdk1.8.0_131
export JRE_HOME=$JAVA_HOME/jre
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH:$ANT_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
3. 使配置生效 source /etc/profile
Step 3: Install Python libraries
升级Python:
Ubuntu14.04默认的python2.7是Python2.7.6,python3是Python 3.4.3,需要升级才能进行后面的安装
$ sudo add-apt-repository ppa:fkrull/deadsnakes-python2.7
$ sudo apt-get update
$ sudo apt-get upgrade
sudo add-apt-repository ppa:fkrull/deadsnakes
$sudo apt-get update
$sudo apt-get upgrade python3.5 //也可以装3.6,看个人喜好了。
sudo apt-get install python-dev python-pip python3-devpython3-pip
sudo-H pip2 install -U pip numpy
sudo -H pip3 install -U pip numpy
如果出现错误:
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirementpip in /usr/lib/python2.7/dist-packages,
解决:
新建文件~/.pip/pip.conf,并添加以下内容。
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com
We will use Virtual Environment to install Python libraries.It is generally a good practice in order to separate your project environmentand global environment.
# Install virtual environment
sudo pip2 install virtualenvvirtualenvwrapper
第二次安装的时候,报错:The directory '/home/moses/.cache/pip/http'or its parent directory is not owned by the current user and the cache has beendisabled. Please check the permissions and owner of that directory. Ifexecuting pip with sudo, you may want sudo's -H flag.
解决:sudo -H pip2install 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
mkvirtualenvfacecourse-py2 -p python2[s6]
workon facecourse-py2
# now install python libraries within this virtualenvironment
pipinstall numpy scipy matplotlib scikit-image scikit-learn ipython
报错1:
Exception:
Traceback(most recent call last):
File "/home/moses/.virtualenvs/facecourse-py2/local/lib/python2.7/site-packages/pip/basecommand.py",line 215, in main
报错2:ImportError: No module named urllib3
(facecourse-py2)moses@ubuntu:/$ sudo pip install --upgrade requests
报错3:Command"python setup.py egg_info" failed with error code 1 in/tmp/pip-build-0U6ZZT/ipython/
解决:$ pipinstall --upgrade setuptools
报错4:IPython 6.0+ does not support Python 2.6,2.7, 3.0, 3.1, or 3.2.
When using Python 2.7, please installIPython 5.x LTS Long Term Support version.
Beginning with IPython 6.0, Python 3.3 andabove is required.
解决:
(facecourse-py2) moses@ubuntu:/$ pip install numpy scipymatplotlib scikit-image scikit-learn
(facecourse-py2) moses@ubuntu:/$ sudo apt-get installipython
# quit virtual environment
(facecourse-py2) moses@ubuntu:/$ deactivate
######################################
############ For Python 3 ############
# create virtual environment
mkvirtualenv facecourse-py3 -p python3
workon facecourse-py3
# now install python libraries within this virtualenvironment
pip install numpy scipy matplotlib scikit-image scikit-learnipython
# quit virtual environment
deactivate
######################################
Step 4: Download OpenCV and OpenCV_contrib
We will download opencv and opencv_contribpackages from their GitHub repositories.
Step 4.1: Download opencv from Github
If you have OpenBlas installed on yourmachine, OpenCV 3.2.0 fails to compile. We will use the commit where this bughas been patched. So this is OpenCV 3.2.0 with few bugs patched.
git clonehttps://github.com/opencv/opencv.git
cd opencv
git checkout2b44c0b6493726c465152e1db82cd8e65944d0db
cd ..
Step 4.2: Download opencv_contrib fromGithub
If we use v3.2.0, python module ofopencv (cv2) fails to import due to a bug in opencv_contrib. Here too we willuse a commit where this bug has been patched.
git clonehttps://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkoutabf44fcccfe2f281b7442dac243e37b7f436d961
cd ..[s8]
Step 5: Compile and install OpenCV with contrib modules
Step 5.1: Create a build directory inside OpenCV directory
cd opencv
mkdir build
cd build
Step 5.2: Run CMake
$ sudo apt-get install cmake-qt-gui #cmake图形化界面
$ cmake-gui #运行图形化cmake
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 \
-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
第一次make会报错,因为下载不到ippicv_linux_20151201.tgz,第二次编译前,把文件ippicv_linux_20151201.tgz,复制到/opt/opencv/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e目录下。
升级cmake
$ sudo add-apt-repository ppa:george-edison55/cmake-3.x
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get --reinstall install cmake
Step 5.3: Compile and Install
# find out number of CPU cores in your machine
nproc
# substitute 4 by output of nproc
make-j4
参考资料:http://blog.youkuaiyun.com/eagelangel/article/details/51530425
文件:/opt/opencv/modules/highgui/src/window_QT.cpp中,mouse_wheel和QPoingF没有定义
参考资料:https://github.com/opencv/opencv/issues/6993
我的解决方案:在cmake里面把QT给关了……郁闷啊
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=OFF \
-D WITH_OPENGL=ON \
-DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
$ sudo make install
$ sudo sh -c 'echo "/usr/local/lib" >>/etc/ld.so.conf.d/opencv.conf'
$ sudo ldconfig
Step 5.4: Create symlink in virtual environment
Depending upon Python version you have, paths would bedifferent. OpenCV’s Python binary (cv2.so) can be installed either in directorysite-packages or dist-packages. Use the following command to find out thecorrect location on your machine.
$ find/usr/local/lib/ -type f -name "cv2*.so"
/usr/local/lib/python3.4/dist-packages/cv2.cpython-34m.so
/usr/local/lib/python2.7/dist-packages/cv2.so
It shouldoutput paths similar to one of these (or two in case OpenCV was compiled forboth Python2 and Python3:
############For Python 2 ############
## binaryinstalled in dist-packages
/usr/local/lib/python2.7/dist-packages/cv2.so
## binaryinstalled in site-packages
/usr/local/lib/python2.7/site-packages/cv2.so
############For Python 3 ############
## binaryinstalled 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
## binaryinstalled 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
Double checkthe exact path on your machine before running the following commands
############ 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.4/site-packages
ln -s /usr/local/lib/python3.4/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.socv2.so
Step 6: Test OpenCV3.1
We will test a red eye remover application written in OpenCVto test our C++ and Python installations. Download RedEyeRemover.zip andextract it into a folder.
Step 6.1: Test C++ code
Move inside extracted folder, compile and run.
# compile
# There are backticks ( ` ) around pkg-config command not singlequotes
g++ -std=c++11 removeRedEyes.cpp `pkg-config --libs --cflagsopencv` -o removeRedEyes
# run
./removeRedEyes
参考资料:http://www.learnopencv.com/automatic-red-eye-remover-using-opencv-cpp-python/
Step 6.2: Test Python code
Activate Python virtual environment
############ For Python 2 ############
workon facecourse-py2
############ For Python 3 ############
workon facecourse-py3
Quick Check
# open ipython (run this command on terminal)
ipython
# import cv2 and print version (run following commands inipython)
import cv2
print cv2.__version__
# If OpenCV3 is installed correctly, the above commandshould give output 3.2.0
# Press CTRL+D to exit ipython
Run RedEyeRemover demo
python removeRedEyes.py
Now you can exit from Python virtual environment.
deactivate
Whenever you are going to run Python scripts which useOpenCV you should activate the virtual environment we created, using workoncommand.
/opt/opencv/modules/highgui/src/window_QT.cpp: Inmember function ‘virtual void DefaultViewPort::wheelEvent(QWheelEvent*)’:
/opt/opencv/modules/highgui/src/window_QT.cpp:2637:27:error: ‘mouse_wheel’ was not declared in this scope
icvmouseHandler(evnt, mouse_wheel, cv_event,flags);
^
/opt/opencv/modules/highgui/src/window_QT.cpp:2638:34:error: ‘QPoingF’ was not declared in this scope
icvmouseProcessing(QPoingF(pt), cv_event, flags);
^
/opt/opencv/modules/highgui/src/window_QT.cpp:2643:5:error: ‘mouseWheel’ is not a member of ‘QWidget’
QWidget::mouseWheel(evnt);
^
make[2]: ***[modules/highgui/CMakeFiles/opencv_highgui.dir/src/window_QT.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: ***[modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
make: *** [all] Error 2