depth-data

Overview

The most important thing while using 3D cameras is to get the depth value. This demo shows how to get the depth data from camera frame. Move the mouse in an OpenCV window and obtain the depth value of the mouse cursor position.

Expect Output

Prerequisite

Tutorial

  • By adding mouse callback function on OpenCV window, we can get the (x,y) coordinate when mouse move over the window.

void onMouseMove(int event, int x, int y, int flags, void *userdata)
{
    if (event == cv::EVENT_MOUSEMOVE)
    {
        std::cout << "Mouse coordinate (x,y) = " << x "," << y << std::endl;
    }
}

cv::namedWindow("Depth Data");
cv::setMouseCallback("Depth Data", onMouseMove, NULL);
  • After converting depthFrame to OpenCV Mat format, we can retrieve the the data by cv::Mat::data. Depth data is store in two bytes, so we use int16_t* to access the data.

cv::Mat depthMat;
...
uint16_t *depthData = (uint16_t *)depthMat.data;
  • Depth data is store in row-major format. You can use the following code to get the value from given x and y coordinate.

uint16_t depthValue = depthData[y * depthMat.cols + x];

Full code

depth_data.cpp

Last updated