欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于ros将opencv版本固定“写死”的一些想法

程序员文章站 2022-07-13 22:25:04
...

今天主要工作是将ros和zed结合起来,但是发现自己安装了opencv3.1,ros indigo安装的是opencv2.4.8,这就麻烦了,zed支持的是opencv3.1。一开始使用slam2时,报错:

编译出现错误,
CMakeFiles/Stereo.dir/src/ros_stereo.cc.o: In function `String':
/usr/local/include/opencv2/core/cvstd.hpp:625: undefined reference to `cv::String::allocate(unsigned long)'
CMakeFiles/Stereo.dir/src/ros_stereo.cc.o: In function `main':
/home/car/Desktop/ORB_SLAM2/Examples/ROS/ORB_SLAM2/src/ros_stereo.cc:86: undefined reference to `cv::FileStorage::FileStorage(cv::String const&, int, cv::String const&)'
CMakeFiles/Stereo.dir/src/ros_stereo.cc.o: In function `main':
/usr/local/include/opencv2/core/cvstd.hpp:667: undefined reference to `cv::String::deallocate()'
CMakeFiles/Stereo.dir/src/ros_stereo.cc.o: In function `~String':
/usr/local/include/opencv2/core/cvstd.hpp:667: undefined reference to `cv::String::deallocate()'
CMakeFiles/Stereo.dir/src/ros_stereo.cc.o: In function `cv::FileNode::operator int() const':
/usr/local/include/opencv2/core/persistence.hpp:1180: undefined reference to `cv::read(cv::FileNode const&, int&, int)'
/usr/local/include/opencv2/core/persistence.hpp:1180: undefined reference to `cv::read(cv::FileNode const&, int&, int)'
/usr/local/include/opencv2/core/persistence.hpp:1180: undefined reference to `cv::read(cv::FileNode const&, int&, int)'
/usr/local/include/opencv2/core/persistence.hpp:1180: undefined reference to `cv::read(cv::FileNode const&, int&, int)'
CMakeFiles/Stereo.dir/src/ros_stereo.cc.o: In function `main':
/usr/local/include/opencv2/core/cvstd.hpp:667: undefined reference to `cv::String::deallocate()'
/usr/local/include/opencv2/core/cvstd.hpp:667: undefined reference to `cv::String::deallocate()'

 

之后:

我将CMakeLists.txt 里面的
find_package(OpenCV 2.4.3 REQUIRED)
改成
find_package(OpenCV 3.1.0 REQUIRED COMPONENTS core highgui imgproc imgcodecs)


好像就可以了,但是,运行出错了
OpenCV Error: Bad argument (Invalid pointer to file storage) in cvGetFileNodeByName, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/persistence.cpp, line 740
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/persistence.cpp:740: error: (-5) Invalid pointer to file storage in function cvGetFileNodeByName


OpenCV Error: Bad argument in cvGetFileNodeByName,src/persistence.cpp,  throwing 'cv::Exception'
  what():  /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/persistence.cpp:740: error: (-5) Invalid pointer to file storage in function cvGetFileNodeByName

 

 

还是出错,改的心慌慌呀。

 

 

网上有个解决方案,转自http://m.blog.csdn.net/article/details?id=52842099

ROS-Kinect cv_bridge的opencv版本兼容问题

发表于2016/10/17 21:32:15  326人阅读

分类: 研究

转自我的博客(http://gongzheng92.net)


今天在编译自己的drone_sensor模块的时候(含stereo camera)出现了

/home/joey/pfl/ros/src/drone_sensor/src/stereo_camera/CameraThread.cpp: In member function ‘virtual void CameraThread::loop()’: /home/joey/pfl/ros/src/drone_sensor/src/stereo_camera/CameraThread.cpp:65:47: error: ‘cvDeInterlace’ was not declared in this scope
         cvDeInterlace(img, evenImage, oddImage);
                                               ^
/home/joey/pfl/ros/src/drone_sensor/src/stereo_camera/CameraThread.cpp: In member function ‘void CameraThread::publishImage(image_transport::CameraPublisher&, IplImage*)’: /home/joey/pfl/ros/src/drone_sensor/src/stereo_camera/CameraThread.cpp:85:32: error: no matching function for call to ‘cv::Mat::Mat(IplImage*&)’
     cv::Mat image = cv::Mat(img);
  • 1
  • 2
  • 3
  • 4
  • 5

的错误(我的catkin_ws即为~/pfl/ros

我的配置为:

  • Ubuntu 16.04 虚拟机
  • ROS版本为Kinect,路径为/opt/ros/kinect

通过这条问题里中提到的命令查询opencv的路径与版本还有依赖库:

    pkg-config --modversion opencv
    2.4.9.1

    pkg-config --cflags opencv
    -I/usr/include/opencv

    pkg-config --libs opencv
    #这里有好多.so文件的路径,每个人都不尽一样,略去。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

从报错以及网上的资料可以发现造成错误的原因主要是由于ros在Kinect的版本中自带了opencv3(通过ros-kinect-opencv3安装),在目录/opt/ros/kinetic/include/opencv-3.1.0-dev下。cv3与cv2的不兼容造成了错误。具体造成本错误的原因是cvDeInterlace这个函数存在于cvaux.h所引用的opencv2/legacy/legacy.hpp文件中,而在cv3中cvaux并没有引用这个文件(那么在cv3中对应的函数应该如何调用?

这个问题中题主遇到了非常相似的情况,然而给出的解决方式:

将cmakelist中
find_package(OpenCV REQUIRED)
改为
find_package(OpenCV 2.4.8 EXACT REQUIRED)
(其中2.4.8为具体版本号,我的情况为2.4.9.1)

并没有用,make还是会去调用ros的cv3.

继续在网上翻找之后发现时cv_bridge的问题。ros kinect的cv_bridge会自动关联cv3。具体在/opt/ros/kinetic/share/cv_bridge/cmake中的cv_bridgeConfig.cmake里可以看到:

94-96行:
    if(NOT "include;/opt/ros/kinetic/include/opencv-3.1.0-dev/opencv;/opt/ros/kinetic/include/opencv-3.1.0-dev " STREQUAL " ")
      set(cv_bridge_INCLUDE_DIRS "")
      set(_include_dirs "include;/opt/ros/kinetic/include/opencv-3.1.0-dev/opencv;/opt/ros/kinetic/include/opencv-3.1.0-dev")
  • 1
  • 2
  • 3
  • 4

以及第112行的所有lib都是cv3的。

因此将所有的这些opencv的路径都改为系统的opencv路径即可解决问题。同时上述的find_package中的信息并没有必要修改。
附上我的修改方案:

#94-96行:
if(NOT "include;/usr/include/opencv;/usr/include " STREQUAL " ")
  set(cv_bridge_INCLUDE_DIRS "")
  set(_include_dirs "include;/usr/include/opencv;/usr/include")
#112行:
set(libraries "cv_bridge;/usr/lib/x86_64-linux-gnu/libopencv_calib3d.s
o;/usr/lib/x86_64-linux-gnu/libopencv_contrib.so;/usr/lib/x86_64-linux-
gnu/libopencv_core.so;/usr/lib/x86_64-linux-gnu/libopencv_features2d.so
;/usr/lib/x86_64-linux-gnu/libopencv_flann.so;/usr/lib/x86_64-linux-gnu
/libopencv_gpu.so;/usr/lib/x86_64-linux-gnu/libopencv_highgui.so;/usr/li
b/x86_64-linux-gnu/libopencv_imgproc.so;/usr/lib/x86_64-linux-gnu/libope
ncv_legacy.so;/usr/lib/x86_64-linux-gnu/libopencv_ml.so;/usr/lib/x86_64-l
inux-gnu/libopencv_objdetect.so;/usr/lib/x86_64-linux-gnu/libopencv_ocl.so
;/usr/lib/x86_64-linux-gnu/libopencv_photo.so;/usr/lib/x86_64-linux-gnu/
libopencv_stitching.so;/usr/lib/x86_64-linux-gnu/libopencv_superres.so;/
usr/lib/x86_64-linux-gnu/libopencv_ts.so;/usr/lib/x86_64-linux-gnu/lib
opencv_video.so;/usr/lib/x86_64-linux-gnu/libopencv_videostab.so")

有了些启发,但是还是没做出来