Debian Codegraph @

interfaces/extkinectmanager.cpp

raw symbols SCIP
1 /*
2 IanniX — a graphical real-time open-source sequencer for digital art
3 Copyright (C) 2010-2012 — IanniX Association
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "extkinectmanager.h"
20
21 ExtKinectManager::ExtKinectManager(QWidget *parent) :
22 QLabel(parent) {
23 frameCounter = 0;
24 threshMin = 0.55;
25 threshMax = 0.75;
26 kinect = 0;
27
28 //Configuration de la Kinect
29 kinect = QKinect::instance();
30 if(kinect)
31 kinect->setGreenLed();
32 bufferCam.resize(FREENECT_VIDEO_RGB_SIZE);
33 bufferDepth.resize(FREENECT_VIDEO_RGB_SIZE);
34 bufferDepth16.resize(FREENECT_FRAME_PIX);
35 for (quint16 indexNormal = 0; indexNormal <= 2047; indexNormal++)
36 rawToMeters[indexNormal] = 1.0 / (indexNormal * -0.0030711016 + 3.3309495161);
37
38 for (quint16 indexNormal = 0; indexNormal <= KINECT_PRECISION; indexNormal++) {
39 QColor color;
40 color.setHslF((qreal)indexNormal / KINECT_PRECISION / 2, 1, 0.5);
41 colorR[indexNormal] = color.red();
42 colorG[indexNormal] = color.green();
43 colorB[indexNormal] = color.blue();
44 colorK[indexNormal] = (qreal)indexNormal / KINECT_PRECISION * 255;
45 }
46
47 for (quint32 indexNormal = 0; indexNormal < FREENECT_FRAME_PIX; indexNormal++) {
48 bufferFinal[indexNormal] = 0;
49 bufferFinalOld[indexNormal] = 0;
50 }
51
52 //Configuration GUI
53 resize(320, 240);
54 setWindowFlags(Qt::Tool);
55 setWindowTitle(tr("IanniX") + QString(" - ") + tr("Kinect"));
56 move(0, 0);
57
58 labelCam = new QLabel();
59 labelCam->resize(width(), height());
60 labelCam->move(x(), 1024 - height());
61 labelCam->setWindowFlags(windowFlags());
62 labelCam->show();
63
64 timerFrame = startTimer(40);
65 timerCounter = startTimer(5000);
66
67 show();
68 }
69
70 void ExtKinectManager::timerEvent(QTimerEvent *event) {
71 if(event->timerId() == timerFrame) {
72 //Image couleur
73 if(kinect)
74 kinect->getRGB(bufferCam);
75
76 //Paramètres de traitement d'image
77 qreal depthValue = 0;
78 quint16 depthValueInt = 0;
79 QRect roi = QRect(0, 0, 640, 480);
80
81 //Buffer original
82 if(kinect)
83 kinect->getDepth16Bit(bufferDepth16);
84 frameCounter++;
85
86 //Filtrage du buffer de profondeur
87 for(quint32 x = 0 ; x < FREENECT_FRAME_W; x++) {
88 //Détection de formes
89 for(quint32 y = 0 ; y < FREENECT_FRAME_H ; y++) {
90 quint32 indexNormal = x + FREENECT_FRAME_W * y;
91
92 //Valeur linéaire et normalisation
93 depthValue = (qreal)(rawToMeters[bufferDepth16[indexNormal]] - threshMin) / (qreal)(threshMax - threshMin);
94
95 //Distribution
96 depthValueInt = depthValue * KINECT_PRECISION;
97 if((0 <= depthValue) && (depthValue <= 1)) {
98 //Image valide
99 bufferDepth[3*indexNormal+0] = colorR[depthValueInt];
100 bufferDepth[3*indexNormal+1] = colorG[depthValueInt];
101 bufferDepth[3*indexNormal+2] = colorB[depthValueInt];
102 }
103 else {
104 depthValue = rawToMeters[bufferDepth16[indexNormal]] / 6.;
105 if(depthValue > 1)
106 depthValue = 1;
107 if(depthValue < 0)
108 depthValue = 1;
109 depthValueInt = (1-depthValue) * KINECT_PRECISION;
110 //Image invalide
111 bufferDepth[3*indexNormal+0] = colorK[depthValueInt];
112 bufferDepth[3*indexNormal+1] = colorK[depthValueInt];
113 bufferDepth[3*indexNormal+2] = colorK[depthValueInt];
114 depthValue = 1;
115 }
116
117 bufferFinal[indexNormal] = bufferFinalOld[indexNormal] + ((1-depthValue) - bufferFinalOld[indexNormal] / 10.);
118 bufferFinalOld[indexNormal] = bufferFinal[indexNormal];
119 }
120 }
121
122 //Conversion en pixmap pour affichage
123 QPixmap pixmapDepth = QPixmap::fromImage(QImage(&bufferDepth[0], FREENECT_FRAME_W, FREENECT_FRAME_H, QImage::Format_RGB888)).scaledToWidth(width());
124 QPixmap pixmapCam = QPixmap::fromImage(QImage(&bufferCam[0], FREENECT_FRAME_W, FREENECT_FRAME_H, QImage::Format_RGB888)).scaledToWidth(width());
125 //Affiche les pixmaps
126 setPixmap(pixmapDepth);
127 labelCam->setPixmap(pixmapCam);
128 }
129 else if(event->timerId() == timerCounter) {
130 if(frameCounter > 0)
131 qDebug("%s", qPrintable(QString("%1 fps").arg(frameCounter/5)));
132 frameCounter = 0;
133 }
134 }
135
136 qreal ExtKinectManager::getDepthAt(qreal _x, qreal _y) const {
137 if(kinect) {
138 quint16 x = (( _x + 5.) / 10.) * FREENECT_FRAME_W;
139 quint16 y = ((-_y + 5.) / 10.) * FREENECT_FRAME_H;
140 if((x < FREENECT_FRAME_W) && (y < FREENECT_FRAME_H))
141 return bufferFinal[x + FREENECT_FRAME_W * y] / 5;
142 else
143 return 0;
144 }
145 else {
146 return (_x + 5) / 10 * 2;
147 }
148 }
149
150
151 //Paint event
152 void ExtKinectManager::paint() {
153 return;
154 //return;
155 glColor4f(1, 1, 1, 0.5);
156 for(qreal y = -5 ; y < 5 ; y += 0.1) {
157 //Dessin des lignes
158 glBegin(GL_LINE_STRIP);
159 for(qreal x = -5 ; x < 5; x += 0.1)
160 glVertex3f(x, y, getDepthAt(x, y));
161 glEnd();
162 }
163 }
164
165
166
167 void ExtKinectManager::keyPressEvent(QKeyEvent *event) {
168 if(event->modifiers() & Qt::AltModifier) {
169 if(event->key() == Qt::Key_Up)
170 threshMax += 0.05;
171 else if(event->key() == Qt::Key_Down)
172 threshMax -= 0.05;
173 }
174 else {
175 if(event->key() == Qt::Key_Up)
176 threshMin += 0.05;
177 else if(event->key() == Qt::Key_Down)
178 threshMin -= 0.05;
179 }
180 qDebug("%f %f", threshMin, threshMax);
181 }