how to get depth values and then convert to millimetres in OpenCV with Kinect?

user1361518 picture user1361518 · Apr 27, 2012 · Viewed 7.7k times · Source

I have VS2010 running with OpenCV 3.2.1 with kniect.

i'm using the tutorial from here

int main( int argc, char* argv[] ){

VideoCapture capture(CV_CAP_OPENNI); // or CV_CAP_OPENNI
for(;;)
{
    Mat depthMap;
    Mat bgrImage;

    capture.grab();

    capture.retrieve( depthMap, CV_CAP_OPENNI_DEPTH_MAP ); // Depth values in mm (CV_16UC1)
    capture.retrieve( bgrImage, CV_CAP_OPENNI_BGR_IMAGE );

    cout << "rows: " << depthMap.rows << " cols: " << depthMap.cols << endl;
    cout << "depth: " << depthMap.at<int>(0,0) << endl;

    imshow("RGB image", bgrImage);

    if( waitKey( 30 ) >= 0 )
        break;
}h
return 0;

after running the code I get the following results:

close to the wall about a meter away:

rows: 480 cols: 640
depth: 1157
rows: 480 cols: 640
depth: 1157
rows: 480 cols: 640
depth: 1157
rows: 480 cols: 640
depth: 1157 

(Kinect facing the wall, just over a meter away)

rows: 480 cols: 640
depth: 83690629
rows: 480 cols: 640
depth: 83690629
rows: 480 cols: 640
depth: 83690629
rows: 480 cols: 640
depth: 83690629
rows: 480 cols: 640
depth: 83690629

I have been told that these values are in fact two pixels? which I really don't understand.

So here is my understanding so far:

I grabbed a depth frame and stored it inside a matrix called depthMap. the size of the matrix is now 640*480. I am using the code depthMap.at(0,0) to grab the first depth value from row 0, column 0. but instead of getting back a result on millimetres i get back 83690629! which is way off the max value of 10000 which i'm expecting

How can I convert these values into millimetres so I can make use of them? thank you

Answer

Eran W picture Eran W · Jun 12, 2012

The depth map data is a single channel unsigned 16Bit. Try to replace int to CV_16UC1