invisible

view src/frame.cc @ 12:226073258785

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