Hi, I want to publish an image..previously I posted the same question but I got that I forgot to add ros::spin().. But even then there is a problem..It compiled successfully..while running it not displaying an image..
pub.cpp:
#include <ros/ros.h> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <cv_bridge/cv_bridge.h> #include <image_transport/image_transport.h> #include <sensor_msgs/image_encodings.h>
int main(int argc, char** argv) { ros::init(argc, argv, "image_publisher"); ros::NodeHandle nh;
cv_bridge::CvImage cv_image;
cv_image.image = cv::imread("/home/abi/Pictures/attachments/",CV_LOAD_IMAGE_COLOR);
cv_image.encoding = "bgr8";
sensor_msgs::Image ros_image;
cv_image.toImageMsg(ros_image);
image_transport::ImageTransport it(nh); image_transport::Publisher pub = it.advertise("/static_image/compressed", 3); ros::Rate loop_rate(5);
while (nh.ok()) { pub.publish(ros_image); loop_rate.sleep(); } }
sub.cpp: #include <ros/ros.h> #include <image_transport/image_transport.h> #include <opencv/cv.h> #include <opencv/highgui.h> #include <cv_bridge/cv_bridge.h>
void imageCallback(const sensor_msgs::ImageConstPtr& msg) { cv_bridge::CvImagePtr cv_ptr; try { cv_ptr = cv_bridge::toCvCopy(msg, "bgr8"); //ROS_INFO("Hi"); cv::imshow("view", cv_ptr->image); cvNamedWindow("view",CV_WINDOW_AUTOSIZE); } catch (cv_bridge::Exception& e) { ROS_ERROR("cv_bridge exception: %s", e.what()); return; } }
int main(int argc, char **argv) { ros::init(argc, argv, "image_listener"); ros::NodeHandle nh; cvNamedWindow("view",CV_WINDOW_AUTOSIZE); image_transport::ImageTransport it(nh); image_transport::Subscriber sub = it.subscribe("/static_image/", 1,imageCallback); ros::spin(); cvDestroyWindow("view"); }
Originally posted by Abinaya on ROS Answers with karma: 1 on 2014-03-26
Post score: 0