Advertisement
j0h

depth2SVG

j0h
Jul 18th, 2023
1,320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. //This program intends to get all the point depths from a frame then dump them to std out and close.
  2. #include <fcntl.h>
  3. #include <libfreenect.h>
  4. #include <libfreenect_sync.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <termios.h>
  9. #include <unistd.h>
  10. #include "svg.h"
  11. //gcc -Wall -g -o "%e" "%f"  -lfreenect -lfreenect_sync  
  12.  int dtime=5;
  13.  int arduino;
  14.  
  15. //int current_tilt=&current_tilt);
  16. freenect_context *f_ctx;
  17. freenect_device *f_dev;
  18.    
  19.  
  20.    
  21. #define WIDTH 640
  22. #define HEIGHT 400
  23.  
  24.      int die = 0;
  25.  
  26. int kInit();                 //initiallize / test kinect library and device connection
  27. void kClose();         //close the kinect device
  28. void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp);
  29. void no_kinect_quit(void);            
  30.  
  31. int main(){
  32.  
  33.     kInit();  //init kinect lib, and device
  34.  
  35.     // Set the depth callback function
  36.     freenect_set_depth_callback(f_dev, depth_cb);
  37.  
  38.     // Start the depth stream
  39.     freenect_start_depth(f_dev);
  40.    
  41.      while (!die)    {
  42.         // Update the Kinect device
  43.         if (freenect_process_events(f_ctx) < 0){
  44.             break;
  45.         }
  46.         }
  47.  
  48. kClose();
  49.  
  50.     return 0;
  51. }
  52.  
  53.  
  54. //Kinect Stuff
  55. int kInit(){
  56.         // Initialize libfreenect
  57.     if (freenect_init(&f_ctx, NULL) < 0)    {
  58.         printf("Failed to initialize libfreenect!\n");
  59.         exit(1);
  60.     }
  61.  
  62.     // Set the log level (optional)
  63.   //  freenect_set_log_level(f_ctx, FREENECT_LOG_DEBUG);
  64.  
  65.     // Open the Kinect device
  66.     if (freenect_open_device(f_ctx, &f_dev, 0) < 0)    {
  67.         printf("Failed to open Kinect device!\n");
  68.        exit(1);
  69.     }
  70.     return 0;
  71.     }
  72.    
  73. void kClose(){
  74.         //shutdown functions
  75.     freenect_stop_depth(f_dev);
  76.     freenect_close_device(f_dev);
  77.     freenect_shutdown(f_ctx);
  78.         }
  79.        
  80. void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp){
  81.    svg* psvg;
  82.     psvg = svg_create(WIDTH, HEIGHT);
  83.     // Cast the depth data to unsigned short (16-bit) xbox uses 11 bits of depth
  84.     uint16_t *depth_data = (uint16_t *)depth;
  85.    // uint16_t depth_value = depth_data[index];
  86.     uint16_t depth_value = 0;
  87. if(depth_value>=2046){
  88.     //nearing buffer overflow. this happens as object approaches camera
  89.     depth_value=2046;
  90.     }
  91.     // Print the depth value at a specific pixel (e.g., pixel at (320, 240) (Center of frame))
  92.     int x = 0;
  93.     int y = 0;
  94.     int index = 0; //index 0 to 307840
  95.  
  96. for (x=0; x<=WIDTH; x+=4){  //I expect 640 numbers by 480
  97.     index = y*WIDTH+x;  
  98.     depth_value = depth_data[index];  
  99.     //printf("%d ",depth_value);
  100.   //printf(" %d %d ", x, y);
  101.     svg_pixelDepth(psvg, x, y, depth_value);
  102.     if(x>=WIDTH-1){
  103.         if(y>=HEIGHT-1){
  104.         svg_finalize(psvg);
  105.         svg_save(psvg, "haaaah.svg");
  106.         svg_free(psvg);
  107.         exit(1);
  108.         }
  109.         y+=4;  //goto next line
  110.         x=0;
  111.         printf("%d\n", y);
  112.         }
  113.     }
  114. }
  115. void no_kinect_quit(void){
  116.     printf("Error: Kinect not connected?\n");
  117.     exit(1);
  118. }
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement