invisible

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