OpenCV(Open Source Computer Vision Library) 是一个基于BSD许可(开源)发行的跨平台计算机视觉库,它具有C ++,C,Python和Java接口,可以运行在Linux、Windows、Mac OS和Android操作系统上。OpenCV的设计是为了提高计算效率,并将重点放在实时应用程序上, 用优化的C / C ++编写,库可以利用多核处理,实现了图像处理和计算机视觉方面的很多通用算法。
#-----------------------------------------------------------------------------------------------------------
0x00 前言
但有时候因为网速或者别的原因,总之我就觉得里面好繁琐,我从上述项目里提炼出关键的语句,运行即可安装成功。
本次只制作ubuntu下的安装方法,其他系统的安装你们可以参照上述项目。
0x01 准备工作
下载opencv源码包(现在最新版本为3.3.1):
更新下ubuntu:
#change to rootsudo -s
#update
apt-get update
准备下文件dependencies.sh
#!/bin/bashecho "--- Removing any pre-installed ffmpeg and x264"
sudo apt-get -qq remove ffmpeg x264 libx264-devfunctioninstall_dependency {echo "--- Installing dependency: $1"
sudo apt-get -y install $1}
install_dependency libopencv-dev
install_dependency build-essential
install_dependency checkinstall
install_dependency cmake
install_dependencypkg-configinstall_dependency yasm
install_dependency libtiff5-dev
install_dependency libjpeg-dev
install_dependency libjasper-dev
install_dependency libavcodec-dev
install_dependency libavformat-dev
install_dependency libswscale-dev
install_dependency libdc1394-22-dev
install_dependency libxine2-dev
install_dependency libgstreamer0.10-dev
install_dependency libgstreamer-plugins-base0.10-dev
install_dependency libv4l-dev
install_dependency python-dev
install_dependency python-numpy
install_dependency libtbb-dev
install_dependency libqt5x11extras5
install_dependency libqt5opengl5
install_dependency libqt5opengl5-dev
install_dependency libgtk2.0-dev
install_dependency libfaac-dev
install_dependency libmp3lame-dev
install_dependency libopencore-amrnb-dev
install_dependency libopencore-amrwb-dev
install_dependency libtheora-dev
install_dependency libvorbis-dev
install_dependency libxvidcore-dev
install_dependency x264
install_dependency v4l-utils
#install_dependency ffmpeg
install_dependencyunzip
准备下install.sh
#!/bin/bash
version=3.3.1downloadfile=opencv-${version}.zipdldir=OpenCV
source dependencies.sh
mkdir -p $dldirmv $downloadfile $dldir/$downloadfile
cd $dldirunzip$downloadfile
cd opencv-$versionmkdirbuild
cd build
cmake-D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..make -j 4
sudo make install
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudoldconfigecho "OpenCV" $version "ready to be used"
0x02 安装OpenCV
只要opencv-3.3.1.zip、dependencies.sh、install.sh在同一个文件夹下,就可以进行安装:
chmod +x dependencies.sh
chmod +x install.shsourceinstall.sh
这安装过程会相对漫长。
0x03 编译OpenCV程序
例如要编译的文件为pcat.cpp
g++ -ggdb `pkg-config –-cflags opencv` `pkg-config –-libs opencv` pcat.cpp –o pcat
0x04 windows安装numpy、cv2等python2.7模块
对于windows,我暂时只需要cv2这个python2.7模块,而这个cv2模块依赖numpy模块
numpy的安装:
现在numpy最新版本是1.13.3,python的32位版本就下载numpy-1.13.3+mkl-cp27-cp27m-win32.whl,64位版本就下载numpy-1.13.3+mkl-cp27-cp27m-win_amd64.whl
pip install numpy-xxx.whl
cv2的安装:
这个opencv-3.3.1-vc14.exe点击后会自动解压,包含sources和build文件夹,\opencv\sources\samples可供学习。
打开\opencv\build\python\2.7,有x86和x64两个版本,关键看你的python是什么版本,复制相对应版本的文件夹下面的cv2.pyd
复制到python2.7的安装目录\Lib\site-packages里面即可
之后可以打开python验证是否成功
import cv2