ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

spagi's profile - activity

2012-09-12 17:27:52 -0500 received badge  Famous Question (source)
2012-09-12 17:27:52 -0500 received badge  Notable Question (source)
2012-08-27 00:54:50 -0500 received badge  Famous Question (source)
2012-06-26 13:22:41 -0500 received badge  Popular Question (source)
2012-06-07 17:16:36 -0500 received badge  Notable Question (source)
2012-03-20 21:58:42 -0500 received badge  Popular Question (source)
2011-11-22 07:06:50 -0500 commented answer how to save an sensor_msgs/Image to a file?
Basically I need the C++ node to get the image from the kinect and then the rest of my code runs on RosJava because I use the image to query google and do lexical analysis to the results. But I think that I might skip the whole image transfer thing and just store it and ready it a a file in rosjava
2011-11-21 10:01:45 -0500 asked a question how to save an sensor_msgs/Image to a file?

Hi all. I am trying to pass an image from a Ros node(C++) to a ROSjava node. I managed to make the rosjava node to subscribe to the others topic. The C++ is publishing an image topic and the Rosjava i supposed to write the image in a file.

The problem is that I cannot find an equivalent on Rosjava for the CVBridge that is used in C++ to make the image message a Iplimage file and save it. What I currently do is try to put the message in a byte[] and then read it in a file. But the result is an jpg file 4 times bigger that the one sent and unreadable. Here is the code I use for the C++ node:

int main(int argc, char **argv)

{    
  ros::init(argc, argv, "imagesender");    
  ros::NodeHandle n;    
  ros::Publisher image_pub = n.advertise<sensor_msgs::Image>("image/mysender", 1);

  printf("Loading image...\n");
  IplImage* imgsrc = cvLoadImage( "/home/spagi/ros_workspace/jpgtransfer/src/testimage.jpg" );
  if(!imgsrc){ printf("Could not load image file\n");}
  else{printf("Image Loaded Sucessfully\n");}

  sensor_msgs::ImagePtr imgmsg = sensor_msgs::CvBridge::cvToImgMsg(imgsrc, "bgr8");

  ros::Rate loop_rate(1);
  while (ros::ok())
  {  
    image_pub.publish(imgmsg);  
    ros::spinOnce();
    loop_rate.sleep();
  }    
  return 0;
}

And here the code for the Rosjava node:

 @Override
  public void main(Node node) {

      final FileInputStream fileInputStream=null;

    Preconditions.checkState(this.node == null);
    this.node = node;
    try {
      //final Log log = node.getLog();
      System.out.println("Started main node");
      node.newSubscriber("image/mysender", "sensor_msgs/Image",
          new MessageListener<org.ros.message.sensor_msgs.Image>() {
            @Override
            public void onNewMessage(org.ros.message.sensor_msgs.Image message) {
                System.out.println("Got a new Image message");
                if(x==0){
                    x++;                    
                byte[] byteArray = message.data;
                try {

                //convert array of bytes into file
                FileOutputStream fileOuputStream = 
                          new FileOutputStream("/home/spagi/Desktop/javaccimage.jpg"); 
                fileOuputStream.write(byteArray);
                fileOuputStream.close();            
                System.out.println("Done");                 
                }catch(Exception e){
                    e.printStackTrace();
                }
                }                   
            }

          });
    } catch (Exception e) {
      if (node != null) {
        node.getLog().fatal(e);
      } else {
        e.printStackTrace();
      }
    }
  }

So again my problem is what to do with that message object and how can I make it a file. I dont know if there is an alternative for the publisher on the :

sensor_msgs::CvBridge::cvToImgMsg(imgsrc, "bgr8");
2011-11-21 09:43:21 -0500 marked best answer image_transport between ROS node and ROSJava node

I haven't used rosjava yet, but probably you just need to use sensor_msgs/Image instead of std_msgs/String. The class should be org.ros.message.sensor_msgs.Image.

I wouldn't worry about image_transport in Java yet. image_transport is a C++ package that basically wraps a normal ROS image topic and provides compressed versions of the image stream, which can be very useful when publishing over a bandwidth-limited network. I believe rosjava has an example of subscribing to a JPEG-compressed image topic directly, but there isn't a nice wrapper for arbitrary compressed transports like in C++.

2011-11-21 09:43:21 -0500 received badge  Scholar (source)
2011-11-21 09:42:33 -0500 received badge  Supporter (source)
2011-11-21 06:58:39 -0500 received badge  Nice Question (source)
2011-11-19 20:53:26 -0500 received badge  Editor (source)
2011-11-19 13:38:24 -0500 received badge  Student (source)
2011-11-19 09:12:56 -0500 asked a question image_transport between ROS node and ROSJava node

Hi, am getting a picture with kinect and I want to tranfer it a Rosjava node to use it there. I have managed to exchange messages between those two nodes, but the image_transport is unclear on the Rosjava side. I cannot find documentation on which imports to make and what libraries to refer to. I am trying to edit a normal message listener rosjava node to make it able to receive images. This is my base code that I try to change:

public class Javacvtransfer implements NodeMain {
private Node node;
  @Override
  public void main(Node node)
  {
      Preconditions.checkState(this.node == null);
      this.node = node;
        try {
            final Log log = node.getLog();
            node.newSubscriber("camera/image", "std_msgs/String",
                new MessageListener<org.ros.message.std_msgs.String>() 
            {
                  @Override
                  public void onNewMessage(org.ros.message.std_msgs.String message) {
                    log.info("I heard: \"" + message.data + "\"");
                  }
                });
          } catch (Exception e) {
            if (node != null) {
              node.getLog().fatal(e);
            } else {
              e.printStackTrace();
            }
          }
        }
  @Override
  public void shutdown() {
    node.shutdown();
    node = null;
  } }

I dont know what imports to make also for the image_transport in rosjava. Any help would be appreciated.