KINECT STATS GENERATOR FOR SPORTS VISUALISATION
1.0
|
00001 /* 00002 * This file is part of the OpenKinect Project. http://www.openkinect.org 00003 * 00004 * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file 00005 * for details. 00006 * 00007 * This code is licensed to you under the terms of the Apache License, version 00008 * 2.0, or, at your option, the terms of the GNU General Public License, 00009 * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, 00010 * or the following URLs: 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * http://www.gnu.org/licenses/gpl-2.0.txt 00013 * 00014 * If you redistribute this file in source form, modified or unmodified, you 00015 * may: 00016 * 1) Leave this header intact and distribute it under the same terms, 00017 * accompanying it with the APACHE20 and GPL20 files, or 00018 * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or 00019 * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file 00020 * In all cases you must keep the copyright notice intact and include a copy 00021 * of the CONTRIB file. 00022 * 00023 * Binary distributions must follow the binary distribution requirements of 00024 * either License. 00025 */ 00026 00027 //---------------------------------------------------------------------------------------------------------------------- 00031 //---------------------------------------------------------------------------------------------------------------------- 00032 00033 #ifndef QKINECT_H__ 00034 #define QKINECT_H__ 00035 #include <QObject> 00036 #include <QThread> 00037 #include <stdexcept> 00038 #include <QMutex> 00039 #include <QMutexLocker> 00040 #include <vector> 00041 #include <libfreenect.h> 00042 #include "opencv2/opencv.hpp" 00043 00044 //---------------------------------------------------------------------------------------------------------------------- 00051 //---------------------------------------------------------------------------------------------------------------------- 00052 00053 class QKinectProcessEvents : public QThread 00054 { 00055 public : 00056 //---------------------------------------------------------------------------------------------------------------------- 00059 //---------------------------------------------------------------------------------------------------------------------- 00060 inline QKinectProcessEvents( 00061 freenect_context *_ctx 00062 ) 00063 {m_ctx=_ctx;} 00064 //---------------------------------------------------------------------------------------------------------------------- 00067 //---------------------------------------------------------------------------------------------------------------------- 00068 inline void setActive(){m_active=true;} 00069 //---------------------------------------------------------------------------------------------------------------------- 00072 //---------------------------------------------------------------------------------------------------------------------- 00073 inline void setInActive(){m_active=false;} 00074 protected : 00075 //---------------------------------------------------------------------------------------------------------------------- 00078 //---------------------------------------------------------------------------------------------------------------------- 00079 void run(); 00080 00081 private : 00082 //---------------------------------------------------------------------------------------------------------------------- 00085 //---------------------------------------------------------------------------------------------------------------------- 00086 bool m_active; 00087 //---------------------------------------------------------------------------------------------------------------------- 00090 //---------------------------------------------------------------------------------------------------------------------- 00091 freenect_context *m_ctx; 00092 }; 00093 00094 //---------------------------------------------------------------------------------------------------------------------- 00103 //---------------------------------------------------------------------------------------------------------------------- 00104 00105 class KinectInterface : public QObject 00106 { 00107 // must include this macro so we inherit all the core QT features such as singnals and slots 00108 Q_OBJECT 00109 public : 00110 00111 //---------------------------------------------------------------------------------------------------------------------- 00117 //---------------------------------------------------------------------------------------------------------------------- 00118 static KinectInterface *instance(); 00119 //---------------------------------------------------------------------------------------------------------------------- 00122 //---------------------------------------------------------------------------------------------------------------------- 00123 inline void setUserDeviceNumber(int _m) {m_userDeviceNumber=_m;} 00124 //---------------------------------------------------------------------------------------------------------------------- 00126 //---------------------------------------------------------------------------------------------------------------------- 00127 void shutDownKinect(); 00128 00129 //---------------------------------------------------------------------------------------------------------------------- 00131 //---------------------------------------------------------------------------------------------------------------------- 00132 void setBoxObject(int _topX,int _topY, int _width, int _height); 00133 00134 //---------------------------------------------------------------------------------------------------------------------- 00136 //---------------------------------------------------------------------------------------------------------------------- 00137 void setBoundsDefined(bool _isBoundsDefined); 00138 00139 //---------------------------------------------------------------------------------------------------------------------- 00141 //---------------------------------------------------------------------------------------------------------------------- 00142 cv::Rect& getBoxObject(); 00143 00144 //---------------------------------------------------------------------------------------------------------------------- 00146 //---------------------------------------------------------------------------------------------------------------------- 00147 void toggleTracking(); 00148 00149 //---------------------------------------------------------------------------------------------------------------------- 00151 //---------------------------------------------------------------------------------------------------------------------- 00152 bool getTrackingFlag(); 00153 00154 //---------------------------------------------------------------------------------------------------------------------- 00157 //---------------------------------------------------------------------------------------------------------------------- 00158 bool getBounds(); 00159 00160 00161 public slots : 00162 00163 //---------------------------------------------------------------------------------------------------------------------- 00169 00170 //---------------------------------------------------------------------------------------------------------------------- 00171 void setVideoMode( 00172 int _mode 00173 ); 00174 00175 //---------------------------------------------------------------------------------------------------------------------- 00179 //---------------------------------------------------------------------------------------------------------------------- 00180 bool getDepth(cv::Mat &o_buffer 00181 ); 00182 //---------------------------------------------------------------------------------------------------------------------- 00186 //---------------------------------------------------------------------------------------------------------------------- 00187 bool getDepthRaw( 00188 cv::Mat &o_buffer, float *o_depthinmBuffer 00189 ); 00190 //---------------------------------------------------------------------------------------------------------------------- 00194 //---------------------------------------------------------------------------------------------------------------------- 00195 bool getDepth16Bit(cv::Mat &o_buffer 00196 ); 00197 00198 00199 bool getDepthSliced(cv::Mat &o_buffer 00200 ); 00201 //---------------------------------------------------------------------------------------------------------------------- 00204 //---------------------------------------------------------------------------------------------------------------------- 00205 bool getRGB(cv::Mat &o_buffer ); 00206 //---------------------------------------------------------------------------------------------------------------------- 00208 //---------------------------------------------------------------------------------------------------------------------- 00209 void startDepth(); 00210 //---------------------------------------------------------------------------------------------------------------------- 00212 //---------------------------------------------------------------------------------------------------------------------- 00213 void stopDepth(); 00214 //---------------------------------------------------------------------------------------------------------------------- 00216 //---------------------------------------------------------------------------------------------------------------------- 00217 void startVideo(); 00218 //---------------------------------------------------------------------------------------------------------------------- 00220 //---------------------------------------------------------------------------------------------------------------------- 00221 void stopVideo(); 00222 //---------------------------------------------------------------------------------------------------------------------- 00225 //---------------------------------------------------------------------------------------------------------------------- 00226 void toggleVideoState( 00227 bool _mode 00228 ); 00229 //---------------------------------------------------------------------------------------------------------------------- 00232 //---------------------------------------------------------------------------------------------------------------------- 00233 void toggleDepthState( 00234 bool _mode 00235 ); 00236 //---------------------------------------------------------------------------------------------------------------------- 00239 //---------------------------------------------------------------------------------------------------------------------- 00240 inline freenect_context *getContext(){return m_ctx;} 00241 00242 00243 void setDepthVolumeLower(double _inputLowerDepth); 00244 00245 00246 void setDepthVolumeHigher(double _inputHigherDepth); 00247 00248 00249 00250 00251 private : 00252 //---------------------------------------------------------------------------------------------------------------------- 00254 //---------------------------------------------------------------------------------------------------------------------- 00255 KinectInterface(); 00256 //---------------------------------------------------------------------------------------------------------------------- 00258 //---------------------------------------------------------------------------------------------------------------------- 00259 ~KinectInterface(); 00260 //---------------------------------------------------------------------------------------------------------------------- 00262 //---------------------------------------------------------------------------------------------------------------------- 00263 Q_DISABLE_COPY(KinectInterface) 00264 //---------------------------------------------------------------------------------------------------------------------- 00267 //---------------------------------------------------------------------------------------------------------------------- 00268 bool init(); 00269 //---------------------------------------------------------------------------------------------------------------------- 00271 //---------------------------------------------------------------------------------------------------------------------- 00272 static KinectInterface *s_instance; 00273 //---------------------------------------------------------------------------------------------------------------------- 00275 //---------------------------------------------------------------------------------------------------------------------- 00276 freenect_context *m_ctx; 00277 //---------------------------------------------------------------------------------------------------------------------- 00279 //---------------------------------------------------------------------------------------------------------------------- 00280 freenect_device *m_dev; 00281 //---------------------------------------------------------------------------------------------------------------------- 00283 //---------------------------------------------------------------------------------------------------------------------- 00284 int m_userDeviceNumber; 00285 00286 //---------------------------------------------------------------------------------------------------------------------- 00292 //---------------------------------------------------------------------------------------------------------------------- 00293 cv::Mat m_bufferDepth; 00294 //---------------------------------------------------------------------------------------------------------------------- 00297 //---------------------------------------------------------------------------------------------------------------------- 00298 cv::Mat m_bufferVideo,m_nextBuffer,m_prevBuffer, m_diffBuffer, m_bufferVideoGray; 00299 //---------------------------------------------------------------------------------------------------------------------- 00304 //---------------------------------------------------------------------------------------------------------------------- 00305 cv::Mat m_bufferDepthRaw; 00306 00307 float m_bufferDepthinM[640 * 480]; 00308 00309 //std::map<int, float[640*480]> m_originalFrameDepth1; 00310 //---------------------------------------------------------------------------------------------------------------------- 00313 //---------------------------------------------------------------------------------------------------------------------- 00314 cv::Mat m_bufferDepthRaw16; 00315 00316 //---------------------------------------------------------------------------------------------------------------------- 00319 //---------------------------------------------------------------------------------------------------------------------- 00320 std::vector<float> m_gamma; 00321 //---------------------------------------------------------------------------------------------------------------------- 00323 //---------------------------------------------------------------------------------------------------------------------- 00324 bool m_newRgbFrame; 00325 //---------------------------------------------------------------------------------------------------------------------- 00327 //---------------------------------------------------------------------------------------------------------------------- 00328 bool m_newDepthFrame; 00329 //---------------------------------------------------------------------------------------------------------------------- 00331 //---------------------------------------------------------------------------------------------------------------------- 00332 bool m_stopDevice; 00333 //---------------------------------------------------------------------------------------------------------------------- 00335 //---------------------------------------------------------------------------------------------------------------------- 00336 bool m_deviceActive; 00337 00338 //---------------------------------------------------------------------------------------------------------------------- 00340 //---------------------------------------------------------------------------------------------------------------------- 00341 unsigned int m_resolutionRGBBytes; 00342 //---------------------------------------------------------------------------------------------------------------------- 00344 //---------------------------------------------------------------------------------------------------------------------- 00345 unsigned int m_resolutionDepthBytes; 00346 00347 //---------------------------------------------------------------------------------------------------------------------- 00349 //---------------------------------------------------------------------------------------------------------------------- 00350 QKinectProcessEvents *m_process; 00351 //---------------------------------------------------------------------------------------------------------------------- 00354 //---------------------------------------------------------------------------------------------------------------------- 00355 QMutex m_mutex; 00356 00357 00358 int m_threshValue; 00359 00360 double m_depthLower, m_depthHigher; 00361 00362 cv::Rect m_selectedBoxCoords; 00363 00364 bool m_toggleTracking,m_setBounds; 00365 00366 00367 //---------------------------------------------------------------------------------------------------------------------- 00372 //---------------------------------------------------------------------------------------------------------------------- 00373 void depthFunc( 00374 freenect_device *_dev, 00375 void *o_depth, 00376 uint32_t _timestamp 00377 ); 00378 //---------------------------------------------------------------------------------------------------------------------- 00383 //---------------------------------------------------------------------------------------------------------------------- 00384 void rgbFunc( 00385 freenect_device *_dev, 00386 void *o_rgb, 00387 uint32_t _timestamp 00388 ); 00389 00390 00391 //---------------------------------------------------------------------------------------------------------------------- 00397 //---------------------------------------------------------------------------------------------------------------------- 00398 void grabDepth( 00399 void *_depth, 00400 uint32_t timestamp 00401 ); 00402 //---------------------------------------------------------------------------------------------------------------------- 00408 //---------------------------------------------------------------------------------------------------------------------- 00409 void grabVideo( 00410 void *_video, 00411 uint32_t timestamp 00412 ); 00413 //---------------------------------------------------------------------------------------------------------------------- 00419 //---------------------------------------------------------------------------------------------------------------------- 00420 static inline void depthCallback( 00421 freenect_device *_dev, 00422 void *_depth, 00423 uint32_t _timestamp=0 00424 ) 00425 { 00426 Q_UNUSED(_dev); 00427 00429 KinectInterface *kinect=KinectInterface::instance(); 00431 kinect->grabDepth(_depth,_timestamp); 00432 } 00433 //---------------------------------------------------------------------------------------------------------------------- 00439 //---------------------------------------------------------------------------------------------------------------------- 00440 00441 static inline void videoCallback( 00442 freenect_device *_dev, 00443 void *_video, 00444 uint32_t _timestamp=0 00445 ) 00446 { 00447 Q_UNUSED(_dev); 00448 00450 KinectInterface *kinect=KinectInterface::instance(); 00452 kinect->grabVideo(_video, _timestamp); 00453 } 00454 00455 00456 00457 00458 }; 00459 00460 00461 00462 #endif