点击小箭头阅读更舒适 如果觉得卡顿,请点击小眼睛关闭蜘蛛网特效

Ubuntu下源码安装Opencv完全指南

《Ubuntu下源码安装Opencv完全指南》

前言

Opencv大家很熟悉了,经典的图像处理库,Opencv在Windows下安装是很简单的,只需要配置DLL即可。但是在Linux下,因为Linux各种发行版本多种多样,所以我们只有自己通过编译源码的方式来安装Opencv了,源码安装会自动根据你当前的Ubuntu系统中安装的组件来编译Opencv源码,所以说你编译好的这份Opencv库是独一无二的,移到别的地方就不行了哦。

这里讲解如何在Ubuntu系统中利用源码安装Opencv。

《Ubuntu下源码安装Opencv完全指南》

安装指南

最开始,老规矩先更新下我们的系统:

sudo apt-get update
sudo apt-get upgrade

其次我们要做的是——安装所有依赖件:

# 首先我们先移除系统中已经存在的依赖,这一部必须要做
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 qt5-default 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
 
# 可选安装
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

需要注意的是,上面的这些依赖件一定要都确认安装完毕,如果某些依赖没有安装正确后,编译好的Opencv会缺失一些基本的功能(在编译Opencv过程中会有提示,但是不会中断!),如果源码编译好后发现Opencv有功能缺失。

例如。利用 cv2.imread() 读出的图像是 None

最好确认一下上述的依赖是否安装正确。

安装python依赖(可选)

(注意,这部分只适合系统中没有安装任何python组件,如果你的系统已经安装了python组件,那么只需要安装python-dev和python3-dev即可,然后在各自的环境中pip安装 numpy scipy matplotlib scikit-image scikit-learn ipython 即可)

安装好所有依赖后,我们接下来安装需要的Python依赖,两个版本的都要装,Opencv会自动生成两个版本的python依赖。
(如果只想用C/C++版不想用python版可以不安装,Opencv安装时会自动忽略)

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

创建虚拟环境进行安装:

# 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

下载下来就对了。

git clone https://github.com/opencv/opencv.git
cd opencv 
git checkout 3.4.2 
cd ..

git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.4.2
cd ..

安装OpenCv

安装过程很简单,进入我们的opencv目录,创建一个build的文件夹,进入这个文件夹执行相应的命令:

cd opencv              # 进入目录
mkdir build            # 创建文件夹
cd build               

进入后,首先我们利用cmake生成makefile,当然还有很多的选择项设置,具体可以查看cmakelist文件中的帮助信息,对于我们来说这些就足够了:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..

如果对CMakeLists文件不进行修改,那么Opencv默认的安装位置

/usr/local/include/opencv2/         -- 新版Opencv核心头文件
/usr/local/include/opencv/          -- 旧Opencv核心头文件
/usr/local/share/OpenCV/            -- 一些Opencv其他安装信息
/usr/local/lib/                     -- Opencv中的动态链接库存放位置

cmake后,我们进行make操作,首先利用nproc命令查看系统有多少线程,其次按照线程数来执行:

# 利用下面的命令得到系统的线程数
nproc
# 得到n下面输入jn
make -j12                        # 这里我是12
sudo make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

假如使用python接口的话,我们编译好了OpenCV后需要查看生成的.so文件在哪里:

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

找到的位置因为自己的python安装地址不同,生成的地址也不同:

############ For Python 2 ############
## binary installed in dist-packages
path/to/python2.6/site-packages/cv2.so
path/to/python2.7/site-packages/cv2.so
## binary installed in site-packages
path/to/python2.6/site-packages/cv2.so
path/to/python2.7/site-packages/cv2.so
  
############ For Python 3 ############
## binary installed in dist-packages
path/to/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so
path/to/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so
## binary installed in site-packages
path/to/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so
path/to/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so

然后根据地址来生成link(注意,下方的地址修改成自己的地址):

############ For Python 2 ############
cd path/to/python2.7/site-packages
ln -s path/to/python2.7/site-packages/cv2.so cv2.so
  
############ For Python 3 ############
cd path/to/python3.6/site-packages
ln -s path/to/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so

后续操作

测试安装是否正确

很简单,自己写个使用Opencv的test.cpp的文件,然后利用下面的命令编译一下看看能不能运行即可:

g++ std=c++11 test.cpp ‘pkgconfig libs cflags opencv’ o test

# 测试是否可以跑起来

./test

对于python的话,在安装OpenCV的python环境下:

>>> import cv2
>>> print(cv2.__version__)
# 如果有显示,就显示安装成功了!但是最好再测试一下其他的功能。

卸载源码安装的Opencv

想要卸载OpenCV的话,在之前创建的built(这是我的名称,你的可能不一样)文件夹中执行sudo make uninstall

prototype@prototype-X299-UD4-Pro:~/Documents/opencv-3.4.2/built$ sudo make uninstall
[sudo] password for prototype: 
Scanning dependencies of target uninstall
-- Uninstalling "/usr/local/include/opencv2/cvconfig.h"
-- Uninstalling "/usr/local/include/opencv2/opencv_modules.hpp"
-- Uninstalling "/usr/local/lib/pkgconfig/opencv.pc"
-- Uninstalling "/usr/local/share/OpenCV/OpenCVModules.cmake"
-- Uninstalling "/usr/local/share/OpenCV/OpenCVModules-release.cmake"
-- Uninstalling "/usr/local/share/OpenCV/OpenCVConfig-version.cmake"
-- Uninstalling "/usr/local/share/OpenCV/OpenCVConfig.cmake"
-- Uninstalling "/usr/local/share/OpenCV/valgrind.supp"
-- Uninstalling "/usr/local/share/OpenCV/valgrind_3rdparty.supp"
-- Uninstalling "/usr/local/include/opencv/cv.h"
-- Uninstalling "/usr/local/include/opencv/cv.hpp"
-- Uninstalling "/usr/local/include/opencv/cvaux.h"
-- Uninstalling "/usr/local/include/opencv/cvaux.hpp"
-- Uninstalling "/usr/local/include/opencv/cvwimage.h"
-- Uninstalling "/usr/local/include/opencv/cxcore.h"
-- Uninstalling "/usr/local/include/opencv/cxcore.hpp"
-- Uninstalling "/usr/local/include/opencv/cxeigen.hpp"
-- Uninstalling "/usr/local/include/opencv/cxmisc.h"
-- Uninstalling "/usr/local/include/opencv/highgui.h"
-- Uninstalling "/usr/local/include/opencv/ml.h"
-- Uninstalling "/usr/local/include/opencv2/opencv.hpp"
-- Uninstalling "/usr/local/lib/libopencv_cudev.so.3.4.1"
-- Uninstalling "/usr/local/lib/libopencv_cudev.so.3.4"
-- File "/usr/local/lib/libopencv_cudev.so.3.4" does not exist.
...

如果卸载不干净,则执行下面的命令:

sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv  /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*

遇到的问题

Scanning dependencies of target opencv_cudafilters
[ 45%] Building CXX object modules/cudafilters/CMakeFiles/opencv_cudafilters.dir/src/filtering.cpp.o
cc1plus: warning: /home/prototype/Downloads/opencv-3.4.0/build/modules/cudafilters/precomp.hpp.gch/opencv_cudafilters_RELEASE.gch: not used because 'OPENCV_TRAITS_ENABLE_DEPRECATED' is defined [-Winvalid-pch]
cc1plus: warning: /home/prototype/Downloads/opencv-3.4.0/build/modules/cudafilters/precomp.hpp.gch/opencv_cudafilters_Release.gch: not used because 'OPENCV_TRAITS_ENABLE_DEPRECATED' is defined [-Winvalid-pch]
[ 45%] Linking CXX shared library ../../lib/libopencv_cudafilters.so
[ 45%] Built target opencv_cudafilters
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2

遇到上面的问题,一般删除所有的OpenCV残留,重来即可。

参考资料:

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

  点赞
本篇文章采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可
转载请务必注明来源: https://oldpan.me/archives/ubuntu-install-opencv-from-source

   欢迎关注Oldpan博客微信公众号,同步更新博客深度学习文章。


 猜你喜欢

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论审核已启用。您的评论可能需要一段时间后才能被显示。