Project

General

Profile

Kinect Scol plugin
common.h
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OpenSpace3D
4 For the latest info, see http://www.openspace3d.com
5 
6 Copyright (c) 2012 I-maginer
7 
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/copyleft/lesser.txt
21 
22 -----------------------------------------------------------------------------
23 */
24 
25 #ifndef __COMMON_H__
26 #define __COMMON_H__
27 
28 //---------------------------------------------------------------------------
29 // Includes
30 //---------------------------------------------------------------------------
31 
32 // OpenCV inlude
33 #include <opencv2/opencv.hpp>
34 
35 // Scol SDK include
36 #include <ScolPlugin.h>
37 //#include <OS_specific/windows/Objstr.h>
38 
39 #include "stdint.h"
40 
41 // OpenNI include
42 #include <OpenNI.h>
43 #include <NiTE.h>
44 
45 // Standard C++ includes
46 // includes
47 #include <boost/unordered_map.hpp>
48 #include <boost/unordered_set.hpp>
49 #include <boost/lexical_cast.hpp>
50 #include <boost/algorithm/string.hpp>
51 #include <boost/thread/thread.hpp>
52 #include <boost/bind.hpp>
53 #include <string>
54 #include <iostream>
55 #include <map>
56 #include <list>
57 #include <set>
58 #include <sstream>
59 #include <tchar.h>
60 #include <math.h>
61 
62 // Standard C includes
63 #include <cassert>
64 
65 #define SCOL_PI 3.14159265358979323846f
66 
67 // Standard C++ namespace
68 using namespace std;
69 
70 //---------------------------------------------------------------------------
71 // Class pre-declarations
72 //---------------------------------------------------------------------------
73 template <class DERIVATED_CLASS> class Singleton;
74 class Thread;
75 class DataSkeleton;
76 class Depth;
77 class Image;
78 class User;
79 class KinectDevice;
80 class KinectUser;
81 class KinectGesture;
82 
83 // Base class managing all the devices
84 class DeviceManager;
85 
86 
87 //---------------------------------------------------------------------------
88 // Boost Map pre-declarations
89 //---------------------------------------------------------------------------
90 typedef std::list<KinectUser*> KinectUserList;
91 typedef std::set<unsigned int> OpenNIUserList;
92 typedef std::map<unsigned int, KinectDevice*> KinectDeviceMap;
93 
94 
96 {
97  public:
98  Quaternion()
99  {
100  x = 0.0f;
101  y = 0.0f;
102  z = 0.0f;
103  w = 1.0f;
104  }
105 
106  float x;
107  float y;
108  float z;
109  float w;
110 };
111 
112 class Matrix3
113 {
114  public:
115 
116  Matrix3 (float fEntry00, float fEntry01, float fEntry02,
117  float fEntry10, float fEntry11, float fEntry12,
118  float fEntry20, float fEntry21, float fEntry22)
119  {
120  m[0][0] = fEntry00;
121  m[0][1] = fEntry01;
122  m[0][2] = fEntry02;
123  m[1][0] = fEntry10;
124  m[1][1] = fEntry11;
125  m[1][2] = fEntry12;
126  m[2][0] = fEntry20;
127  m[2][1] = fEntry21;
128  m[2][2] = fEntry22;
129  }
130 
131 
132  // member access, allows use of construct mat[r][c]
133  inline float* operator[] (size_t iRow) const
134  {
135  return (float*)m[iRow];
136  }
137 
138  bool operator== (const Matrix3& rkMatrix) const;
139  inline bool operator!= (const Matrix3& rkMatrix) const
140  {
141  return !operator==(rkMatrix);
142  }
143 
144  // arithmetic operations
145  Matrix3 operator+ (const Matrix3& rkMatrix) const;
146  Matrix3 operator- (const Matrix3& rkMatrix) const;
147  Matrix3 operator* (const Matrix3& rkMatrix) const;
148  Matrix3 operator- () const;
149 
150  // matrix * vector [3x3 * 3x1 = 3x1]
151  //Vector3 operator* (const Vector3& rkVector) const;
152 
153  protected:
154  float m[3][3];
155 };
156 
157 class cvTools
158 {
159  public:
160  static void DrawContour(cv::Mat &img, const vector<cv::Point> &contour, const cv::Scalar &color) {
161  vector<vector<cv::Point>> contours;
162  contours.push_back(contour);
163  drawContours(img, contours, -1, color);
164  }
165 
166  static float getAngle(cv::Point i, cv::Point j, cv::Point k)
167  {
168  float x1 = static_cast<float>(i.x);
169  float y1 = static_cast<float>(i.y);
170  float x2 = static_cast<float>(j.x);
171  float y2 = static_cast<float>(j.y);
172  float x3 = static_cast<float>(k.x);
173  float y3 = static_cast<float>(k.y);
174  float d12 = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
175  float d13 = (x3-x1)*(x3-x1) + (y3-y1)*(y3-y1);
176  float d23 = (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3);
177  float theta = acos((d13-d12-d23)/(-2*sqrt(d12)*sqrt(d23)));
178  return theta * 180.0f / 3.14159265f;
179  }
180 
181  static cv::Point2i getCentroid(cv::Point i, cv::Point j, cv::Point k)
182  {
183  int x1 = i.x;
184  int y1 = i.y;
185  int x2 = j.x;
186  int y2 = j.y;
187  int x3 = k.x;
188  int y3 = k.y;
189  int x = (x1+x2+x3)/3;
190  int y = (y1+y2+y3)/3;
191  return cv::Point2i(x, y);
192  }
193 
194  static double Convexity(const vector<cv::Point> &contour) {
195  cv::Mat contourMat(contour);
196 
197  vector<int> hull;
198  cv::convexHull(contourMat, hull);
199 
200  int n = hull.size();
201  vector<cv::Point> hullContour;
202 
203  for (int i=0; i<n; i++) {
204  hullContour.push_back(contour[hull[i]]);
205  }
206 
207  cv::Mat hullContourMat(hullContour);
208 
209  return (cv::contourArea(contourMat) / cv::contourArea(hullContourMat));
210  }
211 
212  static void MatAverage(vector<cv::Mat>& frames, cv::Mat& mean)
213  {
214  cv::Mat1d acc(mean.size());
215  cv::Mat1d frame(mean.size());
216 
217  for (unsigned int i=0; i<frames.size(); i++)
218  {
219  frames[i].convertTo(frame, CV_64FC1);
220  acc = acc + frame;
221  }
222 
223  acc = acc / frames.size();
224 
225  acc.convertTo(mean, CV_16SC1);
226  }
227 
228  static void VecAverage(vector<vector<cv::Point>>& dots, vector<cv::Point>& mean)
229  {
230  vector<cv::Point> acc;
231  vector<int> countacc;
232 
233  for (unsigned int i=0; i<dots.size(); i++)
234  {
235  for (unsigned int j=0; j<dots[i].size(); j++)
236  {
237  if (j >= acc.size())
238  {
239  acc.push_back(dots[i][j]);
240  countacc.push_back(1);
241  }
242  else
243  {
244  acc[j] = cv::Point(dots[i][j].x + acc[j].x, dots[i][j].y + acc[j].y);
245  countacc[j] = countacc[j] + 1;
246  }
247  }
248  }
249 
250  for (unsigned int i=0; i<acc.size(); i++)
251  {
252  acc[i] = cv::Point((acc[i].x / countacc[i]), (acc[i].y / countacc[i]));
253  }
254 
255  mean = acc;
256  }
257 
258  static float sqr(float a)
259  {
260  return a * a;
261  }
262 
263  static void OrderPoints(vector<vector<cv::Point>>& dots, vector<vector<cv::Point>>& mean)
264  {
265  vector<vector<cv::Point>> cdots = dots;
266  mean = cdots;
267 /*
268  if (cdots.size() > 1)
269  {
270  //TODO get the largest list, fill holes with current coords
271  vector<cv::Point> vRef = cdots[0];
272 
273  for (unsigned int i=1; i<cdots.size(); i++)
274  {
275  for (unsigned int j=0; j<vRef.size(); j++)
276  {
277  int minDist = -1;
278  int minPos = 0;
279  cv::Point refDot = vRef[j];
280 
281  for (unsigned int k=0; k<cdots[i].size() && j<cdots[i].size(); k++)
282  {
283  int ddist = sqrt(sqr(cdots[i][k].x - refDot.x) + sqr(cdots[i][k].y - refDot.y));
284  if ((ddist < minDist) || (minDist == -1))
285  {
286  minDist = ddist;
287  minPos = k;
288  }
289  }
290 
291  if ((minDist != -1) && (minPos != j))
292  {
293  mean[i].insert(mean[i].begin() + j, mean[i][minPos]);
294  mean[i].erase(mean[i].begin() + minPos);
295  j = cdots[i].erase(dots[i].begin() + minPos);
296  }
297 
298  }
299  }
300  }*/
301  }
302 
303 };
304 
305 #endif
Kinect user handling. .
Definition: KinectUser.h:38
User generator handling. .
Definition: User.h:37
Image generator handling. .
Definition: Image.h:37
Create Singleton type. .
Definition: Thread.h:37
Singleton()
Definition: Singleton.h:72
Depth generator handling. .
Definition: Depth.h:38
Kinect device handling. .
Definition: KinectDevice.h:39
Handle DeviceManager type. .
Definition: DeviceManager.h:42
Create Singleton type. .
Definition: DataSkeleton.h:38