Kinect Scol plugin
Depth.cpp
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 
32 #include "generator/Depth.h"
33 #include "generator/User.h"
34 #include "DeviceManager.h"
35 
36 Depth::Depth(KinectDevice* pDevice)
37 {
38  mData = 0;
39  mReadData = 0;
40  mDepthHist = 0;
41  mReadDepthHist = 0;
42  mCuttedData = 0;
43 
44  iDistCutoff = 10000;
45  fAngCutoff = 0.0f;
46  mParentDevice = pDevice;
47 
48  int width = 0;
49  int height = 0;
50 }
51 
52 Depth::Depth()
53 {
54 }
55 
56 Depth::~Depth()
57 {
58  boost::mutex::scoped_lock l(mmutex);
59 
60  if (mData)
61  free(mData);
62 
63  if (mCuttedData)
64  free(mCuttedData);
65 
66  if (mReadData)
67  free(mReadData);
68 
69  if (mDepthHist)
70  free(mDepthHist);
71 
72  if (mReadDepthHist)
73  free(mReadDepthHist);
74 }
75 
76 KinectDevice* Depth::GetParentDevice()
77 {
78  return mParentDevice;
79 }
80 
81 bool Depth::UpdateData(openni::VideoFrameRef frame)
82 {
83  boost::mutex::scoped_lock l(mmutex);
84 
85  if (!frame.isValid())
86  return false;
87 
88  // init buffers
89  if (mData == 0)
90  {
91  width = frame.getVideoMode().getResolutionX();
92  height = frame.getVideoMode().getResolutionY();
93 
94  unsigned int dataSize = width * height * sizeof(openni::DepthPixel);
95  unsigned int historySize = MAX_DEPTH * sizeof(float);
96 
97  mData = (unsigned short*)malloc(dataSize);
98  memset(mData, 0, dataSize);
99 
100  mCuttedData = (unsigned short*)malloc(dataSize);
101  memset(mCuttedData, 0, dataSize);
102 
103  mReadData = (unsigned short*)malloc(dataSize);
104  memset(mReadData, 0, dataSize);
105 
106  mDepthHist = (float*)malloc(historySize);
107  memset(mDepthHist, 0, historySize);
108 
109  mReadDepthHist = (float*)malloc(historySize);
110  memset(mReadDepthHist, 0, historySize);
111  }
112 
113  /*
114  if (mDepth.readFrame(&frame) != openni::STATUS_OK)
115  return false;
116  */
117 
118  memcpy(mData, frame.getData(), width * height * sizeof(openni::DepthPixel));
119  memcpy(mCuttedData, frame.getData(), width * height * sizeof(openni::DepthPixel));
120  DepthCutoff();
121 
122  const openni::DepthPixel* pDepth = mData;
123 
124  // Calculate the accumulative histogram
125  memset(mDepthHist, 0, MAX_DEPTH * sizeof(float));
126 
127  unsigned int nNumberOfPoints = 0;
128  for (int y = 0; y < frame.getHeight(); ++y)
129  {
130  for (int x = 0; x < frame.getWidth(); ++x, ++pDepth)
131  {
132  if (*pDepth != 0)
133  {
134  mDepthHist[*pDepth]++;
135  nNumberOfPoints++;
136  }
137  }
138  }
139  for (int nIndex = 1; nIndex < MAX_DEPTH; nIndex++)
140  {
141  mDepthHist[nIndex] += mDepthHist[nIndex-1];
142  }
143  if (nNumberOfPoints)
144  {
145  for (int nIndex=1; nIndex < MAX_DEPTH; nIndex++)
146  {
147  mDepthHist[nIndex] = 256.0f * (1.0f - (mDepthHist[nIndex] / nNumberOfPoints));
148  }
149  }
150  return true;
151 }
152 
153 const unsigned short* Depth::GetBuffer()
154 {
155  return mData;
156 }
157 
158 const unsigned short* Depth::GetCuttedBuffer()
159 {
160  return mCuttedData;
161 }
162 
163 void Depth::GetBuffer(PtrObjBitmap B)
164 {
165  // Get depth map
166  {
167  boost::mutex::scoped_lock l(mmutex);
168  if (mData && mDepthHist && mReadData && mReadDepthHist)
169  {
170  memcpy(mReadData, mData, width * height * sizeof(openni::DepthPixel));
171  memcpy(mReadDepthHist, mDepthHist, MAX_DEPTH * sizeof(float));
172  }
173  else
174  return;
175  }
176 
177  // Test bitmap size
178  if ((B->TailleW != width) || (B->TailleH != height))
179  {
180  return;
181  }
182 
183  //bool isMirror = KinectOBJ->GetMirrorMode();
184 
185  // Copy data to Scol buffer
186  int x = 0, y = 0;
187  unsigned int destByte = 0;
188  int color = 0;
189  const openni::DepthPixel* pDepth = mReadData;
190  for (y=0; y<height; y++)
191  {
192  const openni::DepthPixel* pDepthRow = pDepth;
193  for (x=0; x<width; x++, ++pDepthRow)
194  {
195  int color = 0;
196  if (*pDepthRow != 0)
197  color = (int)mReadDepthHist[*pDepthRow];
198 
199  destByte = (x + y * width) * 3;
200 
201  B->bits[destByte] = color;
202  B->bits[destByte+1] = color;
203  B->bits[destByte+2] = color;
204  }
205 
206  pDepth += width;
207  }
208 }
209 
210 bool Depth::IsValid()
211 {
212  return mData != 0 ? true : false;
213 }
214 
215 void Depth::SetMirror(bool state)
216 {
217  /*
218  if (mDepth.isValid())
219  mDepth.setMirroringEnabled(state);
220 */
221 }
222 
223 void Depth::GetDepthSize(int &w, int &h)
224 {
225  w = width;
226  h = height;
227 }
228 
229 void Depth::SetDistCutoff(int dist)
230 {
231  iDistCutoff = dist;
232 }
233 
234 int Depth::GetDistCutoff()
235 {
236  return iDistCutoff;
237 }
238 
239 void Depth::SetAngCutoff(float ang)
240 {
241  fAngCutoff = ang;
242 }
243 
244 float Depth::GetAngCutoff()
245 {
246  return fAngCutoff;
247 }
248 
249 void Depth::DepthCutoff()
250 {
251  openni::DepthPixel val2Replace = 0;
252  int xcut = width;
253 
254  //Kinect IR camera fov 57.8
255  if ((fAngCutoff > 0.0f) && (fAngCutoff < 57.7f))
256  xcut = (int)(((float)width / 57.8f) * fAngCutoff);
257 
258  int minxcut = (width / 2) - (xcut / 2);
259  int maxxcut = (width / 2) + (xcut / 2);
260  for(int y(0); y < height; ++y)
261  {
262  for(int x(0); x < width; ++x)
263  {
264  int pos = x+y*width;
265 
266  if((x < minxcut) || (x > maxxcut) || (iDistCutoff < mCuttedData[pos]))
267  mCuttedData[pos] = val2Replace;
268  }
269  }
270 }
Kinect device handling. .
Definition: KinectDevice.h:39