Project

General

Profile

Kinect Scol plugin
Image.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 
33 #include "generator/Image.h"
34 #include "DeviceManager.h"
35 
36 Image::Image(KinectDevice* pDevice)
37 {
38  int width = 0;
39  int height = 0;
40  bStarted = false;
41  mParentDevice = pDevice;
42 
43  if (mColor.create(pDevice->GetDevice(), openni::SENSOR_COLOR) == openni::STATUS_OK)
44  {
45  pDevice->GetGeneratorsSize(width, height);
46  if (!SetOutputMode(width, height, 30))
47  MMechostr(MSKDEBUG, "OpenNI device : could not set output mode to %dx%d\n", width, height);
48  }
49  else
50  {
51  MMechostr(MSKDEBUG, "OpenNI device : could not create image generator\n");
52  }
53 }
54 
55 Image::Image()
56 {
57 }
58 
59 Image::~Image()
60 {
61  boost::mutex::scoped_lock l(mmutex);
62 
63  if (mColor.isValid())
64  {
65  if (bStarted)
66  mColor.stop();
67  mColor.destroy();
68  }
69 }
70 
71 bool Image::IsStarted()
72 {
73  return bStarted;
74 }
75 
76 KinectDevice* Image::GetParentDevice()
77 {
78  return mParentDevice;
79 }
80 
81 bool Image::SetOutputMode(int xRes, int yRes, int nFps)
82 {
83  openni::VideoMode vm;
84  vm.setFps(nFps);
85  vm.setResolution(xRes, yRes);
86  vm.setPixelFormat(openni::PIXEL_FORMAT_RGB888);
87 
88  width = xRes;
89  height = yRes;
90 
91  // stop the stream
92  if (bStarted)
93  {
94  mColor.stop();
95  bStarted = false;
96  }
97 
98  if (mColor.setVideoMode(vm) != openni::STATUS_OK)
99  return false;
100 
101  mData = cv::Mat(height, width, CV_8UC3);
102 
103  // start the stream
104  if (mColor.start() == openni::STATUS_OK)
105  bStarted = true;
106 
107  return true;
108 }
109 
110 bool Image::UpdateData()
111 {
112  openni::VideoStream* streams[1] = {&mColor};
113 
114  //try to start it now
115  if (!bStarted)
116  {
117  if (mColor.start() == openni::STATUS_OK)
118  bStarted = true;
119  else
120  return false;
121  }
122 
123  int idx;
124  if (mParentDevice->GetDevice().isValid() && openni::OpenNI::waitForAnyStream(streams, 1, &idx, 400) != openni::STATUS_OK)
125  return false;
126 
127  boost::mutex::scoped_lock l(mmutex);
128  if (bStarted && mColor.isValid())
129  {
130  //get the next frame
131  openni::VideoFrameRef frame;
132 
133  if (mColor.readFrame(&frame) != openni::STATUS_OK)
134  return false;
135 
136  memcpy(mData.data, frame.getData(), mData.total() * sizeof(openni::RGB888Pixel));
137  cv::cvtColor(mData, mData, CV_RGB2BGR);
138  return true;
139  }
140  return false;
141 }
142 
143 const unsigned char* Image::GetBuffer()
144 {
145  return mData.data;
146 }
147 
148 void Image::SetMirror(bool state)
149 {
150  //if (mColor.isValid())
151  //mColor.setMirroringEnabled(state);
152 }
153 
154 void Image::GetBuffer(PtrObjBitmap B)
155 {
156  cv::Mat buffer;
157  {
158  boost::mutex::scoped_lock l(mmutex);
159 
160  if (!mData.empty())
161  mData.copyTo(buffer);
162  else
163  return;
164  }
165 
166  if ((B->TailleW != width) || (B->TailleH != height))
167  return;
168 
169  //Scol bitmap are 32bits aligned
170  int sbpl = B->TailleW * 3;
171  int dbpl = B->BPL;
172 
173  if (sbpl == dbpl)
174  memcpy(B->bits, buffer.data, B->TailleW * B->TailleH * B->BytesPP);
175  else
176  for (long y=0; y<B->TailleH; y++)
177  {
178  for (long x=0; x<B->TailleW; x++)
179  {
180  unsigned long srcByte = (x * 3) + (sbpl * y);
181  unsigned long destByte = (x * B->BytesPP) + (dbpl * y);
182 
183  B->bits[destByte] = buffer.data[srcByte];
184  B->bits[destByte + 1] = buffer.data[srcByte + 1];
185  B->bits[destByte + 2] = buffer.data[srcByte + 2];
186  }
187  }
188 }
189 
190 void Image::GetGreyBuffer(PtrObjBitmap B)
191 {
192  cv::Mat buffer;
193  {
194  boost::mutex::scoped_lock l(mmutex);
195 
196  if (!mData.empty())
197  mData.copyTo(buffer);
198  else
199  return;
200  }
201 
202  if ((B->TailleW != width) || (B->TailleH != height))
203  return;
204 
205  //Scol bitmap are 32bits aligned
206  int sbpl = B->TailleW * 3;
207  int dbpl = B->BPL;
208 
209  // Browse all pixels
210  for (long y=0; y<B->TailleH; y++)
211  {
212  for (long x=0; x<B->TailleW; x++)
213  {
214  unsigned long srcByte = (x * 3) + (sbpl * y);
215  unsigned long destByte = (x * B->BytesPP) + (dbpl * y);
216  int avcolor = (buffer.data[srcByte] + buffer.data[srcByte + 1] + buffer.data[srcByte + 2]) / 3;
217  B->bits[destByte] = avcolor;
218  B->bits[destByte + 1] = avcolor;
219  B->bits[destByte + 2] = avcolor;
220  }
221  }
222 }
Kinect device handling. .
Definition: KinectDevice.h:39