ubuntu下编译安装opencv

本篇博客使用的ubuntu版本是18.04和opencv3.2

首先安装CMAKE

sudo apt-get install cmake

接着安装一些依赖项

sudo apt-get install build-essential pkg-config

安装视频I/O包:

sudo apt-get install libgtk2.0-dev libavcodec-dev libavformat-dev libswscale-dev

安装gtk2.0:

sudo apt-get install libgtk2.0-dev

安装常用图像工具包:

sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libdc1394-22-dev

如果没有报错,那是最好的。但是我报错了。报错信息如下:

E: Package 'libpng12-dev' has no installation candidate
E: Unable to locate package libjasper-dev

上网找了解决方案,首先先解决第一个错:

libpng12-dev在Ubuntu16.04之后就被丢弃了,所以放弃用这个吧。把 libpng12-dev 换成 libpng-dev 就行了

接着是第二个错误:

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

然后从官网下载源码,直接git clone。也可以从windows拷到虚拟机(要装VM tools)。网址

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

在解压后的文件夹中新建build文件夹,用来存放编译文件。

mkdir build
cd build

在电脑上有多个版本的python时,可以通过-D PYTHON_DEFAULT_EXECUTABLE=$(which python3)来确定安装在哪个版本python上。

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON_DEFAULT_EXECUTABLE=​\$(which python3) ..

如果执行上面代码没有问题,直接make。接着再执行sudo make install。

我又报错了。。。。报错信息如下:

— ICV: Downloading ippicv_linux_20151201.tgz…
CMake Error at 3rdparty/ippicv/downloader.cmake:73 (file):
file DOWNLOAD HASH mismatch

for file: [/root/library/opencv/opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/ippicv_linux_20151201.tgz]
expected hash: [808b791a6eac9ed78d32a7666804320e]
actual hash: [d41d8cd98f00b204e9800998ecf8427e]
status: [1;”Unsupported protocol”]

Call Stack (most recent call first):
3rdparty/ippicv/downloader.cmake:110 (_icv_downloader)
cmake/OpenCVFindIPP.cmake:243 (include)
cmake/OpenCVFindLibsPerf.cmake:37 (include)
CMakeLists.txt:558 (include)

CMake Error at 3rdparty/ippicv/downloader.cmake:77 (message):
ICV: Failed to download ICV package: ippicv_linux_20151201.tgz.
Status=1;”Unsupported protocol”
Call Stack (most recent call first):
3rdparty/ippicv/downloader.cmake:110 (_icv_downloader)
cmake/OpenCVFindIPP.cmake:243 (include)
cmake/OpenCVFindLibsPerf.cmake:37 (include)
CMakeLists.txt:558 (include)

— Configuring incomplete, errors occurred!
See also “/root/library/opencv/opencv-3.2.0/build/CMakeFiles/CMakeOutput.log”.
See also “/root/library/opencv/opencv-3.2.0/build/CMakeFiles/CMakeError.log”.

百度一下,这是因为我们在编译opencv的时候需要下载ippicv_linux_20151201.tgz,但是由于网络的原因,经常下载失败。解决方案:

手动下载ippicv_linux_20151201.tgz,网上都有资源。读者找一下。接着放入相关路径,我的路径是

home/opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/。读者根据自己的路径放。

在重新执行cmake。

接着执行完cmake后,make编译,sudo make install 安装。

到这里还没装完我们要编译的文件,还有一些模块保留在opencv_contrib的资源库中。所以这个我们也要编译。

将opencv_contrib下到build的同级目录下,在build目录下打开终端或者在终端进入build目录,

cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.2.0/modules/ ..
make
sudo make install

在终端运行python3,import cv2。没有报错,opencv安装就成功了。

我装过好几次opencv,这是我最顺的一次。。。。

----本文结束,感谢您的阅读。如有错,请指正。----
大哥大嫂过年好!支持我一下呗
0%