简体   繁体   中英

ROS question: How to compile custom cv_bridge with opencv4 correctly

I want to use opencv-4.4.0 in my ROS program, and I found that to do this I have to compile cv_bridge from source with current opencv version, since cv_bridge shipped from ROS only support opencv3. After some searching I found this which I think is customized to compatible with opencv4:

https://github.com/fizyr-forks/vision_opencv/tree/opencv4

after catkin build and changing include path in my program, this error occurred:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/imgproc/src/color.cpp, line 9716
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/imgproc/src/color.cpp:9716: error: (-215) scn == 3 || scn == 4 in function cvtColor  

This error is from cv::imshow() after cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO8); It doesn't really matter because I found after changing the last argument MONO8 to BGR8 , imshow was able to work. So I think I can use BGR8 then change it to grey scale using opencv function later. However from this error message I realized cv_bridge is still trying to use opencv-3.2 instead of opencv-4.4.0. Please help me figure it out how to compile it with correct opencv version! Here's my CMakeLists.txt and package.xml . I changed them a little as original files didn't work for me properly(so as mine).
I'm sorry if they are hard to read.
CMakeLists.txt:

cmake_minimum_required(VERSION 2.8) project(cv_bridge)

find_package(catkin REQUIRED COMPONENTS rosconsole sensor_msgs)
 
if(NOT ANDROID)   find_package(PythonLibs)  
if(PYTHONLIBS_VERSION_STRING VERSION_LESS 3)
     find_package(Boost REQUIRED python)   else()
     find_package(Boost REQUIRED python3)   endif() else() find_package(Boost REQUIRED) endif()
 
find_package(OpenCV 4.4.0 REQUIRED   COMPONENTS
     opencv_core
     opencv_imgproc
     opencv_imgcodecs   CONFIG ) set(OpenCV_DIR=/usr/local/lib/cmake/opencv4)
 
catkin_package(   INCLUDE_DIRS include   LIBRARIES ${PROJECT_NAME}  
CATKIN_DEPENDS rosconsole sensor_msgs   DEPENDS OpenCV   CFG_EXTRAS
cv_bridge-extras.cmake )

catkin_python_setup()

include_directories(include ${Boost_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS})

if(NOT ANDROID) add_subdirectory(python) endif() add_subdirectory(src)
if(CATKIN_ENABLE_TESTING)   add_subdirectory(test) endif()

install(   DIRECTORY include/${PROJECT_NAME}/   DESTINATION
${CATKIN_PACKAGE_INCLUDE_DESTINATION} )  

package.xml:

<package format="2">   <name>cv_bridge</name>  
<version>1.13.0</version>   <description>
    This contains CvBridge, which converts between ROS
    Image messages and OpenCV images.   </description>   <author>Patrick Mihelich</author>   <author>James Bowman</author>  
<maintainer email="vincent.rabaud@gmail.com">Vincent
Rabaud</maintainer>   <license>BSD</license>   <url
type="website">http://www.ros.org/wiki/cv_bridge</url>   <url
type="repository">https://github.com/ros-perception/vision_opencv</url>
<urlI'm sorry if it's hard to read.
type="bugtracker">https://github.com/ros-perception/vision_opencv/issues</url>

  <export>
    <rosdoc config="rosdoc.yaml" />   </export>

  <buildtool_depend>catkin</buildtool_depend>

<build_depend>boost</build_depend>  
<build_depend>opencv2</build_depend>  
<build_depend>python</build_depend>  
<build_depend>python-opencv</build_depend>  
<build_depend>rosconsole</build_depend>  
<build_depend>sensor_msgs</build_depend>

  <exec_depend>boost</exec_depend>  
<exec_depend>opencv2</exec_depend>   <exec_depend>python</exec_depend>
<exec_depend>python-opencv</exec_depend>  
<exec_depend>rosconsole</exec_depend>  
<build_export_depend>opencv2</build_export_depend>  
<build_export_depend>sensor_msgs</build_export_depend>

  <test_depend>rostest</test_depend>  
<test_depend>python-numpy</test_depend>

  <doc_depend>dvipng</doc_depend> </package>  

update:

I insert a line of code to print out opencv version in cv_bridge.cpp. And the result is 4.4.0. Does this mean it's using the correct version of opencv and the upper error message is from another package? I checked both cv_bridge and my own program that are both using opencv 4.4.0 as I included. I really run out of idea. Here is what I included:

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include </home/robotics/catkin_ws/src/vision_opencv-opencv4/cv_bridge/include/cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv4/opencv2/imgproc/imgproc.hpp>
#include <opencv4/opencv2/highgui/highgui.hpp>
#include <iostream>

I'm answering my own question cause I finally found a solution! According to this post ,I found that my CMakeLists.txt of custmized cv_bridge didn't have set (CMAKE_CXX_STANDARD 11) . So I added it and catkin build ,then try my program and every thing fixed: Here's my whole CMakelists:

cmake_minimum_required(VERSION 2.8)
set (CMAKE_CXX_STANDARD 11)
project(cv_bridge)

find_package(catkin REQUIRED COMPONENTS rosconsole sensor_msgs)

if(NOT ANDROID)
  find_package(PythonLibs)
  if(PYTHONLIBS_VERSION_STRING VERSION_LESS 3)
    find_package(Boost REQUIRED python)
  else()
    find_package(Boost REQUIRED python3)
  endif()
else()
find_package(Boost REQUIRED)
endif()

set(CV_MAJOR_VERSION=4.4.0)
set(OpenCV_DIR=/usr/local/lib/cmake/opencv4)
find_package(OpenCV 4.4.0 REQUIRED
  COMPONENTS
    core
    imgproc
    imgcodecs
  CONFIG
)


catkin_package(
  INCLUDE_DIRS include
  LIBRARIES ${PROJECT_NAME}
  CATKIN_DEPENDS rosconsole sensor_msgs
  DEPENDS OpenCV
  CFG_EXTRAS cv_bridge-extras.cmake
)

catkin_python_setup()

include_directories(include ${Boost_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS})

if(NOT ANDROID)
add_subdirectory(python)
endif()
add_subdirectory(src)
if(CATKIN_ENABLE_TESTING)
  add_subdirectory(test)
endif()

# install the include folder
install(
  DIRECTORY include/${PROJECT_NAME}/
  DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM