g++ 编译cpp程序出现类似如下等问题。解决办法是使用opencv+opencv_contrib联合安装。
/usr/bin/ld: cannot find -lopencv_dnn
/usr/bin/ld: cannot find -lopencv_ml
/usr/bin/ld: cannot find -lopencv_shape
/usr/bin/ld: cannot find -lopencv_stitching
/usr/bin/ld: cannot find -lopencv_cudafeatures2d
/usr/bin/ld: cannot find -lopencv_superres
/usr/bin/ld: cannot find -lopencv_cudacodec
/usr/bin/ld: cannot find -lopencv_videostab
/usr/bin/ld: cannot find -lopencv_cudaoptflow
/usr/bin/ld: cannot find -lopencv_cudalegacy
/usr/bin/ld: cannot find -lopencv_calib3d
/usr/bin/ld: cannot find -lopencv_features2d
安装过程中遇到一些问题,找到一些解决办法,亲测可用。这里结合本人操作实际汇总一下。
我用的是opencv-4.4.0和opencv_contrib-4.4.0,大家查找文件时按照自己对应的目录就行了。建议将文件夹名称的-4.4.0去掉,便于各种版本通用移植。
编译OpenCV提示opencv_contrib缺少boostdesc_bgm.i等文件
解决方法:
查看 build 文件夹下的日志文件 CMakeDownloadLog.txt,在日志文件CMakeDownloadLog.txt中搜索 boostdesc_bgm.i 关键词。日志文件里就有它的下载地址,到指定位置下载即可。
如果找不到也没关系,我用的是:https://github.com/huangcaohui/OpenCV-xfeatures2d
在这里下载也一样。
下载zip文件4.4.0.zip,解压放到放在 opencv_contrib/modules/xfeatures2d/src/ 路径下即可。
sudo apt-get install libjaster-dev
提示:errorE: unable to locate libjasper-dev
后来google到解决办法,复制到这里
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
成功的解决了问题,其中libjasper1是libjasper-dev的依赖包
找不到相关文件
先安装
sudo apt install libgtk-3-dev
再建立软连接
sudo ln -s /usr/include/gtk-3.0 /home/beimingke/opencv/modules/highgui/include/gdk
/home/beimingke/opencv/opencv_contrib/modules/xfeatures2d/test/test_features2d.cpp:51:10: fatal error: features2d/test/test_detectors_regression.impl.hpp: 没有那个文件或目录
#include "features2d/test/test_detectors_regression.impl.hpp"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/build.make:134: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/test/test_features2d.cpp.o' failed
make[2]: *** [modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/test/test_features2d.cpp.o] Error 1
make[2]: *** 正在等待未完成的任务....
/home/beimingke/opencv/opencv_contrib/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp:8:10: fatal error: test_descriptors_invariance.impl.hpp: 没有那个文件或目录
#include "test_descriptors_invariance.impl.hpp" // main OpenCV repo
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/build.make:254: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/test/test_rotation_and_scale_invariance.cpp.o' failed
make[2]: *** [modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/test/test_rotation_and_scale_invariance.cpp.o] Error 1
CMakeFiles/Makefile2:6852: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/all' failed
make[1]: *** [modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
头文件include地址不对,解决方法如下:
将opencv/modules/features2d/test/文件下的
test_descriptors_invariance.impl.hpp
test_descriptors_regression.impl.hpp
test_detectors_invariance.impl.hpp
test_detectors_regression.impl.hpp
test_invariance_utils.hpp
拷贝到opencv_contrib/modules/xfeatures2d/test/文件下。
同时,将opencv_contrib/modules/xfeatures2d/test/test_features2d.cpp文件下的
#include "features2d/test/test_detectors_regression.impl.hpp"
#include "features2d/test/test_descriptors_regression.impl.hpp"
改成:
#include "test_detectors_regression.impl.hpp"
#include "test_descriptors_regression.impl.hpp"
将opencv_contrib-4.4.0/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp文件下的
#include "features2d/test/test_detectors_invariance.impl.hpp"
#include "features2d/test/test_descriptors_invariance.impl.hpp"
改成:
#include "test_detectors_invariance.impl.hpp"
#include "test_descriptors_invariance.impl.hpp"
参考https://blog.csdn.net/chujian_CSDN/article/details/106137508
解决办法:
sudo apt-get install libopencv-dev
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib/modules/ ..
可以多核提速,根据自己CPU的核心数
sudo make -j8
sudo gedit /etc/ld.so.conf
在文档中修改为
include /etc/ld.so.conf.d/*.conf
include /usr/local/lib
然后为了生效需要输入
sudo ldconfig
接着修改bash.bashrc文件
sudo gedit /etc/bash.bashrc
在bash.bashrc文件中增加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
最后在命令行输入
source /etc/bash.bashrc
成功后可以通过
pkg-config opencv --modversion
验证是否安装成功