简体   繁体   English

ROS问题:如何用opencv4正确编译自定义cv_bridge

[英]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.我想在我的 ROS 程序中使用 opencv-4.4.0,我发现要做到这一点,我必须使用当前 opencv 版本从源代码编译 cv_bridge,因为从 ROS 发布的 cv_bridge 仅支持 opencv3。 After some searching I found this which I think is customized to compatible with opencv4:经过一番搜索,我发现这个我认为是定制的与 opencv4 兼容:

https://github.com/fizyr-forks/vision_opencv/tree/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);此错误来自cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO8); cv::imshow() ); It doesn't really matter because I found after changing the last argument MONO8 to BGR8 , imshow was able to work.这并不重要,因为我发现在将最后一个参数MONO8更改为BGR8imshow能够工作。 So I think I can use BGR8 then change it to grey scale using opencv function later.所以我想我可以使用BGR8 ,然后使用 opencv function 将其更改为灰度。 However from this error message I realized cv_bridge is still trying to use opencv-3.2 instead of opencv-4.4.0.但是,从这个错误消息中,我意识到 cv_bridge 仍在尝试使用 opencv-3.2 而不是 opencv-4.4.0。 Please help me figure it out how to compile it with correct opencv version!请帮我弄清楚如何用正确的 opencv 版本编译它! Here's my CMakeLists.txt and package.xml .这是我的CMakeLists.txtpackage.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: 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.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.我插入一行代码打印出 cv_bridge.cpp 中的 opencv 版本。 And the result is 4.4.0.结果是4.4.0。 Does this mean it's using the correct version of opencv and the upper error message is from another package?这是否意味着它使用的是正确版本的 opencv 并且上面的错误消息来自另一个 package? I checked both cv_bridge and my own program that are both using opencv 4.4.0 as I included.我检查了 cv_bridge 和我自己的程序,它们都使用 opencv 4.4.0,包括在内。 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) .根据这篇文章,我发现我的自定义 cv_bridge 的 CMakeLists.txt 没有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:所以我添加了它并catkin build了catkin,然后尝试了我的程序并修复了所有问题:这是我的整个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}
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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