Demo entry 6763248

Subscriber.cpp

   

Submitted by anonymous on Oct 21, 2018 at 11:00
Language: C++. Code size: 919 Bytes.

#include <ros/ros.h>
   2 #include <image_transport/image_transport.h>
   3 #include <opencv2/highgui/highgui.hpp>
   4 #include <cv_bridge/cv_bridge.h>
   5 
   6 void imageCallback(const sensor_msgs::ImageConstPtr& msg)
   7 {
   8   try
   9   {
  10     cv::imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image);
  11     cv::waitKey(30);
  12   }
  13   catch (cv_bridge::Exception& e)
  14   {
  15     ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
  16   }
  17 }
  18 
  19 int main(int argc, char **argv)
  20 {
  21   ros::init(argc, argv, "image_listener");
  22   ros::NodeHandle nh;
  23   cv::namedWindow("view");
  24   cv::startWindowThread();
  25   image_transport::ImageTransport it(nh);
  26   image_transport::Subscriber sub = it.subscribe("camera/image", 1, imageCallback);
  27   ros::spin();
  28   cv::destroyWindow("view");
  29 }

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).