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

转自我的博客(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);

的错误(我的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文件的路径,每个人都不尽一样,略去。

从报错以及网上的资料可以发现造成错误的原因主要是由于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")

以及第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.so;/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/lib/x86_64-linux-gnu/libopencv_imgproc.so;/usr/lib/x86_64-linux-gnu/libopencv_legacy.so;/usr/lib/x86_64-linux-gnu/libopencv_ml.so;/usr/lib/x86_64-linux-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/libopencv_video.so;/usr/lib/x86_64-linux-gnu/libopencv_videostab.so")

你可能感兴趣的:(ros,opencv,cv-bridge,研究)