While working with the kinect I found out, that the bitmap and its depth information are unreliable and for some reason much more disturbed than the data from the actual byte array.
I realised this when I tried get the min and max by accessing the bitmap like this
for (var y = 0; y < height; y++)
{
var heightOffset = y * width;
for (var x = 0; x < width; x++)
{
var index = ((width - x - 1) + heightOffset) * 4;
var distance = GetDistance(depth[depthIndex], depth[depthIndex + 1]);
But on the other hand I achieved much better results when I directly accessed the depth byte array (as a Stackoverflow member Chris pointed out, thank you, Chris) with this method:
int min2 = int.MaxValue, max2 = int.MinValue;
for (int i = 0; i < depth.Length; i += 2)
{
int dist = GetDistance(depth[i], depth[i + 1]);
if (dist < min2 && dist > 0)
min2 = dist;
if (dist > max2)
max2 = dist;
}
My question is:
- I want to count all the points within a certain distance intervall.
- Then I want to get their actual position (x,y).
How can I get the position or directly add those points to a list - for further action?
My ultimate goal is to identify certain predefined objects by counting the number of pixels within certain intervall (to distinct a ball from a block for example).
for (int i = 0; i < depth.Length; i += 2)
{
int dist = GetDistance(depth[i], depth[i + 1]);
if (dist < z2 && dist > z1)
a++;
}
Assuming depth
is a byte array with 2 * width
bytes per row, and height
rows, try something like this:
var points = new List<Point>();
for( int y = 0; y < height; y++ )
for( int x = 0; x < width; x++ )
{
int i = y * width * 2 + x * 2;
int dist = GetDistance( depth[i], depth[i + 1] );
if( dist < z2 && dist > z1 )
points.Add( new Point( x, y ) );
}
You'll obviously have to replace Point
with some type to represent an int x/y pair!
You could also do it with a simple for (int i = 0; i < depth.Length; i += 2)
as before, and then calculate the x/y values from i
. (Hint: the modulus operator will give you the x value, and a simple integer division will get you y, more or less)
来源:https://stackoverflow.com/questions/19933755/how-to-get-the-position-x-y-from-a-kinect-depth-array