Opencv 安装

本文主要说明了在ubuntu上通过源码安装Opencv3,包含各种独立接口.具体可以参照LearnOpencv:

https://www.learnopencv.com/install-opencv3-on-ubuntu/

更新系统源

    sudo apt-get update
sudo apt-get upgrade

安装系统依赖库

    # 移除先前安装的x264</h3>
sudo apt-get remove x264 libx264-dev #安装依赖项
sudo apt-get install build-essential checkinstall cmake pkg-config yasm
sudo apt-get install git gfortran
sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev # Ubuntu 14.04
sudo apt-get install libtiff4-dev
# 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 libqt5-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 # 可选依赖项,一般DL都需要
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

python环境

    #python2.7与3接口
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
######################################

OpenCV与OpenCV_contrib

此处均从github的源码下载
    git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 3.3.1
cd ..
#contrib
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.3.1
cd ..

开始编译

    cd opencv
mkdir build
cd build 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 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
make -j4
sudo make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

\(python使用opencv需要将opencv编译的cv2.so加入到python环境中,实际放在python \\
下的site-package或者dist-package,查找cv2.so位置\)

    find /usr/local/lib -type f -name "cv2*.so"

    # 结果出现可能如下
############ 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++接口

    # compile
g++ -std=c++11 removeRedEyes.cpp `pkg-config --libs --cflags opencv` -o removeRedEyes
# run
./removeRedEyes

python 接口

激活虚拟环境

  ############ For Python 2 ############
workon facecourse-py2 ############ For Python 3 ############
workon facecourse-py3 ipython
import cv2
print cv2.__version__ # 或者执行python removeRedEyes.py # 退出虚拟环境
deactivate

本文作者: 张峰

本文链接: http://www.enjoyai.site/2017/11/23/InstallOpenCV/

版权声明: 本博客所有文章,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!

OpenCV3 安装的更多相关文章

  1. Linux 下 OpenCV3 安装

    编译安装OpenCV3 从官网下载:http://opencv.org/releases.html 选择一个较新版本的opencv3.X,下载source源代码 下载之后解压,并cd到该文件夹进行编译 ...

  2. ubuntu18.04 下利用conda安装opencv3

    ubuntu18.04 下利用conda安装opencv3 安装opencv3 conda install -c https://conda.anaconda.org/menpo opencv3 出现 ...

  3. Ubuntu 17.04版本下,opencv进行源码编译安装

    本文主要针对Ubuntu 17.04版本下,opencv进行源码编译安装.开发环境主要针对python 对 openCV库的调用. 安装 gcc cmake 编译环境 sudo apt-get ins ...

  4. opencv基础教程

    1,基本语法 环境:python3.6.6+numpy+opencv3 安装:没有详细编译,直接pip install opencv-python 矩阵和图片: img=numpy.zeros((3, ...

  5. 7行Python代码的人脸识别

    随着去年alphago 的震撼表现,AI 再次成为科技公司的宠儿.AI涉及的领域众多,图像识别中的人脸识别是其中一个有趣的分支.百度的BFR,Face++的开放平台,汉王,讯飞等等都提供了人脸识别的A ...

  6. darknet+opencv在windows上的编译

    darknet 源码网站:https://github.com/pjreddie/darknet 技术支持官网:https://pjreddie.com/darknet/ darknet采用C++编写 ...

  7. ubuntu---记录.opencv多版本管理与切换

    首先当然准备两个不同版本opencv (比如2..9和3.1.0) cmake-gui 设置 CMAKE_INSTALL_PREFIX 安装路径:/usr/local/opencv2 安装路径 :/u ...

  8. centos 安装 opencv-3.1.0

    官方安装教程 http://docs.opencv.org/3.1.0/d9/d52/tutorial_java_dev_intro.html#gsc.tab=0 注意要先安装jdk和apache a ...

  9. ubuntu16041,安装opencv3.1.0

    [非常感谢:http://www.linuxdiyf.com/linux/18482.html] 1.依赖关系: sudo apt-get install build-essentialsudo ap ...

随机推荐

  1. 超实用的JavaScript代码段

    1. 判断日期是否有效 JavaScript中自带的日期函数还是太过简单,很难满足真实项目中对不同日期格式进行解析和判断的需要.JQuery也有一些第三方库来使日期相关的处理变得简单,但有时你可能只需 ...

  2. Java中数组的反转

    public class ArrayDemo2 { public static void main(String[] args) { //定义一个数组存放元素 int[] arr3 = {10, 20 ...

  3. Ubuntu Server 18.04 LTS 安装

    版本:Ubuntu Server 18.04.1 LTS 环境:VMware Workstation 14 Player 下载地址:https://www.ubuntu.com/download/se ...

  4. WPF 创建用户控件并引用

    项目源码地址:https://github.com/lizhiqiang0204/WpfControlLibrary.git 首先创建新项目->WPF用户控件库项目 在UserControl1. ...

  5. css3媒介查询使用规则小结

    响应式布局可以为不同终端的用户提供更加舒适的界面和更好的用户体验,而且随着目前大屏幕移动设备的普及,用大势所趋来形容也不为过. 用一句话来说:使用同一套Html代码来适配不同设备和满足不同场景不同用户 ...

  6. php进程控制

    1 POSIX扩展    posix_access($file,$mode)  查看文件的访问权限,可以由is_readable等几个函数代替    posix_errno()  返回posix函数执 ...

  7. [NOI2005]瑰丽华尔兹_动态规划_单调队列

    Code: #include<cstdio> #include<cstring> #include<deque> #include<algorithm> ...

  8. laravel 运行migrate报错 1071 Specified key was too long

     转自:https://segmentfault.com/a/1190000008416200 laravel运行命令migrate时报错: 1071 Specified key was too lo ...

  9. HDU1846 - Brave Game【巴什博弈】

    十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫<勇敢者的游戏>(英文名称:Zathura),一直到现在,我依然对于电影中的部分电脑特技印象深刻.  今天,大家选 ...

  10. Spring 静态工厂实例

    直接上代码,看注释. 创建实体类: package com.spring.classs; public class Test {    private String name;    private ...