1. 问题现象

1.1 编译过程警告

warning: lipopencv_improc.so.406, needed by /usr/local/lib/libopencv_xfeatures2d.so.4.6.0, 
may conflict with libopencv_imgproc.so.4.2

1.2 程序运行报错

terminate called after throwing an instance of 'cv::Exception'
error:(-4:Insufficient memory) Failed to locate 142099092348209 bytes in function 'OutOfMemoryError'
REQUIRED process has died!

2. 分析问题原因

根据编译过程提示是opencv版本冲突问题,仅以警告形式出现,程序可以编译通过并运行,但是运行过程中报错。
使用命令$ ldd <exe文件路径> | grep opencv查看库链接关系:

libopencv_core.so.406 => /usr/local/lib/libopencv_core.so.406 (0x00007febe47fc000)
libopencv_xfeatures2d.so.406 => /usr/local/lib/libopencv_xfeatures2d.so.406 (0x00007febe3f25000)
libopencv_imgcodecs.so.406 => /usr/local/lib/libopencv_imgcodecs.so.406 (0x00007febe25f0000)
libopencv_imgproc.so.406 => /usr/local/lib/libopencv_imgproc.so.406 (0x00007febe0a6d000)
libopencv_features2d.so.406 => /usr/local/lib/libopencv_features2d.so.406 (0x00007febded2f000)
libopencv_flann.so.406 => /usr/local/lib/libopencv_flann.so.406 (0x00007febd8b7a000)
libopencv_core.so.4.2 => /usr/local/lib/libopencv_core.so.4.2 (0x00007febe47fc000)
libopencv_xfeatures2d.so.4.2 => /usr/local/lib/libopencv_xfeatures2d.so.4.2 (0x00007febe3f25000)
libopencv_imgcodecs.so.4.2 => /usr/local/lib/libopencv_imgcodecs.so.4.2 (0x00007febe25f0000)
libopencv_imgproc.so.4.2 => /usr/local/lib/libopencv_imgproc.so.4.2 (0x00007febe0a6d000)
libopencv_features2d.so.4.2 => /usr/local/lib/libopencv_features2d.so.4.2 (0x00007febded2f000)
libopencv_flann.so.4.2 => /usr/local/lib/libopencv_flann.so.4.2 (0x00007febd8b7a000)

看到每个opencv动态链接库被链接到了两个版本,分析原因为ROS Noetic安装中会自带OpenCV 4.2库和cv_bridge,而我自己安装使用了OpenCV 4.6和OpenCV_contrib 4.x,两者造成了冲突,cv_bridge的冲突通过修改cmake文件更改所使用的OpenCV库路径可以解决,具体参考以下方法:
REF:https://blog.csdn.net/qq_34213260/article/details/123761580

3. 解决方法

在工程的CMakeList.txt中指定工程所使用的OpenCV库的具体版本,可以通过设置精确库路径

set(OpenCV_DIR "<OpenCVConfig.cmake文件的绝对路径>")
GitHub 加速计划 / opencv31 / opencv
77.38 K
55.71 K
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:2 个月前 )
c3747a68 Added Universal Windows Package build to CI. 11 天前
9b635da5 - 11 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐