direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
glwidget.cpp
Go to the documentation of this file.
1 #include "glwidget.h"
2 
3 
4 GLWidget::GLWidget(QWidget *_parent) : QGLWidget(_parent)
5 {
6  // setMinimumSize(640,480);
7  // re-size the widget to that of the parent (in this case the GLFrame passed in on construction)
8  this->resize(_parent->size());
9 
10  // start the timer to re-draw as quick as possible
11  startTimer(50); // each 500 ms
12 
13  // create space for our image data from the Kinect
14  m_rgb.resize(640*480*3);
15 }
16 
17 
19 {
20  glClearColor(0.0f,0.0f,0.0f,1.0f);
21 }
22 
23 
25 {
26  //
27  // get picture data from kinect class
28  //
29  QKinect *kinect=QKinect::instance();
30 /*
31  if(m_mode ==0)
32  {
33 */
34  kinect->getRGB(m_rgb);
35 /*
36  }
37  else if(m_mode == 1)
38  {
39  kinect->getDepth(m_rgb);
40  }
41 */
42 
43  cv::Mat depthMat(cv::Size(640,480), CV_16UC1);
44  cv::Mat rgbMat(cv::Size(640,480), CV_8UC3, cv::Scalar(0));
45 
46  //copy to ocv_buf..
47  memcpy(rgbMat.data, &m_rgb[0], 640*480*3);
48 
49  // convert to qimate
50  qframe = mat2qimage(rgbMat);
51 
52  // qframe = QImage((const unsigned char*)(&m_rgb[0]), 640, 480, QImage::Format_RGB888);
53 
54 
55  glClear (GL_COLOR_BUFFER_BIT);
56  glClearColor (0.0,0.0,0.0,1.0);
57 
58  if (!qframe.isNull())
59  {
60  qframe = qframe.scaled(this->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
61  // you can use glDrawPixels directly
62  // glDrawPixels(qframe.width(),qframe.height(), GL_RGBA, GL_UNSIGNED_BYTE, qframe.bits());
63  // or do 2D texture mapping
64  glDisable(GL_DEPTH_TEST);
65  glMatrixMode(GL_PROJECTION);
66  glLoadIdentity();
67  gluOrtho2D(0,qframe.width(),qframe.height(),0);
68  glMatrixMode(GL_MODELVIEW);
69  glLoadIdentity();
70  glEnable(GL_TEXTURE_2D);
71  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
72  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
73  glTexImage2D( GL_TEXTURE_2D, 0, 4, qframe.width(), qframe.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, qframe.bits() );
74  glBegin(GL_QUADS);
75  glTexCoord2f(0,0); glVertex2f(0,qframe.height());
76  glTexCoord2f(0,1); glVertex2f(0,0);
77  glTexCoord2f(1,1); glVertex2f(qframe.width(),0);
78  glTexCoord2f(1,0); glVertex2f(qframe.width(),qframe.height());
79  glEnd();
80  glDisable(GL_TEXTURE_2D);
81  // .... end
82 
83  // some example of alpha blending
84  //glEnable(GL_DEPTH_TEST);
85  //glEnable(GL_BLEND);
86  //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
87  //glColor4f(0.0f,1.0f,1.0f, 0.9f);
88  glFlush();
89  }
90 }
91 
92 
93 void GLWidget::resizeGL(int w, int h)
94 {
95  glViewport (0, 0, (GLsizei)w, (GLsizei)h);
96  glMatrixMode (GL_PROJECTION);
97  glLoadIdentity ();
98  gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
99  glMatrixMode (GL_MODELVIEW);
100  // qDebug() << "Resizing...";
101 }
102 
103 
104 void GLWidget::sendImage(cv::Mat* img)
105 {
106  qframe = QImage((const unsigned char*)(img->data), img->cols, img->rows, img->step, QImage::Format_RGB888).rgbSwapped();
107  qframe = QGLWidget::convertToGLFormat(qframe);
108  this->updateGL();
109 
110 }
111 
112 
113 cv::Mat GLWidget::qimage2mat(const QImage& qimage)
114 {
115  cv::Mat mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC4, (uchar*)qimage.bits(), qimage.bytesPerLine());
116  cv::Mat mat2 = cv::Mat(mat.rows, mat.cols, CV_8UC3 );
117  int from_to[] = { 0,0, 1,1, 2,2 };
118  cv::mixChannels( &mat, 1, &mat2, 1, from_to, 3 );
119 
120  return mat2;
121 }
122 
123 
124 QImage GLWidget::mat2qimage(const cv::Mat& mat)
125 {
126  cv::Mat rgb;
127  cv::cvtColor(mat, rgb, CV_BGR2RGB);
128 
129  return QImage((const unsigned char*)(rgb.data), rgb.cols, rgb.rows, QImage::Format_RGB888);
130 }
131 
132 
134 {
135  QImage qimage = QImage((const unsigned char*)(&m_rgb[0]), 640, 480, QImage::Format_RGB888);
136 
137  // - - - - - - - -
138  // - - - - - - - -
139  // - - - - - - - -
140 
141 
142  // grab image from the kinect frame in the GUI
143 // QImage qimage = ui.frameDepth->grabFrameBuffer();
144 
145  // convert QImage to OpeneCV's cv::Mat
146  //
147  // http://permalink.gmane.org/gmane.comp.lib.opencv/37800
148  //
149  //Mat qimage2mat(const QImage& qimage) {
150 
151  cv::Mat mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC4, (uchar*)qimage.bits(), qimage.bytesPerLine());
152  cv::Mat mat2 = cv::Mat(mat.rows, mat.cols, CV_8UC3 );
153  int from_to[] = { 0,0, 1,1, 2,2 };
154  cv::mixChannels( &mat, 1, &mat2, 1, from_to, 3 );
155 
156  // result is now in 'mat2'
157 
158  mImage.release(); // reset picture
159 
160  // depth image from Kinect now! (mat2)
161  cv::cvtColor( mat2, mImage, CV_BGR2RGB);
162 
163 // QImage tmp( (uchar*)mImage.data, mImage.cols, mImage.rows, mImage.step, QImage::Format_RGB888 );
164 // ui.lblOpenCV->setPixmap( QPixmap::fromImage( tmp ) );
165 
166 
167  if (mImage.data)
168  {
169  cv::Mat gray;
170  cv::cvtColor( mImage, gray, CV_RGB2GRAY );
171  cv::Canny( gray, gray, 10, 30, 3 );
172 
173  // convert b/w Mat to QImage
174 // QImage tmp( (uchar*)gray.data, gray.cols, gray.rows, gray.step, QImage::Format_Indexed8 );
175 
176  // send image to GUI
177 // ui.lblOpenCV->setPixmap( QPixmap::fromImage( tmp ) );
178  }
179 }
180 
181 
182 void GLWidget::timerEvent(QTimerEvent *_event)
183 {
184  // re-draw GL
185  updateGL();
186 }