invisible

view src/frame.cc @ 11:d2158a527488

commented out inpaint and painted the missing areas white
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Mon, 04 Nov 2013 21:53:21 +0200
parents a0397f57c07f
children 226073258785
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_BGRA, video_buf.size().width, video_buf.size().height,
41 0, GL_BGRA, GL_UNSIGNED_BYTE, &video_buf);
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_BGRA, depth_buf.size().width, depth_buf.size().height,
49 0, GL_BGRA, GL_UNSIGNED_BYTE, &depth_buf);
51 }
53 void Frame::process()
54 {
55 if(has_video && has_depth)
56 printf("Depth and Rgb\n");
57 }
59 void video_cb(freenect_device *kin_dev, void *video, uint32_t time)
60 {
61 if(!video || !frame) {
62 has_video = false;
63 return;
64 }
66 /* freenect video data to cv mat: */
67 unsigned char* src = (unsigned char*)video;
68 unsigned char* dest = frame->video_buf.data;
70 for(int i=0; i<KINECT_VIDEO_HEIGHT * KINECT_VIDEO_WIDTH; i++) {
71 dest[0] = src[2];
72 dest[1] = src[1];
73 dest[2] = src[0];
74 dest += 3;
75 src += 3;
76 }
77 has_video = true;
78 }
80 void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time)
81 {
82 if(!depth || !frame) {
83 has_depth = false;
84 return;
85 }
87 /* freenect depth data to cv mat: */
88 uint16_t* src = (uint16_t*)depth;
89 uint8_t* dest = (uint8_t*)frame->depth_buf.data;
91 for(int i=0; i<KINECT_DEPTH_HEIGHT * KINECT_DEPTH_WIDTH; i++) {
92 uint16_t val = *src;
93 if(val >= 2047) {
94 val = 2047;
95 }
96 *dest = val >> 3;
97 src++;
98 dest++;
99 }
101 // cv::GaussianBlur(frame->depth_buf, frame->depth_buf, cv::Size(3, 3), 0.5);
103 /*
104 //Inpaint to fill the regions
105 //^ val = 0 instead of 2047 and:
106 cv::Mat mask = 255 - frame->depth_buf;
107 cv::threshold(mask, mask, 254, 255, 3);
108 cv::inpaint(frame->depth_buf, mask, frame->depth_buf, 1, cv::INPAINT_NS);
109 */
110 cv::imshow("foo", frame->depth_buf);
111 cv::waitKey(30);
112 has_depth = true;
113 }
115 bool save_video_ppm(void *video, int w, int h)
116 {
117 FILE *fp;
118 if(!(fp = fopen("test_video.ppm", "wb"))) {
119 fprintf(stderr, "Failed to open video file for writing.\n");
120 return false;
121 }
122 fprintf(fp, "P6\n%d %d\n255\n", w, h);
123 unsigned char *ptr = (unsigned char*)video;
124 for(int i=0; i<w * h * 3; i++) {
125 fputc(*ptr, fp);
126 ptr++;
127 }
128 fclose(fp);
129 return true;
130 }