CCCPaste

Xbox360Kinect_DepthMap

#include 
#include 
#include 
#include 
#include 
#include 
#include 
/*get depth at a single point
*To do: get all points in array 0-307840
* or select points 
* implement other functions.
*/
//gcc -Wall -g -o "%e" "%f"  -lfreenect -lfreenect_sync   
#define TILT_STEP 5
//int current_tilt=¤t_tilt);
freenect_context *f_ctx;
freenect_device *f_dev;

#define WIDTH 640
#define HEIGHT 480
//(0,0) is top left also index 0
freenect_context *f_ctx;
freenect_device *f_dev;
int die = 0;

void depth_cb(freenect_device *dev, void *depth, uint32_t timestamp){
    // Cast the depth data to unsigned short (16-bit)
    uint16_t *depth_data = (uint16_t *)depth;

    // Print the depth value at a specific pixel (e.g., pixel at (320, 240) (Center of frame))
    int x = 320;
    int y = 240;
    int index = y * WIDTH + x;  //index 0 to 307840 
    uint16_t depth_value = depth_data[index];
if(depth_value>=2046){
	//nearing buffer overflow. this happens as object approaches camera
	depth_value=2046;
	}
    printf("Depth value at (%d, %d): %u\n", x, y, depth_value);

    // You can further process the depth data or perform other actions here
}

int main(){
    // Initialize libfreenect
    if (freenect_init(&f_ctx, NULL) < 0)    {
        printf("Failed to initialize libfreenect!\n");
        return 1;
    }

    // Set the log level (optional)
    freenect_set_log_level(f_ctx, FREENECT_LOG_DEBUG);

    // Open the Kinect device
    if (freenect_open_device(f_ctx, &f_dev, 0) < 0)    {
        printf("Failed to open Kinect device!\n");
        return 1;
    }

    // Set the depth callback function
    freenect_set_depth_callback(f_dev, depth_cb);

    // Start the depth stream
    freenect_start_depth(f_dev);
    printf("Press any key to exit...\n");

    while (!die)    {
        // Update the Kinect device
        if (freenect_process_events(f_ctx) < 0)
            break;
    }

//shutdown functions
    freenect_stop_depth(f_dev);
    freenect_close_device(f_dev);
    freenect_shutdown(f_ctx);

    return 0;
}