How to get OpenCV (EmguCV) Image from Primesense Kinect RGB camera

Previous article shows how to  construct OpenCV Image from data comming from Kinect Depth camera using Primesense drivers. This article shows the same for Kinect RGB camera. Instead of constructing Image we will construct RGB Image .

Solution

The code  takes RGB map from _rgbGenerator (OpenNI.ImageGenerator) and calculates RGB image (new Image<Bgr, Byte>(width, height)). Code presented uses unsafe sequences and array pointers to maximize the speed (you need to set up compiler with unsafe option).

Working C# code sample

This code is part of TouchTable project.

private unsafe void processDepthFrame()
{
var depthMd = new DepthMetaData();
// process pDepth
_depthGenerator.GetMetaData(depthMd);
var pDepth = (ushort*)_depthGenerator.DepthMapPtr.ToPointer();
ushort* pCurrentDepth = pDepth;
var width = depthMd.XRes;
var height = depthMd.YRes;
depthImage = new Image<Gray, Byte>(width, height);
var data = depthImage.Data;
var minimalFingerHeight = Config.MinimalFingerHeight;
fixed (int* pEnvDistances = _envDistances)
{
fixed (int* pTableDistances = TableDistancesYX)
{
fixed (byte* pImageData = data)
{
var pCurrEnvDistance = pEnvDistances;
var pCurrTableDistance = pTableDistances;
var pCurrImageData = pImageData;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++, pCurrentDepth += 1, pCurrEnvDistance++, pCurrTableDistance++, pCurrImageData++)
{
var distFromKinect = *pCurrentDepth;
// Remember values
if (saveEnvDistances)
{
*pCurrEnvDistance = distFromKinect;
}
int distFromTable = (*pCurrEnvDistance – distFromKinect);
*pCurrTableDistance = distFromTable;
if (distFromTable > minimalFingerHeight)
{
// We will make pixel gray if it is considered a hand pixel
*pCurrImageData = 128;
}
}
}
}
}
}
}

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

1 Comment so far