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