Ask Your Question
0

problem in displaying an image

asked 2014-03-24 15:24:29 -0500

Abinaya gravatar image

updated 2014-03-24 16:32:17 -0500

Hi, I want to publish 10 images from my pictures folder..so that I added the link for that..code runs correctly but image is not displaying..

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");
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-03-24 16:38:12 -0500

updated 2014-03-26 15:56:57 -0500

You forgot to tell ros to "spin" in pub.cpp:

while (nh.ok()) 
 {
   pub.publish(ros_image);
   ros::spinOnce(); //Here is where the magic happens
   loop_rate.sleep();
 }

And I am not all that sure about your call to imread

cv::imread("/home/abi/Pictures/attachments/",CV_LOAD_IMAGE_COLOR);

You are passing a folder name instead of a path to an image. Should be something like

cv::imread("/home/abi/Pictures/attachments/someimage.jpg",CV_LOAD_IMAGE_COLOR);

[UPDATE]

Also, you are publishing the topic /static_image/compressed but subscribing on the topic /static_image/. Make sure that you are publishing what you think you are publishing. You can run the publisher node and then check the published topics with: rostopic list

edit flag offensive delete link more

Comments

thank you..But it didn't work..can you suggest some other option.. thank you.

Abinaya gravatar image Abinaya  ( 2014-03-25 05:16:50 -0500 )edit

I have updated my answer, please check it out.

Martin Peris gravatar image Martin Peris  ( 2014-03-26 15:52:16 -0500 )edit

will this has something to do with the compression. i use this image viewer to load and display image in windows and on web. but i have never display static image before. so maybe it won't work

lilylily gravatar image lilylily  ( 2014-03-30 18:25:42 -0500 )edit

Hi, lilylily. How about building a web-based image viewer to help display images? Using code is too complicated for me. I prefer to ask some 3rd party Imaging SDKs for help. It will be better if the imaging SDK is totally manual and can be customized by users according to our own favors. Any other suggestion?

arronlee gravatar image arronlee  ( 2014-05-11 17:03:28 -0500 )edit

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

Stats

Asked: 2014-03-24 15:24:29 -0500

Seen: 828 times

Last updated: May 11 '14