33 #include "generator/Image.h" 34 #include "DeviceManager.h" 41 mParentDevice = pDevice;
43 if (mColor.create(pDevice->GetDevice(), openni::SENSOR_COLOR) == openni::STATUS_OK)
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);
51 MMechostr(MSKDEBUG,
"OpenNI device : could not create image generator\n");
61 boost::mutex::scoped_lock l(mmutex);
71 bool Image::IsStarted()
81 bool Image::SetOutputMode(
int xRes,
int yRes,
int nFps)
85 vm.setResolution(xRes, yRes);
86 vm.setPixelFormat(openni::PIXEL_FORMAT_RGB888);
98 if (mColor.setVideoMode(vm) != openni::STATUS_OK)
101 mData = cv::Mat(height, width, CV_8UC3);
104 if (mColor.start() == openni::STATUS_OK)
110 bool Image::UpdateData()
112 openni::VideoStream* streams[1] = {&mColor};
117 if (mColor.start() == openni::STATUS_OK)
124 if (mParentDevice->GetDevice().isValid() && openni::OpenNI::waitForAnyStream(streams, 1, &idx, 400) != openni::STATUS_OK)
127 boost::mutex::scoped_lock l(mmutex);
128 if (bStarted && mColor.isValid())
131 openni::VideoFrameRef frame;
133 if (mColor.readFrame(&frame) != openni::STATUS_OK)
136 memcpy(mData.data, frame.getData(), mData.total() *
sizeof(openni::RGB888Pixel));
137 cv::cvtColor(mData, mData, CV_RGB2BGR);
143 const unsigned char* Image::GetBuffer()
148 void Image::SetMirror(
bool state)
154 void Image::GetBuffer(PtrObjBitmap B)
158 boost::mutex::scoped_lock l(mmutex);
161 mData.copyTo(buffer);
166 if ((B->TailleW != width) || (B->TailleH != height))
170 int sbpl = B->TailleW * 3;
174 memcpy(B->bits, buffer.data, B->TailleW * B->TailleH * B->BytesPP);
176 for (
long y=0; y<B->TailleH; y++)
178 for (
long x=0; x<B->TailleW; x++)
180 unsigned long srcByte = (x * 3) + (sbpl * y);
181 unsigned long destByte = (x * B->BytesPP) + (dbpl * y);
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];
190 void Image::GetGreyBuffer(PtrObjBitmap B)
194 boost::mutex::scoped_lock l(mmutex);
197 mData.copyTo(buffer);
202 if ((B->TailleW != width) || (B->TailleH != height))
206 int sbpl = B->TailleW * 3;
210 for (
long y=0; y<B->TailleH; y++)
212 for (
long x=0; x<B->TailleW; x++)
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;
Kinect device handling. .