ubuntu安装opencv_contrib扩展库,附踩坑+测试

举报
振华OPPO 发表于 2022/09/15 20:45:57 2022/09/15
【摘要】 博主昨晚需要用到OpenCV的SURF接口,但是发现无法调用,因为没有头文件。于是查阅了下资料,发现这些库已经被美国买下专利,成为付费库,都在opencv_contrib中。如果你已经安装了OpenCV,或者还没有安装OpenCV,都可以跟随本教程安装好opencv_contrib。 1、安装OpenCV如果还没有安装过OpenCV,可以先看下:Ubuntu安装OpenCV3.4.5(两种方...

博主昨晚需要用到OpenCV的SURF接口,但是发现无法调用,因为没有头文件。于是查阅了下资料,发现这些库已经被美国买下专利,成为付费库,都在opencv_contrib中。如果你已经安装了OpenCV,或者还没有安装OpenCV,都可以跟随本教程安装好opencv_contrib。

1、安装OpenCV

如果还没有安装过OpenCV,可以先看下:Ubuntu安装OpenCV3.4.5(两种方法&&图文详解),安装好之后可以按照下面步骤进行。

在这里插入图片描述

2、下载opencv_contrib

下载opencv_contrib,在Tags里找到自己对应的版本进行下载。然后将opencv_contrib解压到opencv目录下。

在这里插入图片描述

3、cmake分析

启动终端,cd到build目录,输入下面命令,Path后是你的modules的目录。

cmake -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_EXTRA_MODULES_PATH=~/Software/opencv-3.4.5/opencv_contrib-3.4.5/modules ..

应该会出现下面的未安装的问题:

-- xfeatures2d/vgg: Download: vgg_generated_64.i
CMake Warning at cmake/OpenCVDownload.cmake:193 (message):
  xfeatures2d/vgg: Download failed: 28;"Timeout was reached"

  For details please refer to the download log file:

  /home/dzh/Software/opencv-3.4.5/build/CMakeDownloadLog.txt

Call Stack (most recent call first):
  opencv_contrib-3.4.5/modules/xfeatures2d/cmake/download_vgg.cmake:16 (ocv_download)
  opencv_contrib-3.4.5/modules/xfeatures2d/CMakeLists.txt:9 (download_vgg_descriptors)


-- xfeatures2d/vgg: Download: vgg_generated_80.i
-- 
=======================================================================
  Couldn't connect to server from the Internet.
  Perhaps direct connections are not allowed in the current network.
  To use proxy please check/specify these environment variables:
  - http_proxy/https_proxy
  - and/or HTTP_PROXY/HTTPS_PROXY
=======================================================================

CMake Warning at cmake/OpenCVDownload.cmake:193 (message):
  xfeatures2d/vgg: Download failed: 7;"Couldn't connect to server"

  For details please refer to the download log file:

  /home/dzh/Software/opencv-3.4.5/build/CMakeDownloadLog.txt

Call Stack (most recent call first):
  opencv_contrib-3.4.5/modules/xfeatures2d/cmake/download_vgg.cmake:16 (ocv_download)
  opencv_contrib-3.4.5/modules/xfeatures2d/CMakeLists.txt:9 (download_vgg_descriptors)


-- data: Download: face_landmark_model.dat
-- 
=======================================================================
  Couldn't connect to server from the Internet.
  Perhaps direct connections are not allowed in the current network.
  To use proxy please check/specify these environment variables:
  - http_proxy/https_proxy
  - and/or HTTP_PROXY/HTTPS_PROXY
=======================================================================

CMake Warning at cmake/OpenCVDownload.cmake:193 (message):
  data: Download failed: 7;"Couldn't connect to server"

  For details please refer to the download log file:

  /home/dzh/Software/opencv-3.4.5/build/CMakeDownloadLog.txt

Call Stack (most recent call first):
  opencv_contrib-3.4.5/modules/face/CMakeLists.txt:13 (ocv_download)


CMake Warning at opencv_contrib-3.4.5/modules/face/CMakeLists.txt:26 (message):
  Face: Can't get model file for face alignment.


-- No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
-- Found installed version of gflags: /usr/lib/x86_64-linux-gnu/cmake/gflags
-- Detected gflags version: 2.2.1
-- Found installed version of Eigen: /usr/local/lib/cmake/eigen3
-- Found required Ceres dependency: Eigen version 3.2.92 in /usr/local/include/eigen3
-- Found required Ceres dependency: glog
-- Found installed version of gflags: /usr/lib/x86_64-linux-gnu/cmake/gflags
-- Detected gflags version: 2.2.1
-- Found required Ceres dependency: gflags
-- Found Ceres version: 1.14.0 installed in: /usr/local with components: [EigenSparse, SparseLinearAlgebraLibrary, LAPACK, SuiteSparse, CXSparse, SchurSpecializations, OpenMP, Multithreading]
-- Checking SFM deps... TRUE

其实是因为网络连接问题,无法连接到下载地址,所以下载失败。网上的所有教程总结下来就是:1、更换热点,多连几次;2、从网上下载好文件到本地;3、修改下载地址,

这里我采用科学上网,依次到github上下载文件到本地,将下载好的boostdesc_bgm.i系列和vgg_generated_48.i系列放到opencv-3.4.5/opencv_contrib-3.4.5/modules/xfeatures2d/src/目录下。将下载好的face_landmark_model.dat放到opencv-3.4.5/opencv_contrib-3.4.5/modules/face/src/

在这里插入图片描述
再次输入cmake命令进行分析,还会报上面文件的错误,但是不需要管它,因为需要的文件我们已经有了。

4、make编译

输入sudo make -j4进行编译,-j4表示采用4线程运行。

Error 1
In file included from /home/dzh/Software/opencv-3.4.5/modules/stitching/include/opencv2/stitching.hpp:49:0,
                 from /home/dzh/Software/opencv-3.4.5/modules/stitching/src/precomp.hpp:59,
                 from /home/dzh/Software/opencv-3.4.5/build/modules/stitching/opencv_stitching_pch_dephelp.cxx:1:
/home/dzh/Software/opencv-3.4.5/modules/stitching/include/opencv2/stitching/detail/matchers.hpp:52:12: fatal error: opencv2/xfeatures2d/cuda.hpp: 没有那个文件或目录
 #  include "opencv2/xfeatures2d/cuda.hpp"
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
modules/stitching/CMakeFiles/opencv_stitching_pch_dephelp.dir/build.make:62: recipe for target 'modules/stitching/CMakeFiles/opencv_stitching_pch_dephelp.dir/opencv_stitching_pch_dephelp.cxx.o' failed
make[2]: *** [modules/stitching/CMakeFiles/opencv_stitching_pch_dephelp.dir/opencv_stitching_pch_dephelp.cxx.o] Error 1
CMakeFiles/Makefile2:15804: recipe for target 'modules/stitching/CMakeFiles/opencv_stitching_pch_dephelp.dir/all' failed
make[1]: *** [modules/stitching/CMakeFiles/opencv_stitching_pch_dephelp.dir/all] Error 2
make[1]: *** 正在等待未完成的任务....

相信一定会出现上面的ERROR,原因是include的cuda.hpp头文件路径找不到,需要改为绝对路径。打开它提示的文件matchers.hpp,然后找到你的cuda.hpp目录,如下所示。

在这里插入图片描述复制你的路径,然后替换掉include "opencv2/xfeatures2d/cuda.hpp",比如我的路径:/home/dzh/Software/opencv-3.4.5/opencv_contrib-3.4.5/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.hpp

Error 2
In file included from /home/dzh/Software/opencv-3.4.5/build/modules/stitching/opencv_stitching_pch_dephelp.cxx:1:0:
/home/dzh/Software/opencv-3.4.5/modules/stitching/src/precomp.hpp:91:12: fatal error: opencv2/xfeatures2d/cuda.hpp: 没有那个文件或目录
 #  include "opencv2/xfeatures2d/cuda.hpp"
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

有了Error1的经验,根据报错信息找到错误文件的地方,将路径改为上面的绝对路径。

Error 3
[ 90%] Building CXX object modules/stitching/CMakeFiles/opencv_stitching.dir/src/matchers.cpp.o
/home/dzh/Software/opencv-3.4.5/modules/stitching/src/matchers.cpp:52:10: fatal error: opencv2/xfeatures2d.hpp: 没有那个文件或目录
 #include "opencv2/xfeatures2d.hpp"
          ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
modules/stitching/CMakeFiles/opencv_stitching.dir/build.make:164: recipe for target 'modules/stitching/CMakeFiles/opencv_stitching.dir/src/matchers.cpp.o' failed
make[2]: *** [modules/stitching/CMakeFiles/opencv_stitching.dir/src/matchers.cpp.o] Error 1
CMakeFiles/Makefile2:15854: recipe for target 'modules/stitching/CMakeFiles/opencv_stitching.dir/all' failed
make[1]: *** [modules/stitching/CMakeFiles/opencv_stitching.dir/all] Error 2
make[1]: *** 正在等待未完成的任务....
[ 90%] Building CXX object modules/sfm/CMakeFiles/opencv_sfm.dir/src/simple_pipeline.cpp.o
/home/dzh/Software/opencv-3.4.5/opencv_contrib-3.4.5/modules/sfm/src/simple_pipeline.cpp:44:10: fatal error: opencv2/xfeatures2d.hpp: 没有那个文件或目录
 #include <opencv2/xfeatures2d.hpp>
          ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
modules/sfm/CMakeFiles/opencv_sfm.dir/build.make:230: recipe for target 'modules/sfm/CMakeFiles/opencv_sfm.dir/src/simple_pipeline.cpp.o' failed
make[2]: *** [modules/sfm/CMakeFiles/opencv_sfm.dir/src/simple_pipeline.cpp.o] Error 1
make[2]: *** 正在等待未完成的任务....

报错显示找不到xfeatures2d.hpp,同理,还是找到报错的地方,然后将路径改为绝对路径:

/home/dzh/Software/opencv-3.4.5/opencv_contrib-3.4.5/modules/xfeatures2d/include/opencv2/xfeatures2d.hpp
Success

经历了种种错误,最终make成功!正应了古话:世上无难事,只怕有心人。

在这里插入图片描述

5、make install安装

安装的过程是非常快的,大概15s就安装好了。

sudo make install

在这里插入图片描述

6、测试安装

#include <QApplication>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/xfeatures2d/nonfree.hpp"
#include "opencv2/xfeatures2d.hpp"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    cv::Mat image = cv::imread("/home/dzh/QtOpenCV/image.png", 0); //以灰度图像读取
    vector<cv::KeyPoint> keypoints;//特征点数组
//    cv::FAST(image, keypoints, 40, true); //Fast特征点
//    cv::drawKeypoints(image, keypoints, image, cv::Scalar(255 ,255, 255), cv::DrawMatchesFlags::DRAW_OVER_OUTIMG);
    cv::Ptr<cv::xfeatures2d::SURF> detector = cv::xfeatures2d::SURF::create(2500.);//初始化检测器对象
    detector->detect(image, keypoints);//检测SURF特征点
    cv::drawKeypoints(image, keypoints, image, cv::Scalar::all(-1), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);//绘制特征点
    cv::imshow("image", image);
    cv::waitKey(0);
    return a.exec();
}

在这里插入图片描述

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。