Rosanswers logo

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..


#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;

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


1 Answer 1


Rosanswers logo

It should work like this:

cv::imshow("view", cv_ptr->image);

To wait for a couple of miliseconds with waitKey() when trying to display an image is important in OpenCV, because it allows some time for GUI processing.

You don't really need cvNamedWindow() if you use the default parameter CV_WINDOW_AUTOSIZE because a window is already created by imshow(). If you want to use name window anyway, you should stick to the OpenCV C++ API: cv::namedWindow()

Originally posted by Malefitz with karma: 136 on 2014-07-03

This answer was ACCEPTED on the original site

Post score: 1


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.