ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Converting CvImage to a ROS image not working on Ubuntu 18.04 - Melodic

asked 2019-07-16 17:27:47 -0500

RayROS gravatar image

updated 2019-07-17 13:56:38 -0500

Hello, I am trying to convert a CvImage to a ROS image. I followed the tutorial present on this source but I don't understand the error that is keeping me from compiling and running the example. Below the code I am using from the same tutorial:

UPDATE CMake Code:

cmake_minimum_required(VERSION 2.8.3)
project(map_ros)

add_compile_options(-std=c++11)
find_package(octomap REQUIRED)

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})

find_package(catkin REQUIRED COMPONENTS
    roscpp
    sensor_msgs
    std_msgs
    message_generation
    pcl_ros
    pcl_conversions
    geometry_msgs
    nav_msgs
    grid_map_core
    grid_map_ros
    grid_map_cv
    grid_map_filters
    grid_map_loader
    grid_map_msgs
    grid_map_octomap
    grid_map_rviz_plugin
    grid_map_visualization
    cv_bridge
    octomap_msgs
    filters
    image_transport
    )

catkin_package(
  INCLUDE_DIRS include
  LIBRARIES ${PROJECT_NAME}
  CATKIN_DEPENDS
      roscpp
      sensor_msgs
      std_msgs
      message_generation
      pcl_ros
      pcl_conversions
      geometry_msgs
      nav_msgs
      grid_map_core
      grid_map_ros
      grid_map_cv
      grid_map_filters
      grid_map_loader
      grid_map_msgs
      grid_map_octomap
      grid_map_rviz_plugin
      grid_map_visualization
      cv_bridge
      octomap_msgs
      filters
      DEPENDS EIGEN3
)

###########
## Build ##
###########

include_directories(${catkin_INCLUDE_DIRS})
add_executable(imageConverter src/imageConverter.cpp ${SRCS})
target_link_libraries(imageConverter ${catkin_LIBRARIES})

And the C++ code I am using:

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

static const std::string OPENCV_WINDOW = "Image window";

class ImageConverter
{
  ros::NodeHandle nh_;
  image_transport::ImageTransport it_;
  image_transport::Subscriber image_sub_;
  image_transport::Publisher image_pub_;

public:
  ImageConverter()
    : it_(nh_)
  {
    // Subscrive to input video feed and publish output video feed
    image_sub_ = it_.subscribe("/camera/image_raw", 1,
      &ImageConverter::imageCb, this);
    image_pub_ = it_.advertise("/image_converter/output_video", 1);

    cv::namedWindow(OPENCV_WINDOW);
  }

  ~ImageConverter()
  {
    cv::destroyWindow(OPENCV_WINDOW);
  }

  void imageCb(const sensor_msgs::ImageConstPtr& msg)
  {
    cv_bridge::CvImagePtr cv_ptr;
    try
    {
      cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
    }
    catch (cv_bridge::Exception& e)
    {
      ROS_ERROR("cv_bridge exception: %s", e.what());
      return;
    }

    // Draw an example circle on the video stream
    if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)
      cv::circle(cv_ptr->image, cv::Point(50, 50), 10, CV_RGB(255,0,0));

    // Update GUI Window
    cv::imshow(OPENCV_WINDOW, cv_ptr->image);
    cv::waitKey(3);

    // Output modified video stream
    image_pub_.publish(cv_ptr->toImageMsg());
  }
};

int main(int argc, char** argv)
{
  ros::init(argc, argv, "image_converter");
  ImageConverter ic;
  ros::spin();
  return 0;
}

Here the errors which states that a bunch of undefined references are found:

[100%] Built target rviz_imu_plugin
CMakeFiles/imageConverter.dir/src/imageConverter.cpp.o: In function `ImageConverter::ImageConverter()':
/home/emanuele/catkin_ws/src/map_ros/src/imageConverter.cpp:89: undefined reference to `cv::namedWindow(cv::String const&, int)'
CMakeFiles/imageConverter.dir/src/imageConverter.cpp.o: In function `ImageConverter::~ImageConverter()':
/home/emanuele/catkin_ws/src/map_ros/src/imageConverter.cpp:94: undefined reference to `cv::destroyWindow(cv::String const&)'
CMakeFiles/imageConverter.dir/src/imageConverter.cpp.o: In function `ImageConverter::imageCb(boost::shared_ptr<sensor_msgs::Image_<std::allocator<void> > const> const&)':
/home/emanuele/catkin_ws/src/map_ros/src/imageConverter.cpp:115: undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/home/emanuele/catkin_ws/src/map_ros/src/imageConverter.cpp:116: undefined reference to `cv::waitKey(int)'
collect2: error: ld returned 1 exit status
map_ros/CMakeFiles/imageConverter.dir/build.make:334: recipe for target '/home/emanuele/catkin_ws/devel/lib/map_ros/imageConverter' failed
make[2]: *** [/home/emanuele/catkin_ws/devel/lib/map_ros/imageConverter] Error 1
CMakeFiles/Makefile2:9236: recipe for target 'map_ros/CMakeFiles/imageConverter.dir/all' failed
make[1]: *** [map_ros/CMakeFiles/imageConverter.dir ...
(more)
edit retag flag offensive close merge delete

Comments

this is a linker error. You probably forgot to specify target_link_libraries correctly in you CMakeLists.txt. Please update your question with the content of your CMakeLists.txt (and please keep it concise, i.e. remove all unnecessary comments)

mgruhler gravatar image mgruhler  ( 2019-07-17 00:58:10 -0500 )edit

@mgruhler, thank you very much for reading the question. I updated the question inserting the small CMake code I am using.

RayROS gravatar image RayROS  ( 2019-07-17 13:59:34 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-07-01 03:52:31 -0500

hosh gravatar image

Add/edit the follwing to the CMakeLists.txt:

find_package(OpenCV  REQUIRED)
include_directories(
  ${OpenCV_INCLUDE_DIRS}
  ${catkin_INCLUDE_DIRS}
)
target_link_libraries(imageConverter ${catkin_LIBRARIES}  ${OpenCV_LIBRARIES})
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2019-07-16 17:27:47 -0500

Seen: 412 times

Last updated: Jul 01 '21