ubuntu18.04 opencv4.4安装

本文详细介绍了在Ubuntu上从官方源下载OpenCV4压缩包,如何解压并配置编译参数,包括开启非免费模块、指定安装路径、生成pkg-config文件等。在编译过程中遇到的boostdesc文件缺失和features2d测试文件错误的问题,以及解决方案。最后,讲解了如何配置环境变量,更新动态链接库,并检验opencv版本。整个过程全面且实用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、官网下载压缩包

连接:https://opencv.org/releases/

2、解压到文件夹

mkdir build
cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 -D OPENCV_ENABLE_NONFREE=YES -D OPENCV_EXTRA_MODULES_PATH=/home/wyh/opencv-4.4.0/opencv_contrib-4.4.0/modules  ..
sudo make -j8
sudo make install

命令说明:

OpenCV4默认不生成.pc文件,OPENCV_GENERATE_PKGCONFIG=ON才会生成。该编译选项开启生成opencv4.pc文件,支持pkg-config功能。
可用-D CMAKE_INSTALL_PREFIX指定安装目录。如果指定安装目录则默认各部分分别安装在/usr/local/目录的include/ bin/ lib/3个文件夹下.
CMAKE_BUILD_TYPE=RELEASE:表示编译发布版本
CMAKE_INSTALL_PREFIX:表示生成动态库的安装路径,可以自定义
WITH_GTK=ON:这个配置是为了防止GTK配置失败:即安装了libgtk2.0-dev依赖,还是报错未安装
OPENCV_GENERATE_PKGCONFIG=NO:表示自动生成OpenCV的pkgconfig文件,否则需要自己手动生成。
OPENCV_ENABLE_NONFREE=YES,允许使用申请了专利的算法
1问题 /opencv_contrib/modules/xfeatures2d/src/boostdesc.cpp:673:20: fatal error: boostdesc_bgm.i: No such file or directory
解决:下载文件放到放在 opencv_contrib/modules/xfeatures2d/src/ 路径下即可。
总共缺了以下几个文件,都需要拷贝:

boostdesc_bgm.i
boostdesc_bgm_bi.i
boostdesc_bgm_hd.i
boostdesc_lbgm.i
boostdesc_binboost_064.i
boostdesc_binboost_128.i
boostdesc_binboost_256.i
vgg_generated_120.i
vgg_generated_64.i
vgg_generated_80.i
vgg_generated_48.i

2:make编译出错:fatal error: features2d/test/test_detectors_regression.impl.hpp: No such file or directory #include "features2d/test/test_detectors_regression.impl.hpp" ^
解决:将opencv / modules / features2d复制,然后粘贴到build目录中来解决该问题。
3 问题:home/wyh/opencv-4.4.0/opencv_contrib-4.4.0/modules/xfeatures2d/test/test_features2d.cpp:51:10: fatal error: features2d/test/test_detectors_regression.impl.hpp: No such file or directory #include "features2d/test/test_detectors_regression.impl.hpp"
解决:对比一下opencv-4.4.0/opencv_contrib-4.4.0/modules/xfeatures2d/test/ 和 opencv-4.4.0/modules/feature2d/test/ 文件夹,看看前者缺了哪些.hpp文件,然后把缺的那些都复制进去,应该是缺了以下五个。

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-4.4.0/opencv_contrib-4.4.0/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-4.4.0/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"

3、配置环境

用gedit打开/etc/ld.so.conf
在文件中加上一行 /usr/loacal/lib
其中/user/loacal是opencv安装路径也就是makefile中指定的安装路

sudo gedit /etc/ld.so.conf

在这里插入图片描述运行sudo ldconfig,
修改bash.bashrc文件

sudo gedit /etc/bash.bashrc 

在文件末尾加入命令:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv4/lib/pkgconfig
export PKG_CONFIG_PATH

在这里插入图片描述然后在命令行中输入

source /etc/bash.bashrc

4、检验

输入命令:

pkg-config opencv --modversion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值