#include#include #include void imageCallback(const sensor_msgs::ImageConstPtr& msg) { ROS_INFO("Received image of size %dx%d", msg->width, msg->height); } int main(int argc, char **argv) { ros::init(argc, argv, "image_subscriber"); ros::NodeHandle nh; image_transport::ImageTransport it(nh); image_transport::Subscriber sub = it.subscribe("camera/image_raw", 1, imageCallback); ros::spin(); // wait for image messages return 0; }
#includeThis example creates a subscriber to a compressed image topic `camera/image/compressed`. In the callback function `compressedImageCallback`, the received image message is decoded and converted into an OpenCV image using `cv::imdecode` method. The size of the image is displayed using `ROS_INFO`. The package library for `image_transport` is `image_transport`.#include #include void compressedImageCallback(const sensor_msgs::CompressedImageConstPtr& msg) { cv::Mat img = cv::imdecode(cv::Mat(msg->data), 1); ROS_INFO("Received image of size %dx%d", img.cols, img.rows); } int main(int argc, char** argv) { ros::init(argc, argv, "image_subscriber"); ros::NodeHandle nh; image_transport::ImageTransport it(nh); image_transport::Subscriber sub = it.subscribe("camera/image/compressed", 1, compressedImageCallback); ros::spin(); return 0; }