0

My Kinect will be mounted on the ceiling looking downwards directly to the ground (should be paralell to ground). For object recognition i want to get the distance to the ground(maxDistance) and the distance to the object (minDistance). I wrote a loop that adds all distance values of each pixel to a list and then tried to get the Maximum int and the minimun of that list.

Unfortunately the result (that i am writing to a textbox, to check it) for zMIN and zMAX are always equally the same - which definetly is wrong.

QUESTION: What am i doing wrong? :)

List<int> tiefe = new List<int>();

        for (var y = 0; y < height; y++)  
        {  

            for (var x = 0; x < width; x++)
            { 

                var distance = GetDistance(depthdata[depthIndex], depthdata[depthIndex + 1]);
                tiefe.Add(distance); 
                depthIndex += 2;
            }
        }
        var zMAX = tiefe.Max();
        var zMin = tiefe.Min();
Nikolas Rieble
  • 2,416
  • 20
  • 43

2 Answers2

2

If you just need the min/max, and depthData is a byte array of 16 bit depth values, this would be easier and faster:

int min = int.MaxValue, max = int.MinValue;
for( int i = 0; i < depthData.Length; i += 2 )
{
    int dist = GetDistance( depthData[i], depthData[i + 1] );
    if( dist < min ) min = dist;
    if( dist > max ) max = dist;
}

Using that depthIndex variable that seems to be declared somewhere else looks dangerous. And the list is just redundant!

Chris
  • 5,442
  • 17
  • 30
  • ok, i got it. i now used the code of the loop like this http://stackoverflow.com/questions/19274287/creating-a-bitmap-of-depth-data-where-color-shows-the-distance-c-sharp, but still the output are two numbers that are exactly the same. – Nikolas Rieble Oct 18 '13 at 15:52
  • You still have `depthdata[depthIndex]` while your calculated index is called just `index`. Typo? – Chris Oct 18 '13 at 15:59
  • in the other loop (that i linked in the comment) the index is only used to color the new with a certain color. So in this case i should actually not even need it. I am confused now why i am using a loop without using x and y within the loop. Nontheless NOW i now get a result for zMax that seems legit and always 0 for zMin. – Nikolas Rieble Oct 18 '13 at 16:15
  • Yeah it's a very strange loop you have there now. What type and size is `depthdata`? – Chris Oct 18 '13 at 16:18
  • Byte[] depthdata = imageFrame.Image.Bits; – Nikolas Rieble Oct 18 '13 at 16:18
  • Width * Height bytes? x2, x4? – Chris Oct 18 '13 at 16:21
  • i dont know. should i add more code for understanding? (i can understand that this is a basic question, nontheless i dont have the knowledge to answer it) – Nikolas Rieble Oct 18 '13 at 16:30
  • Yeah it all depends on what depthData actually contains, and what `GetDistance` does. The docs seem to say there are a few different formats possible, so we'd have to see how you configure the kinect to know more. – Chris Oct 18 '13 at 16:33
  • Thank you. My mistake was to think that i have to loop through the bitmap to get the distance instead of directly accessing the byte array of depth data. I yet have to learn a lot. :) – Nikolas Rieble Oct 18 '13 at 16:39
  • Dear Chris, i dont know how to contact you otherwise, but you helped me a lot with this question and i have another question that you might just solve as well! :D please have a look at: http://stackoverflow.com/questions/19933755/how-to-get-the-position-x-y-from-a-kinect-depth-array – Nikolas Rieble Nov 13 '13 at 15:26
0

In order to compute the zMax you can search for the largest value in the depth data array, however to compute zMin you have to search for the smallest value which is larger than FLT_EPSILON (=1.192092896e-07f). The code that implements exactly what you need using the J4K Java for Kinect library is below:

public void onDepthFrameEvent(short[] packed_depth, int[] U, int V[]) {
    DepthMap map=new DepthMap(depthWidth(),depthHeight(),packed_depth);
    float zMIN=4;//The largest possible value
    float zMAX=0;//The smallest possible value
    for(int i=0;i<map.realZ.length;i++)
    {
        if(zMAX<map.realZ[i]) zMAX=map.realZ[i];
        if(map.realZ[i]>DepthMap.FLT_EPSILON && zMIN>map.realZ[i]) zMIN=map.realZ[i];
    }

}
Angelos B
  • 96
  • 3
  • Thank you for the answer, nontheless i am obliged to use c# and due to my old kinect i also have to use beta sdk 2. Yet you are right, i need zMIN which is bigger than 0(cause 0 is the value of all points too far or too close.) – Nikolas Rieble Oct 28 '13 at 11:41
  • You are welcome. As a minor comment, testing for >0 is slightly different than testing for >FLT_EPSILON, which is used in all Kinect SDK examples by Microsoft. – Angelos B Oct 28 '13 at 11:52