invisible

changeset 5:700127288dc5

quick backup todo: fix save_ppm subimage2d
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Thu, 24 Oct 2013 00:09:13 +0300
parents 1ff5a1a50b41
children db8f1c036d0e
files Makefile src/frame.cc src/frame.h src/kinect.cc src/kinect.h
diffstat 5 files changed, 124 insertions(+), 29 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Wed Oct 16 15:41:08 2013 +0300
     1.2 +++ b/Makefile	Thu Oct 24 00:09:13 2013 +0300
     1.3 @@ -14,6 +14,8 @@
     1.4  $(bin): $(obj)
     1.5  	$(CXX) -o $@ $(obj) $(LDFLAGS)
     1.6  
     1.7 +-include $(dep)
     1.8 +
     1.9  %.d: %.cc
    1.10  	@$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@
    1.11  
     2.1 --- a/src/frame.cc	Wed Oct 16 15:41:08 2013 +0300
     2.2 +++ b/src/frame.cc	Thu Oct 24 00:09:13 2013 +0300
     2.3 @@ -1,3 +1,4 @@
     2.4 +#include <GL/gl.h>
     2.5  #include <cxcore.h>
     2.6  #include <highgui.h> //TODO remove
     2.7  
     2.8 @@ -16,8 +17,33 @@
     2.9  
    2.10  Frame::Frame()
    2.11  {
    2.12 -    video_buf = cv::Mat(FREENECT_VIDEO_WIDTH, FREENECT_VIDEO_HEIGHT, CV_8UC1, 0);
    2.13 -    depth_buf = cv::Mat(FREENECT_DEPTH_WIDTH, FREENECT_DEPTH_HEIGHT, CV_16UC1, 0);
    2.14 +    video_buf = cv::Mat::zeros(KINECT_VIDEO_HEIGHT, KINECT_VIDEO_WIDTH, CV_8UC3);
    2.15 +    depth_buf = cv::Mat::zeros(KINECT_DEPTH_HEIGHT, KINECT_DEPTH_WIDTH, CV_8UC1);
    2.16 +
    2.17 +	tex_setup();
    2.18 +}
    2.19 +
    2.20 +void Frame::tex_setup()
    2.21 +{
    2.22 +	glGenTextures(1, &video_tex);
    2.23 +	glGenTextures(1, &depth_tex);
    2.24 +
    2.25 +	glBindTexture(GL_TEXTURE_2D, video_tex);
    2.26 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    2.27 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    2.28 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    2.29 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    2.30 +	glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, video_buf.size().width, video_buf.size().height,
    2.31 +			0, GL_BGRA, GL_UNSIGNED_BYTE, &video_buf);
    2.32 +
    2.33 +	glBindTexture(GL_TEXTURE_2D, depth_tex);
    2.34 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    2.35 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    2.36 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    2.37 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    2.38 +	glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, depth_buf.size().width, depth_buf.size().height,
    2.39 +			0, GL_BGRA, GL_UNSIGNED_BYTE, &depth_buf);
    2.40 +
    2.41  }
    2.42  
    2.43  void Frame::process()
    2.44 @@ -33,22 +59,77 @@
    2.45  		return;
    2.46  	}
    2.47  
    2.48 -    uint8_t* video_data = static_cast<uint8_t*>(video);
    2.49 -    frame->video_buf.data = (uchar*)video_data;
    2.50 -    //frame->video_buf.convertTo(frame->video_buf, CV_8UC1);
    2.51 -    cv::imshow("wx", frame->video_buf);
    2.52 -    cv::waitKey(100);
    2.53 +	/* freenect video data to cv mat: */
    2.54 +	unsigned char* src = (unsigned char*)video;
    2.55 +	unsigned char* dest = frame->video_buf.data;
    2.56 +
    2.57 +	for(int i=0; i<KINECT_VIDEO_HEIGHT * KINECT_VIDEO_WIDTH; i++) {
    2.58 +		dest[0] = src[2];
    2.59 +		dest[1] = src[1];
    2.60 +		dest[2] = src[0];
    2.61 +		dest += 3;
    2.62 +		src += 3;
    2.63 +	}
    2.64  	has_video = true;
    2.65  }
    2.66  
    2.67  void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time)
    2.68  {
    2.69 -	if(!depth) {
    2.70 +	if(!depth || !frame) {
    2.71  		has_depth = false;
    2.72  		return;
    2.73  	}
    2.74  
    2.75 -    uint16_t* depth_data = static_cast<uint16_t*>(depth);
    2.76 -    frame->depth_buf.data = (uchar*)depth_data;
    2.77 +	/* freenect depth data to cv mat: */
    2.78 +	unsigned char* src = (unsigned char*)depth;
    2.79 +	unsigned char* dest = frame->depth_buf.data;
    2.80 +
    2.81 +	for(int i=0; i<KINECT_DEPTH_HEIGHT * KINECT_DEPTH_WIDTH; i++) {
    2.82 +		*dest = *src;
    2.83 +		dest++;
    2.84 +		src++;
    2.85 +	}
    2.86 +
    2.87 +	save_depth_ppm(depth, 640, 480);
    2.88  	has_depth = true;
    2.89  }
    2.90 +
    2.91 +bool save_video_ppm(void *video, int w, int h)
    2.92 +{
    2.93 +	FILE *fp;
    2.94 +	if(!(fp = fopen("test_video.ppm", "wb"))) {
    2.95 +		fprintf(stderr, "Failed to open video file for writing.\n");
    2.96 +		return false;
    2.97 +	}
    2.98 +	fprintf(fp, "P6\n640 480\n255\n");
    2.99 +/*	unsigned char *ptr = (unsigned char*)video;
   2.100 +	for(int i=0; i<w * h * 3; i++) {
   2.101 +		fputc(*ptr, fp);
   2.102 +		ptr++;
   2.103 +	}
   2.104 +	fclose(fp);
   2.105 +	return true;
   2.106 +	*/
   2.107 +	return false;
   2.108 +}
   2.109 +
   2.110 +bool save_depth_ppm(void *depth, int w, int h)
   2.111 +{
   2.112 +	FILE *fp;
   2.113 +	if(!(fp = fopen("test_depth.ppm", "wb"))) {
   2.114 +		fprintf(stderr, "Failed to open depth file for writing.\n");
   2.115 +		return false;
   2.116 +	}
   2.117 +	fprintf(fp, "P6\n640 480\n255\n");
   2.118 +	unsigned char *ptr = (unsigned char*)depth;
   2.119 +	unsigned char *foo = (unsigned char*)depth;
   2.120 +	for(int i=0; i<h; i++) {
   2.121 +		for(int j=0; j<w; j++) {
   2.122 +			*foo = *ptr;
   2.123 +			fputc(foo[i * w + j], fp);
   2.124 +			ptr++;
   2.125 +		}
   2.126 +	}
   2.127 +	fclose(fp);
   2.128 +	return true;
   2.129 +}
     3.1 --- a/src/frame.h	Wed Oct 16 15:41:08 2013 +0300
     3.2 +++ b/src/frame.h	Thu Oct 24 00:09:13 2013 +0300
     3.3 @@ -8,13 +8,23 @@
     3.4  	cv::Mat depth_buf;
     3.5  	cv::Mat video_buf;
     3.6  
     3.7 +	unsigned int depth_tex;
     3.8 +	unsigned int video_tex;
     3.9 +
    3.10  	Frame();
    3.11      ~Frame(); //TODO delete Mat
    3.12  
    3.13 +	void tex_setup();
    3.14  	void process();
    3.15 +
    3.16 +	void draw();
    3.17 +	void draw_video();
    3.18 +	void draw_depth();
    3.19  };
    3.20  
    3.21  void video_cb(freenect_device *kin_dev, void *video, uint32_t time);
    3.22  void depth_cb(freenect_device *kin_dev, void *depth, uint32_t time);
    3.23  
    3.24 +bool save_video_ppm(void *video, int w, int h);
    3.25 +bool save_depth_ppm(void *depth, int w, int h);
    3.26  #endif // FRAME_H_
     4.1 --- a/src/kinect.cc	Wed Oct 16 15:41:08 2013 +0300
     4.2 +++ b/src/kinect.cc	Thu Oct 24 00:09:13 2013 +0300
     4.3 @@ -5,6 +5,22 @@
     4.4  #include "kinect.h"
     4.5  #include "frame.h"
     4.6  
     4.7 +KinectParams::KinectParams()
     4.8 +{
     4.9 +		angle = 10;
    4.10 +
    4.11 +		video_width = KINECT_VIDEO_WIDTH;
    4.12 +		video_height = KINECT_VIDEO_HEIGHT;
    4.13 +		depth_width = KINECT_DEPTH_WIDTH;
    4.14 +		depth_height = KINECT_DEPTH_HEIGHT;
    4.15 +
    4.16 +		led_color = LED_RED;
    4.17 +		video_format = FREENECT_VIDEO_RGB;
    4.18 +		depth_format = FREENECT_DEPTH_11BIT;
    4.19 +		video_res = FREENECT_RESOLUTION_MEDIUM;
    4.20 +		depth_res = FREENECT_RESOLUTION_MEDIUM;
    4.21 +}
    4.22 +
    4.23  bool init_kinect(freenect_context **kin_ctx, freenect_device **kin_dev, KinectParams *kin_params)
    4.24  {
    4.25  	if(freenect_init(kin_ctx, NULL) < 0) {
     5.1 --- a/src/kinect.h	Wed Oct 16 15:41:08 2013 +0300
     5.2 +++ b/src/kinect.h	Thu Oct 24 00:09:13 2013 +0300
     5.3 @@ -4,10 +4,10 @@
     5.4  #include <libfreenect.h>
     5.5  
     5.6  /* from specs */
     5.7 -#define FREENECT_VIDEO_WIDTH 640
     5.8 -#define FREENECT_VIDEO_HEIGHT 480
     5.9 -#define FREENECT_DEPTH_WIDTH 640
    5.10 -#define FREENECT_DEPTH_HEIGHT 480
    5.11 +#define KINECT_VIDEO_WIDTH 640
    5.12 +#define KINECT_VIDEO_HEIGHT 480
    5.13 +#define KINECT_DEPTH_WIDTH 640
    5.14 +#define KINECT_DEPTH_HEIGHT 480
    5.15  
    5.16  struct KinectParams {
    5.17  	double angle;
    5.18 @@ -23,21 +23,7 @@
    5.19  	freenect_resolution video_res;
    5.20  	freenect_resolution depth_res;
    5.21  
    5.22 -	KinectParams()
    5.23 -	{
    5.24 -		angle = 10;
    5.25 -
    5.26 -		video_width = FREENECT_VIDEO_WIDTH;
    5.27 -		video_height = FREENECT_DEPTH_HEIGHT;
    5.28 -		depth_width = FREENECT_VIDEO_WIDTH;
    5.29 -		depth_height = FREENECT_DEPTH_HEIGHT;
    5.30 -
    5.31 -		led_color = LED_RED;
    5.32 -		video_format = FREENECT_VIDEO_RGB;
    5.33 -		depth_format = FREENECT_DEPTH_11BIT;
    5.34 -		video_res = FREENECT_RESOLUTION_MEDIUM;
    5.35 -		depth_res = FREENECT_RESOLUTION_MEDIUM;
    5.36 -	}
    5.37 +	KinectParams();
    5.38  };
    5.39  
    5.40  bool init_kinect(freenect_context **kin_ctx, freenect_device **kin_dev, KinectParams *kin_params);