invisible

view src/frame.cc @ 9:6f5f53d0d166

use 8bit mat to save depth frames to avoid convertions during inpaint
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Mon, 04 Nov 2013 00:29:14 +0200
parents bd1b0385a10b
children a0397f57c07f
line source
1 #include <GL/gl.h>
2 #include <cxcore.h>
3 #include <opencv2/imgproc/imgproc.hpp>
5 #include <highgui.h> //TODO remove
6 #include <cv.h> //TODO remove
8 #include <pthread.h>
9 #include <stdio.h>
10 #include <string.h>
12 #include "frame.h"
13 #include "kinect.h"
15 extern Frame *frame;
16 extern KinectParams *kin_params;
18 extern bool has_video;
19 extern bool has_depth;
21 Frame::Frame()
22 {
23 video_buf = cv::Mat::zeros(KINECT_VIDEO_HEIGHT, KINECT_VIDEO_WIDTH, CV_8UC3);
24 depth_buf = cv::Mat::zeros(KINECT_DEPTH_HEIGHT, KINECT_DEPTH_WIDTH, CV_8UC1);
26 tex_setup();
27 }
29 void Frame::tex_setup()
30 {
31 glGenTextures(1, &video_tex);
32 glGenTextures(1, &depth_tex);
34 glBindTexture(GL_TEXTURE_2D, video_tex);
35 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
36 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
37 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
38 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
39 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, video_buf.size().width, video_buf.size().height,
40 0, GL_BGRA, GL_UNSIGNED_BYTE, &video_buf);
42 glBindTexture(GL_TEXTURE_2D, depth_tex);
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
45 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
47 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, depth_buf.size().width, depth_buf.size().height,
48 0, GL_BGRA, GL_UNSIGNED_BYTE, &depth_buf);
50 }
52 void Frame::process()
53 {
54 if(has_video && has_depth)
55 printf("Depth and Rgb\n");
56 }
58 void video_cb(freenect_device *kin_dev, void *video, uint32_t time)
59 {
60 if(!video || !frame) {
61 has_video = false;
62 return;
63 }
65 /* freenect video data to cv mat: */
66 unsigned char* src = (unsigned char*)video;
67 unsigned char* dest = frame->video_buf.data;
69 for(int i=0; i<KINECT_VIDEO_HEIGHT * KINECT_VIDEO_WIDTH; i++) {
70 dest[0] = src[2];
71 dest[1] = src[1];
72 dest[2] = src[0];
73 dest += 3;
74 src += 3;
75 }
76 has_video = true;
77 }
79 void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time)
80 {
81 if(!depth || !frame) {
82 has_depth = false;
83 return;
84 }
86 /* freenect depth data to cv mat: */
87 uint16_t* src = (uint16_t*)depth;
88 uint8_t* dest = (uint8_t*)frame->depth_buf.data;
90 for(int i=0; i<KINECT_DEPTH_HEIGHT * KINECT_DEPTH_WIDTH; i++) {
91 uint16_t val = *src;
92 if(val >= 2047) {
93 val = 0;
94 }
95 *dest = val >> 3;
96 src++;
97 dest++;
98 }
100 cv::medianBlur(frame->depth_buf, frame->depth_buf, 5);
101 cv::imshow("foo", frame->depth_buf);
102 cv::waitKey(100);
103 has_depth = true;
104 }
106 bool save_video_ppm(void *video, int w, int h)
107 {
108 FILE *fp;
109 if(!(fp = fopen("test_video.ppm", "wb"))) {
110 fprintf(stderr, "Failed to open video file for writing.\n");
111 return false;
112 }
113 fprintf(fp, "P6\n%d %d\n255\n", w, h);
114 unsigned char *ptr = (unsigned char*)video;
115 for(int i=0; i<w * h * 3; i++) {
116 fputc(*ptr, fp);
117 ptr++;
118 }
119 fclose(fp);
120 return true;
121 }