Ask Your Question

problem in displaying an image

asked 2014-03-26 07:28:27 -0500

Abinaya gravatar image

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"); }

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2014-07-03 09:00:39 -0500

Malefitz gravatar image

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()

edit flag offensive delete link more

answered 2017-04-18 11:31:30 -0500

lolo gravatar image

hi try to change your topic to "camera/image" and add waitkey() it worked for me

edit flag offensive delete link more

answered 2014-03-26 07:33:23 -0500

Wolf gravatar image

Try adding cv::startWindowThread() after your cv::namedWindow() ....

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-03-26 07:28:27 -0500

Seen: 2,555 times

Last updated: Apr 18 '17