ubuntu编译安装opencv

时间:2023-03-08 17:03:01
ubuntu编译安装opencv

简易安装opencv2:

conda install --channel https://conda.anaconda.org/menpo opencv

或:

sudo apt-get install libopencv-dev python-opencv

简易安装opencv3:

pip install opencv-python

或:

pip install opencv-python==3.1.0

简易安装方式是从库中安装编译好了的Opencv,这种安装方式简单方便,缺点是容易在使用中出现未知bug,比如不能imshow图像,不能读视频文件等(opencv3好像好一点)。

推荐使用源码编译安装。

源码编译安装Opencv2或Opencv3

1.  安装依赖文件

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

2. 下载源码包

官网下载地址: https://opencv.org/opencv-3-2.html

这里选择下载GitHub源上适用所有平台的tar.gz文件(下载链接:https://github.com/opencv/opencv/archive/3.2.0.tar.gz),opencv-3.2.0.tar.gz, 文件大小75MB

 

 3. 解压源码并编译安装

tar xvf opencv-3.2.0.tar.gz
cd opencv-3.2.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install

备注: 执行cmake指令的时候,需要下载一个ippicv_linux_20151201.tgz文件,如果网不好,可能会卡在:

ICV: Downloading ippicv_linux_20151201.tgz...

这时候可以单独下载 ippicv_linux_20151201.tgz文件(文件大小36.5MB,下载链接:https://raw.githubusercontent.com/Itseez/opencv_3rdparty/81a676001ca8075ada498583e4166079e5744668/ippicv/ippicv_linux_20151201.tgz),下载后放到  ‘/opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/’  文件夹下再重新cmake就行了。