Notice: MediaWiki has been updated. Report any rough edges to marcan@marcan.st

Difference between revisions of "C Sync Wrapper"

From OpenKinect
Jump to: navigation, search
(Example)
m (Interface)
 
(16 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
== About ==
 
== About ==
The freenect driver provides an "asynchronous" interface where you provide callbacks and it calls them when sensor data is available.  There is another way of thinking about the data by having a "synchronous" interface where you call a function to get an image "freenect_sync_get_depth" or "freenect_sync_get_rgb" and it blocks and gives you back the data.
+
The freenect driver provides an "asynchronous" interface where you provide callbacks and it calls them when sensor data is available.  There is another way of thinking about the data by having a "synchronous" interface where you call a function to get an image "freenect_sync_get_depth" or "freenect_sync_get_video" and it blocks and gives you back the data.
  
 
This is implemented by using a thread to handle the callbacks and a buffer to provide an interface for the client.  You are guaranteed to get the buffers in order by timestamp (e.g., you won't get something older than your last).  You may not get all frames with this interface (e.g., there may be gaps); however, a 'best effort' is made.
 
This is implemented by using a thread to handle the callbacks and a buffer to provide an interface for the client.  You are guaranteed to get the buffers in order by timestamp (e.g., you won't get something older than your last).  You may not get all frames with this interface (e.g., there may be gaps); however, a 'best effort' is made.
Line 10: Line 10:
 
However ([[User:Brandyn|Brandyn]]) and ([[User:amiller|amiller]]) are trying to organize things for the Synchronous interface.
 
However ([[User:Brandyn|Brandyn]]) and ([[User:amiller|amiller]]) are trying to organize things for the Synchronous interface.
  
== Interface ==                                                                                
+
== Interface ==                                                                                                                                                          
<nowiki>#</nowiki>define RGB_BYTES 921600  // 480 * 640 * 3
+
int freenect_sync_get_video(void **video, uint32_t *timestamp, int index, freenect_video_format fmt);
                                                                           
+
     Synchronous video function, starts the runloop if it isn't running                                                           
int freenect_sync_get_rgb(char **rgb, uint32_t *timestamp);
+
   
     Synchronous rgb function, starts the runloop if it isn't running                                                           
+
    The returned buffer is valid until this function is called again, after which the buffer must not
 +
    be used again.  Make a copy if the data is required.       
 
                                                                                                                                
 
                                                                                                                                
 
     Args:                                                                                                                     
 
     Args:                                                                                                                     
         rgb: Populated with a pointer to a RGB buffer (of size RGB_BYTES) that you need to free                                                                                                    
+
         video: Populated with a pointer to a video buffer with a size of the requested type                                                                                                    
         timestamp: Populated with a pointer to the associated timestamp                                                      
+
         timestamp: Populated with the associated timestamp
 +
        index: Device index (0 is the first)
 +
        fmt: Valid format                                                 
 
                                                                                                                                
 
                                                                                                                                
 
     Returns:                                                                                                                   
 
     Returns:                                                                                                                   
 
         Nonzero on error.                                                                                                     
 
         Nonzero on error.                                                                                                     
  
 
+
int freenect_sync_get_depth(void **depth, uint32_t *timestamp, int index, freenect_depth_format fmt);
<nowiki>#</nowiki>define DEPTH_BYTES 614400 // 480 * 640 * 2
+
     Synchronous depth function, starts the runloop if it isn't running
 
+
   
int freenect_sync_get_depth(char **depth, uint32_t *timestamp);
+
    The returned buffer is valid until this function is called again, after which the buffer must not
     Synchronous depth function, starts the runloop if it isn't running                                                         
+
    be used again.  Make a copy if the data is required.                                                        
 
                                                                                                                                
 
                                                                                                                                
 
     Args:                                                                                                                     
 
     Args:                                                                                                                     
         depth: Populated with a pointer to a depth buffer (of size DEPTH_BYTES) that you need to free                                                
+
         depth: Populated with a pointer to a depth buffer with a size of the requested type                                                
         timestamp: Populated with a pointer to the associated timestamp                                                      
+
         timestamp: Populated with the associated timestamp
 +
        index: Device index (0 is the first)
 +
        fmt: Valid format                                                   
 
                                                                                                                                
 
                                                                                                                                
 
     Returns:                                                                                                                   
 
     Returns:                                                                                                                   
 
         Nonzero on error.
 
         Nonzero on error.
 +
 +
void freenect_sync_stop(void);
 +
    Stops the runloop (if running), cleans up all buffers, unsets callbacks, and flushes queues.
 +
   
 +
    Returns:
 +
        void.
 +
 
== Example ==
 
== Example ==
Here is a very short example using the c_sync wrappers and opencv.
+
Here is a very short example using the c_sync wrappers and opencv:
  
http://pastebin.com/bSVxJZfU
+
  // Be sure to link with -lfreenect_sync
 +
  #include <stdlib.h>
 +
  #include <stdio.h>
 +
  #include <opencv/cv.h>
 +
  #include <opencv/highgui.h>
 +
  #include <libfreenect_sync.h>
 +
  int main()
 +
  { 
 +
      IplImage *image = cvCreateImageHeader(cvSize(640,480), 8, 3);
 +
      while (cvWaitKey(10) < 0)
 +
      {
 +
        char *data;
 +
        unsigned int timestamp;
 +
        freenect_sync_get_video((void**)(&data), &timestamp, 0, FREENECT_VIDEO_RGB);
 +
        cvSetData(image, data, 640*3);
 +
        cvCvtColor(image, image, CV_RGB2BGR);
 +
        cvShowImage("RGB", image);
 +
      }
 +
      freenect_sync_stop();     
 +
      cvFree(&image);
 +
      return EXIT_SUCCESS;
 +
  }
  
 
== Performance ==
 
== Performance ==

Latest revision as of 00:45, 20 March 2011

About

The freenect driver provides an "asynchronous" interface where you provide callbacks and it calls them when sensor data is available. There is another way of thinking about the data by having a "synchronous" interface where you call a function to get an image "freenect_sync_get_depth" or "freenect_sync_get_video" and it blocks and gives you back the data.

This is implemented by using a thread to handle the callbacks and a buffer to provide an interface for the client. You are guaranteed to get the buffers in order by timestamp (e.g., you won't get something older than your last). You may not get all frames with this interface (e.g., there may be gaps); however, a 'best effort' is made.

Who is involved

Search for Synchronous in People

However (Brandyn) and (amiller) are trying to organize things for the Synchronous interface.

Interface

int freenect_sync_get_video(void **video, uint32_t *timestamp, int index, freenect_video_format fmt);

   Synchronous video function, starts the runloop if it isn't running                                                          
   
   The returned buffer is valid until this function is called again, after which the buffer must not
   be used again.  Make a copy if the data is required.         
                                                                                                                             
   Args:                                                                                                                     
       video: Populated with a pointer to a video buffer with a size of the requested type                                                                                                    
       timestamp: Populated with the associated timestamp
       index: Device index (0 is the first)
       fmt: Valid format                                                   
                                                                                                                             
   Returns:                                                                                                                  
       Nonzero on error.                                                                                                     

int freenect_sync_get_depth(void **depth, uint32_t *timestamp, int index, freenect_depth_format fmt);

   Synchronous depth function, starts the runloop if it isn't running
   
   The returned buffer is valid until this function is called again, after which the buffer must not
   be used again.  Make a copy if the data is required.                                                        
                                                                                                                             
   Args:                                                                                                                     
       depth: Populated with a pointer to a depth buffer with a size of the requested type                                               
       timestamp: Populated with the associated timestamp  
       index: Device index (0 is the first)
       fmt: Valid format                                                     
                                                                                                                             
   Returns:                                                                                                                  
       Nonzero on error.

void freenect_sync_stop(void);

   Stops the runloop (if running), cleans up all buffers, unsets callbacks, and flushes queues.
   
   Returns:
       void.

Example

Here is a very short example using the c_sync wrappers and opencv:

  // Be sure to link with -lfreenect_sync
  #include <stdlib.h>
  #include <stdio.h>
  #include <opencv/cv.h>
  #include <opencv/highgui.h>
  #include <libfreenect_sync.h>
  int main()
  {   
      IplImage *image = cvCreateImageHeader(cvSize(640,480), 8, 3);
      while (cvWaitKey(10) < 0) 
      {
        char *data;
        unsigned int timestamp;
        freenect_sync_get_video((void**)(&data), &timestamp, 0, FREENECT_VIDEO_RGB);
        cvSetData(image, data, 640*3);
        cvCvtColor(image, image, CV_RGB2BGR);
        cvShowImage("RGB", image);
      }
      freenect_sync_stop();       
      cvFree(&image);
      return EXIT_SUCCESS;
  }

Performance

TODO

TODO