invisible

view src/frame.cc @ 10:a0397f57c07f

quick backup - TODO: fix inpaint :p
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Mon, 04 Nov 2013 01:45:51 +0200
parents 6f5f53d0d166
children d2158a527488
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 = 0;
95 }
96 *dest = val >> 3;
97 src++;
98 dest++;
99 }
101 // cv::fastNlMeansDenoising(frame->depth_buf, frame->depth_buf, 7, 21, 3);
103 cv::GaussianBlur(frame->depth_buf, frame->depth_buf, cv::Size(3, 3), 0.5);
104 cv::Mat mask = 255 - frame->depth_buf;
105 cv::threshold(mask, mask, 254, 255, 3);
106 cv::inpaint(frame->depth_buf, mask, frame->depth_buf, 1, cv::INPAINT_NS);
108 cv::imshow("foo", frame->depth_buf);
109 cv::waitKey(30);
110 has_depth = true;
111 }
113 bool save_video_ppm(void *video, int w, int h)
114 {
115 FILE *fp;
116 if(!(fp = fopen("test_video.ppm", "wb"))) {
117 fprintf(stderr, "Failed to open video file for writing.\n");
118 return false;
119 }
120 fprintf(fp, "P6\n%d %d\n255\n", w, h);
121 unsigned char *ptr = (unsigned char*)video;
122 for(int i=0; i<w * h * 3; i++) {
123 fputc(*ptr, fp);
124 ptr++;
125 }
126 fclose(fp);
127 return true;
128 }