NaviTrack Tutorial:Integrating:Passing image data

From NAMIC Wiki
Revision as of 19:43, 14 June 2007 by Tokuda (talk | contribs)
Jump to: navigation, search
Home < NaviTrack Tutorial:Integrating:Passing image data

Overview for passing image data

NaviTrack have a capability to send any C++ data types through NaviTrack data stream. That means, once you define a C++ class, you can transfer any kind of data even through a network, in quite same way as you did for coordinate data in previous tutorial pages.

For an image transfer, NaviTrack has already had a "Image" class to pass image data. This class has a few member variables including image size and image itself, as well as memberfunctions to serialize image data. Serialized image data is pushed into NaviTrack data flow as an event, and transfered to other nodes.

Image class is defined in include/OpenTracker/types/Image.h and src/input/types/Image.cxx.

Sending side

Modify MyTutorialModule

To send data through NaviTrack, it is necessary to generate a new event, which contains the data. The following code is implemented into MyTutorialModule to be called from your application.

 void MyTutorialModule::setImage(Image& img, int w, int h)
 {  
   if(source!=NULL)
     {
       Event event;
       event.setAttribute("image", img);
       event.setAttribute("xsize", w);
       event.setAttribute("ysize", h);
       event.timeStamp();
       source->updateObservers( event );
     }
 }

pushimage.cxx

The core part of image sending would be like following code:

 MyTutorialModule* mtm = (MyTutorialModule*) context.getModule("MyTutorialConfig");
 
 int stopflag = 0;
 int i = 0;
 while (stopflag == 0) {
   
    // load image data to image_data here.
     ....
   
   Image img(width, heigt, sizeof(short), (char*)image_data);
   
   if (mtm) {
     mtm->setImage(img, w, h);
   }
   
   stopflag = context.loopOnce();
   
   usleep(rate);
 }

Receiving side

Modify MyTutorialSink and MyTutorialModule

MyTutorialSink.h: Add member variables to hold the coordinate data:

 private:
   Image image;
   int width;
   int height;

MyTutorialSink.cxx:

 void MyTutorialSink::onEventGenerated( Event& event, Node& generator)
 {
   if (event.hasAttribute("image") &&
       event.hasAttribute("xsize") &&
       event.hasAttribute("ysize"))
     {
       image = event.getAttribute((Image*)NULL, "image");
       width  = event.getAttribute(std::string("xsize"),0);
       height = event.getAttribute(std::string("ysize"),0);
     }
   else
     {
       width  = 0;
       height = 0;
     }
 }

pullimage.cxx

 MyTutorialModule* mtm = (MyTutorialModule*) context.getModule("MyTutorialConfig");
 
 while (stopflag == 0) {
   
   stopflag = context.loopOnce();   // push and pull Event
   
   if (mtm) {
     Image img;
     int w, h;
     
     mtm->getImage(img, w, h);
     if (img.size() != 0) {
        
       // do something for the received image here
        ...
     
     }
   }
 
   usleep(rate);
 }

Testing

In terminal 1 (sending side: pushimage)

$ ./pushimage tutorial_source.xml

In terminal 2 (receiving side: pullimage)

$ ./pullimage tutorial_source.xml


Current Probelms on Image Transfer using NaviTrack

Microsoft Windows issue

In case that the image size exceeds approx 8000 bytes (uncertain), data transfer from NetworkSink to NetworkSource might fail in Windows environment. This is thought to be due to an implementation of OpenTracker or ACE library, which NaviTrack depends on, but we are not sure the reason, and we continue to work on this issue. At this moment, to avoid this problem, the programs used in this tutorial splits image data into several fragments with size of less than 8000 bytes in a pushimage program, and reconstruct a complete image from fragments in a pullimage program.

UDP issue

You may encounter application errors, while passing image data through a network using NaviTrack NetworkModule. This is because NaviTrack NetworkModule uses UDP for a communication between Sink and Source. Each data has header information in this communication including entire size of the data, and packet loss causes inconsistency between data size information in header and actual data size. Currently, NaviTrack has no capability to recover this kind of errors.

Back to Integrating into your application.