1.下载opencv3.4.0及opencv3.4.0_contrib
opencv和opencv_contrib下载地址:
https://github.com/opencv/opencv/releases
https://github.com/opencv/opencv_contrib/releases
将opencv3.4.0及opencv3.4.0_contrib解压,将opencv3.4.0_contrib的文件夹放到opencv3.4.0的文件夹中.
2.编译安装opencv
(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
如果有报错说libjaster-dev无法安装,解决办法如下:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
(2).新建build文件夹进行编译
先设置路径文件:
sudo mkdir /usr/local/opencv3.4.0
在解压的opencv3.4.0文件下新建build文件夹并打开执行:
cmake -DOPENCV_EXTRA_MODULES_PATH=/home/xxx/opencv-3.4.0/opencv_contrib-3.4.0/modules -DBUILD_opencv_legacy=OFF /home/xxx/opencv-3.4.0 -D ENABLE_PRECOMPILED_HEADERS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv3.4.0 -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
注意: 上面-D OPENCV_EXTRA_MODULES_PATH路径需要改成自己opencv_contrib下modules 的路径
其实,可以认真阅读opencv_contrib下README.md的解释:
$ cd <opencv_build_directory>
$ cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules <opencv_source_directory>
$ make -j5
在执行make时会有以下报错和解决方法:
报错1:
Could not find a package configuration file provided by "Qt5Concurrent"with any of the following names:
Qt5ConcurrentConfig.cmake
qt5concurrent-config.cmake
解决:sudo apt-get install libqglviewer-dev-qt5
报错2:
ICV: Failed to download ICV package: ippicv_linux_20151201.tgz.
解决:网速不行,自行下载ippicv_linux_20151201.tgz(博客上一大堆下载地址)并替换./opencv-3.4.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e下的文件.
报错3:
fatal error: boostdesc_bgm.i: 没有那个文件或目录
解决:https://blog.csdn.net/u011736771/article/details/85960300,下载你缺失的文件,并放到~/opencv_contrib/modules/xfeatures2d/src/下.
报错4:
fatal error: opencv-3.4.0/modules/stitching/include/opencv2/stitching/detail/matchers.hpp:52:12: fatal error: opencv2/xfeatures2d/cuda.hpp: 没有那个文件或目录
解决:找到matchers.hpp文件,将对应的头文件地址修改为绝对路径: /home/xxx/opencv-3.4.0/opencv_contrib-3.4.0/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.hpp 以及在opencv-3.4.0/modules/stitching的cmakelists添加绝对路径: INCLUDE_DIRECTORIES("/home/xxx/opencv-3.4.0/opencv_contrib-3.4.0/modules/xfeatures2d/include")
最后:
make -j4
sudo make install
3.配置OpenCV的编译环境
目的是让系统找到OpenCV的库的路径,打开编写一个空白的文件opencv.conf:
sudo gedit /etc/ld.so.conf.d/opencv.conf
在opencv.conf中添加如下内容:
/usr/local/opencv3.4.0/lib
保存并关闭文件,使刚配置路径生效:
sudo ldconfig
打开bash文件,配置bash:
sudo gedit /etc/bash.bashrc
在打开的bash.bashrc的最末尾添加如下代码:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv3.4.0/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv3.4.0/lib
保存关闭文件,使刚才的配置生效:
source /etc/bash.bashrc
用一个小程序测试一下 到opencv-3.4.0/samples/cpp/example_cmake目录下 我们可以看到这个目录里官方已经给出了一个cmake的example我们可以拿来测试下
cmake .
make
./opencv_example
如果成功运行了电脑摄像头则安装成功.
|