Search code examples
opencvblockkinectclosest

Need suggestion to find the closest block in opencv using kinect


I'm totally new to opencv, and I was recently assigned an assignment to find the center of the closest blob detected by kinect, which means that if you reach out your hands in front of kinect, then your hands become the closest blobs compared to your body.

I'm given a sample code, which can also be found online at here. So far I can only think of 2 approaches, one is using cvFindContours() to get all contours of objects, and loop though each of them and find the closest one. The problem is I don't know how to do that, because I don't know if there are functions that I can use to get each contour's depth information.

My second idea is to just loop though the depthMat matrix, and find the lowest value, which is supposed to be the closest point, and propagate out to find a block, and draw the block. The problem with this is that it takes so many calculations that my kinect video response very slowly.

Does anyone have a good suggestion on this task? Thanks.


Solution

  • You could try iterative range gating. The maximum effective range on the Kinect is about 6m, so for limited distances this might work.

    Assuming depth is in centimeters (if it's in counts, divide counts up into bins of say 10, 100 or 1000 counts whatever seems appropriate).

    Mat depthData, objectMask;
    Mat closestDepth, closestObjectMask, closestDist;
    
    // getting depth data from kinect out here...
    
    for(int i = 600; i > 0; i -= 20)
    {
        threshold(depthData, objectMask, (double)i, 1, THRESH_BINARY_INV);
        // gated out all objects (may need to adjust pixel count for best results)
        Scalar pixelCount = sum(objectMask);
        if(pixelCount.val[0] < 1.0) 
        {
            break;
        }
    
        closestDepth = depthData; // last good depth data
        closestObjectMask = objectMask; // last good object mask
        closestDist = i; // last closest distance
    }
    

    Once that loop breaks out, you should have the following:

    • closestDepth - should be depth image to use to get actual object distance by multiplying it with the mask.
    • closestDist - should be the distance of the range gate before "black out"
    • closestObjectMask - should be a mask of the closest object (track it with largest contour or something similar)

    Hope that's helpful!