Difference between revisions of "NaviTrack Tutorial:Integrating:Passing image data"

From NAMIC Wiki
Jump to: navigation, search
Line 76: Line 76:
 
         height = 0;
 
         height = 0;
 
       }
 
       }
  }
 
 
MyTutorialModule.cxx (and .h): add the follwing function:
 
  void MyTutorialModule::getImage(Image& img, int& w, int& h)
 
  {
 
    if(sink != NULL)
 
      {
 
        sink->getImage(img, w, h);
 
      }
 
 
 
 
   }
 
   }
  

Revision as of 16:23, 14 June 2007

Home < NaviTrack Tutorial:Integrating:Passing image data

Overview for passing image data

You can pass image data from one application to another through NaviTrack in almost same way as passing coordinate data.

You can use Image class in NaviTrack to image transfer. 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


!!NOTE!! 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. NaviTrack has no capability to recover this kind of error in the current version.

Back to Integrating into your application.