ROS indigo 的cv_bridge默认的opencv版本为opencv2.4.8,但项目里的opencv版本要求opencv3,如果使用默认设置,则报错:
Bad argument (Invalid pointer to file storage) in cvGetFileNodeByName
实际上FileStorage的读入路径没有问题,结果表现为不能读入文件。
更改cv_bridge的.cmake文件:
sudo gedit /opt/ros/indigo/share/cv_bridge/cmake/cv_bridgeConfig.cmake
注释掉默认的opencv头文件路径,改为自己的opencv3的头文件路径
#change opencv version to 3.4.1
#if(NOT "include;/usr/include/opencv;/usr/include " STREQUAL " ")
if(NOT "include;/opencv3.4.1/usr/include/opencv2;/opencv3.4.1/usr/include " STREQUAL " ")
set(cv_bridge_INCLUDE_DIRS "")
#change opencv version to 3.4.1
#set(_include_dirs "include;/usr/include/opencv;/usr/include")
set(_include_dirs "include;/opencv3.4.1/usr/include/opencv2;/opencv3.4.1/usr/include")
注释掉默认的opencv库文件路径,改为自己需要的opencv3的库文件路径
#change opencv version to 3.4.1
#set(libraries "cv_bridge;/usr/lib/x86_64-linux-gnu/libopencv_videostab.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_video.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_superres.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_stitching.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_photo.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_ocl.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_objdetect.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_ml.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_legacy.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_gpu.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_flann.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_features2d.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_contrib.so.2.4.8;/usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.2.4.8")
set(libraries "cv_bridge;/opencv3.4.1/usr/lib/libopencv_videostab.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_video.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_superres.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_stitching.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_photo.so.3.4.1;/usr/lib/x86_64-linux-gnu/libopencv_ocl.so.2.4.8;/opencv3.4.1/usr/lib/libopencv_objdetect.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_ml.so.3.4.1;/usr/lib/x86_64-linux-gnu/libopencv_legacy.so.2.4.8;/opencv3.4.1/usr/lib/libopencv_imgproc.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_highgui.so.3.4.1;/usr/lib/x86_64-linux-gnu/libopencv_gpu.so.2.4.8;/opencv3.4.1/usr/lib/libopencv_flann.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_features2d.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_core.so.3.4.1;/usr/lib/x86_64-linux-gnu/libopencv_contrib.so.2.4.8;/opencv3.4.1/usr/lib/libopencv_calib3d.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_aruco.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_bgsegm.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_bioinspired.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_ccalib.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_datasets.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_dpm.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_face.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_freetype.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_fuzzy.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_hdf.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_hfs.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_imgcodecs.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_img_hash.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_line_descriptor.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_objdetect.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_optflow.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_phase_unwrapping.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_plot.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_reg.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_rgbd.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_saliency.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_sfm.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_shape.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_stereo.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_structured_light.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_surface_matching.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_tracking.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_viz.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_xfeatures2d.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_ximgproc.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_xobjdetect.so.3.4.1;/opencv3.4.1/usr/lib/libopencv_xphoto.so.3.4.1")
保存,重新catkin_make,问题解决。
如果需要切换回来,再恢复即可。
镜像问题:
https://blog.csdn.net/m0_37811342/article/details/79971993
以上操作只解决了FileStorage的读取问题,但依然在cv_bridge::toCvCopy函数上莫名报错,发现这种方式还是不能从根本上解决问题,回想起编译时的警告:
/usr/bin/ld: warning: libopencv_highgui.so.2.4, needed by /opt/ros/jade/lib/libcv_bridge.so, may conflict with libopencv_highgui.so.3.2
解决问题的根本方法是另外安装一个版本的cv_bridge,即kenetic版本,编译后发现,kinetic版本的cv_bridge与indigo版本的其他package是兼容的(暂时没出现什么异常)
注意:保留indigo版本的cv_bridge,另外安装新的kinetic版本:
选择kinetic分支:
git clone [email protected]:ros-perception/vision_opencv.git
mkdir build
cd build
cmake ../cv_bridge
make
sudo make install
在使用到cv_bridge的package的CMakeLists.txt中,在find_package(cv_bridge)之前添加:
set(cv_bridge_DIR /usr/local/share/cv_bridge/cmake)
如:
set(cv_bridge_DIR /usr/local/share/cv_bridge/cmake)
find_package(catkin REQUIRED COMPONENTS
OpenCV
roscpp
std_msgs
sensor_msgs
cv_bridge
camera_model
)
添加库路径到环境变量.bashrc
export LD_LIBRARY_PATH=/usr/local/lib:LD_LIBRARY_PATH
PS:在indigo中默认find_package 时寻找自己安装的opencv3.4.1
#默认使用opencv3
export PKG_CONFIG_PATH=/opencv3.4.1/usr/lib/pkgconfig:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=/opencv3.4.1/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH