Advertisement
j0h

Xbox360Kinect_DepthMap

j0h
Jul 6th, 2023 (edited)
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <termios.h>
  5. #include <fcntl.h>
  6. #include <libfreenect.h>
  7. #include <libfreenect_sync.h>
  8. /*get depth at a single point
  9. *To do: get all points in array 0-307840
  10. * or select points
  11. * implement other functions.
  12. */
  13. //gcc -Wall -g -o "%e" "%f"  -lfreenect -lfreenect_sync  
  14. #define TILT_STEP 5
  15. //int current_tilt=&current_tilt);
  16. freenect_context *f_ctx;
  17. freenect_device *f_dev;
  18.  
  19. #define WIDTH 640
  20. #define HEIGHT 480
  21. //(0,0) is top left also index 0
  22. freenect_context *f_ctx;
  23. freenect_device *f_dev;
  24. int die = 0;
  25.  
  26. void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp){
  27.     // Cast the depth data to unsigned short (16-bit)
  28.     uint16_t *depth_data = (uint16_t *)depth;
  29.  
  30.     // Print the depth value at a specific pixel (e.g., pixel at (320, 240) (Center of frame))
  31.     int x = 320;
  32.     int y = 240;
  33.     int index = y * WIDTH + x;  //index 0 to 307840
  34.     uint16_t depth_value = depth_data[index];
  35. if(depth_value>=2046){
  36.     //nearing buffer overflow. this happens as object approaches camera
  37.     depth_value=2046;
  38.     }
  39.     printf("Depth value at (%d, %d): %u\n", x, y, depth_value);
  40.  
  41.     // You can further process the depth data or perform other actions here
  42. }
  43.  
  44. int main(){
  45.     // Initialize libfreenect
  46.     if (freenect_init(&f_ctx, NULL) < 0)    {
  47.         printf("Failed to initialize libfreenect!\n");
  48.         return 1;
  49.     }
  50.  
  51.     // Set the log level (optional)
  52.     freenect_set_log_level(f_ctx, FREENECT_LOG_DEBUG);
  53.  
  54.     // Open the Kinect device
  55.     if (freenect_open_device(f_ctx, &f_dev, 0) < 0)    {
  56.         printf("Failed to open Kinect device!\n");
  57.         return 1;
  58.     }
  59.  
  60.     // Set the depth callback function
  61.     freenect_set_depth_callback(f_dev, depth_cb);
  62.  
  63.     // Start the depth stream
  64.     freenect_start_depth(f_dev);
  65.     printf("Press any key to exit...\n");
  66.  
  67.     while (!die)    {
  68.         // Update the Kinect device
  69.         if (freenect_process_events(f_ctx) < 0)
  70.             break;
  71.     }
  72.  
  73. //shutdown functions
  74.     freenect_stop_depth(f_dev);
  75.     freenect_close_device(f_dev);
  76.     freenect_shutdown(f_ctx);
  77.  
  78.     return 0;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement