invisible
view src/frame.cc @ 25:96b022f1210e
'w' to switch to wireframe
author | Eleni Maria Stea <eleni@mutantstargoat.com> |
---|---|
date | Sun, 17 Nov 2013 12:46:45 +0200 |
parents | 65fd6d7c42b1 |
children |
line source
1 #include <GL/gl.h>
2 #include <cxcore.h>
3 #include <opencv2/imgproc/imgproc.hpp>
4 #include <opencv2/photo/photo.hpp>
6 #include <highgui.h> //TODO remove
7 #include <cv.h> //TODO remove
9 #include <pthread.h>
10 #include <stdio.h>
11 #include <string.h>
13 #include "frame.h"
14 #include "kinect.h"
16 extern Frame *frame;
17 extern KinectParams *kin_params;
19 extern bool has_video;
20 extern bool has_depth;
22 Frame::Frame()
23 {
24 video_buf = cv::Mat::zeros(KINECT_VIDEO_HEIGHT, KINECT_VIDEO_WIDTH, CV_8UC3);
25 depth_buf = cv::Mat::zeros(KINECT_DEPTH_HEIGHT, KINECT_DEPTH_WIDTH, CV_8UC1);
27 tex_setup();
28 }
30 void Frame::tex_setup()
31 {
32 glGenTextures(1, &video_tex);
33 glGenTextures(1, &depth_tex);
35 glBindTexture(GL_TEXTURE_2D, video_tex);
36 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
37 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
38 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
39 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
40 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, KINECT_VIDEO_WIDTH, KINECT_VIDEO_HEIGHT,
41 0, GL_BGR, GL_UNSIGNED_BYTE, 0);
43 glBindTexture(GL_TEXTURE_2D, depth_tex);
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
45 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
47 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
48 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, KINECT_DEPTH_WIDTH, KINECT_DEPTH_HEIGHT,
49 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, 0);
50 }
52 void Frame::process()
53 {
54 if(has_video) {
55 glBindTexture(GL_TEXTURE_2D, video_tex);
56 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, KINECT_VIDEO_WIDTH, KINECT_VIDEO_HEIGHT, GL_BGR, GL_UNSIGNED_BYTE, video_buf.data);
57 }
58 if(has_depth) {
59 glBindTexture(GL_TEXTURE_2D, depth_tex);
60 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, KINECT_DEPTH_WIDTH, KINECT_DEPTH_HEIGHT, GL_LUMINANCE, GL_UNSIGNED_BYTE, depth_buf.data);
61 }
62 if(has_video && has_depth) {//pot
63 }
64 }
66 void video_cb(freenect_device *kin_dev, void *video, uint32_t time)
67 {
68 if(!video || !frame) {
69 has_video = false;
70 return;
71 }
73 /* freenect video data to cv mat: */
74 unsigned char* src = (unsigned char*)video;
75 unsigned char* dest = frame->video_buf.data;
77 for(int i=0; i<KINECT_VIDEO_HEIGHT * KINECT_VIDEO_WIDTH; i++) {
78 dest[0] = src[2];
79 dest[1] = src[1];
80 dest[2] = src[0];
81 dest += 3;
82 src += 3;
83 }
84 has_video = true;
85 }
87 void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time)
88 {
89 if(!depth || !frame) {
90 has_depth = false;
91 return;
92 }
94 /* freenect depth data to cv mat: */
95 uint16_t* src = (uint16_t*)depth;
96 uint8_t* dest = (uint8_t*)frame->depth_buf.data;
98 for(int i=0; i<KINECT_DEPTH_HEIGHT * KINECT_DEPTH_WIDTH; i++) {
99 uint16_t val = *src;
100 if(val >= 2047) {
101 val = 2047;
102 }
103 *dest = val >> 2; //should be >> 3
104 src++;
105 dest++;
106 }
108 // cv::GaussianBlur(frame->depth_buf, frame->depth_buf, cv::Size(3, 3), 0.5);
110 /*
111 //Inpaint to fill the regions
112 //^ val = 0 instead of 2047 and:
113 cv::Mat mask = 255 - frame->depth_buf;
114 cv::threshold(mask, mask, 254, 255, 3);
115 cv::inpaint(frame->depth_buf, mask, frame->depth_buf, 1, cv::INPAINT_NS);
116 */
117 has_depth = true;
118 }
120 bool save_video_ppm(void *video, int w, int h)
121 {
122 FILE *fp;
123 if(!(fp = fopen("test_video.ppm", "wb"))) {
124 fprintf(stderr, "Failed to open video file for writing.\n");
125 return false;
126 }
127 fprintf(fp, "P6\n%d %d\n255\n", w, h);
128 unsigned char *ptr = (unsigned char*)video;
129 for(int i=0; i<w * h * 3; i++) {
130 fputc(*ptr, fp);
131 ptr++;
132 }
133 fclose(fp);
134 return true;
135 }