温馨提示:本博文在已安装相同版本的opencv库的环境,安装opencv库请看【Ubuntu上用cmake编译安装OpenCV】。
未安装opencv的亦可将opencv和contrib一起安装,需结合安装opencv教程。
本文实践版本: opencv-3.2.0 opencv-3.4.0
没错,博主就是这么两个版本都折腾了一遍,各种问题见下文。
找到opencv-3.x官方的人脸识别示例,COPY过来准备体验一番,编译的时候竟然出错了!
官方的示例都会出错?没道理的。
赶紧百度了一下,原来还要安装opencv_contrib扩展模块。
准备安装包:
到 https://github.com/opencv/opencv_contrib 下载,记得是要对应版本的包。
针对 opencv-3.xx 应选择 opencv_contrib-3.xx.tar.gz
一、安装相关依赖库
$ 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
注:如若安装失败,请执行更新命令再尝试(再失败请换更换软件源,本人用的是163,亲测可用)
$ sudo apt-get update
$ sudo apt-get upgrade
二、安装
总体思路:解压opencv (针对未安装opencv的)、opencv_contrib,将opencv_contrib移到opencv目录下,在opencv下创建编译目录build,进入build中配置cmake,make,make install。
1、解压
$ tar zxvf opencv_contrib-3.2.0.tar.gz
2、将解压后的库复制/移动至opencv源码目录下
$ mv opencv_contrib-3.2.0 opencv-3.2.0 -rf
3、将进行opencv源码下的build目录,将build目录清空,执行cmake命令(无build目录则创建)
$ cd opencv-3.2.0/build
$ rm * -rf
$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-3.2.0/modules/ ..
命令与安装opecncv库时的相似,多了参数 OPENCV_EXTRA_MODULES_PATH ,指 opencv_contrib 库下的modules目录
最后的“..”指opencv源目录
这一步,在3.2.0版本会出现以下错误(实际上,3.4版本也会有这情况,但不会报错,可能你不会发觉,先不管了):
-- Downloading v3.1.0/protobuf-cpp-3.1.0.tar.gz...
CMake Error at cmake/OpenCVUtils.cmake:1043 (file):
file DOWNLOAD HASH mismatch
for file: [/root/library/opencv/opencv-3.2.0/opencv_contrib-3.2.0/modules/dnn/.download/bd5e3eed635a8d32e2b99658633815ef/v3.1.0/protobuf-cpp-3.1.0.tar.gz]
expected hash: [bd5e3eed635a8d32e2b99658633815ef]
actual hash: [d41d8cd98f00b204e9800998ecf8427e]
status: [1;"Unsupported protocol"]
Call Stack (most recent call first):
opencv_contrib-3.2.0/modules/dnn/cmake/download_protobuf.cmake:23 (ocv_download)
opencv_contrib-3.2.0/modules/dnn/cmake/download_protobuf.cmake:51 (ocv_protobuf_download)
opencv_contrib-3.2.0/modules/dnn/cmake/OpenCVFindLibProtobuf.cmake:30 (include)
opencv_contrib-3.2.0/modules/dnn/CMakeLists.txt:35 (include)
解决方法跟 Ubuntu上用cmake编译安装OpenCV 中遇到的相似。
先手动下载 protobuf-cpp-3.1.0.tar ,再放置指定目录下(见上面输出的提示信息,如下:)
[/root/library/opencv/opencv-3.2.0/opencv_contrib-3.2.0/modules/dnn/.download/bd5e3eed635a8d32e2b99658633815ef/v3.1.0/protobuf-cpp-3.1.0.tar.gz]
PS:这个 protobuf-cpp-3.1.0.tar 很难找,找到的都是要CSDN积分的。无积分的就要找官方下载了。
免费下载地址: https://github.com/google/protobuf 找到对应版本下载就OK了。
敬告:
opencv-3.2这类问题有好几个,解决方法同上。
网上的资源都是要CSDN积分下载的。(我当时求助的是【万能的淘宝】)
这些文件我都有,本想免费共享的,可偏偏CSDN资源非要积分,想免费的私信我吧。
CSDN下载链接【opencv3_cmake_files】:
https://download.csdn.net/download/qq_30155503/10305922
文件下载列表:
boostdesc_bgm.i boostdesc_bgm_bi.i boostdesc_bgm_hd.i boostdesc_binboost_064.i boostdesc_binboost_128.i
boostdesc_binboost_256.i boostdesc_lbgm.i ippicv_linux_20151201.tgz protobuf-cpp-3.1.0.tar.gz
vgg_generated_48.i vgg_generated_64.i vgg_generated_80.i vgg_generated_120.i
备注:缺少的文件可在opencv的官方github上opencv_3rdparty选择相应branch下载:
https://github.com/opencv/opencv_3rdparty/tree/contrib_xfeatures2d_boostdesc
4、编译
$ make
漫长编译路。。。。。。最终是编译成功的。
很遗憾告诉你,这只是3.2版本的结果。
3.4版本结果如下:
错误一:
In file included from /root/library/opencv/opencv-3.4.0/modules/stitching/include/opencv2/stitching.hpp:49:0,
from /root/library/opencv/opencv-3.4.0/modules/stitching/src/precomp.hpp:59,
from /root/library/opencv/opencv-3.4.0/build/modules/stitching/opencv_stitching_pch_dephelp.cxx:1:
/root/library/opencv/opencv-3.4.0/modules/stitching/include/opencv2/stitching/detail/matchers.hpp:52:42: fatal error: opencv2/xfeatures2d/cuda.hpp: 没有那个文件或目录
# include "opencv2/xfeatures2d/cuda.hpp"
^
compilation terminated.
[ 24%] Building CXX object 3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/field_mask.pb.cc.o
[ 24%] Linking CXX static library ../../lib/libopencv_test_face_pch_dephelp.a
[ 24%] Built target opencv_test_face_pch_dephelp
[ 24%] Generate files for Python bindings and documentation
make[2]: *** [modules/stitching/CMakeFiles/opencv_stitching_pch_dephelp.dir/opencv_stitching_pch_dephelp.cxx.o] 错误 1
make[1]: *** [modules/stitching/CMakeFiles/opencv_stitching_pch_dephelp.dir/all] 错误 2
make[1]: *** 正在等待未完成的任务....
没有找到opencv2/xfeatures2d/cuda.hpp???
或
错误二:
[ 84%] Built target pch_Generate_opencv_xfeatures2d
[ 84%] Building CXX object modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o
/root/library/opencv/opencv-3.4.0/opencv_contrib-3.4.0/modules/xfeatures2d/src/boostdesc.cpp:646:37: fatal error: boostdesc_bgm.i: 没有那个文件或目录
#include "boostdesc_bgm.i"
^
compilation terminated.
make[2]: *** [modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o] 错误 1
make[1]: *** [modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/all] 错误 2
make: *** [all] 错误 2
这种情况怎么解决?回想一下3.2版本,可能你会联想到在build目录下cmake下载文件失败。
opencv-3.4.0版本cmake的时候下载文件失败不会终止而是继续,让你有种错觉觉得一切正常。
但是,出来混,迟早都要还的。3.4在make的时候就出事了。
回去重新cmake一次,住上翻输出信息,你会发现诸多download failed如下:
-- xfeatures2d/boostdesc: Download: boostdesc_lbgm.i
CMake Warning at cmake/OpenCVDownload.cmake:190 (message):
xfeatures2d/boostdesc: Download failed: 1;"Unsupported protocol"
因此,还是要老老实实地按上面操作吧。
不过,3.4好像没有提示具体的文件目录喔。。。反正我选了3.2,3.4没试过
5、安装
$ make install
顺利!