获取深度图任意指定点的深度值

在立体视觉开发的时候,我们常常需要观察摄像头所获取图像的深度值规律,以便进一步提取特征
而在我们提取深度值的时候,往往更希望能够快速准确的获取指定点的深度值

本程序基于Kinect2所获取的深度图,其余深度相机获取的深度图可通用

程序原理

通过设置一个onmouse时间,当触发点击事件后,可以获取到点击点所在图像的长宽位置,再根据该位置提取深度值

程序

#include  
#include  
#include  

using namespace cv;
using namespace std;
typedef unsigned short      UINT16, *PUINT16;

vector src;

int i = 0;
int nWidth = 512;
int nHeight = 424;
UINT16 *pBuffer = NULL;

Mat depth_image(nHeight, nWidth, CV_16UC1);
Mat DepthImage(nHeight, nWidth, CV_16UC1);

Point p;
void onMouse(int event, int x, int y, int flags, void *param)
{
    Mat *img = reinterpret_cast(param);
    if (event == CV_EVENT_LBUTTONDOWN)//左键按下,读取初始坐标  
    {
        i++;//统计点击的次数  
        p.x = x;
        p.y = y;
        src.push_back(p);
        cout << p << static_cast<int>(img->at<unsigned short>(cv::Point(x, y))) << endl;
        cout << i << endl;
        cout << p.x << " " << p.y << " " << static_cast<int>(img->at<unsigned short>(cv::Point(x, y))) << endl;
    }
}
int main(int argc, char** argv)
{
    Mat frame;
    depth_image = imread("Picture1.png", IMREAD_ANYDEPTH);
    while (true)
    {
        namedWindow("image", CV_WINDOW_AUTOSIZE);
        setMouseCallback("image", onMouse, &depth_image);
        imshow("image", depth_image*20);
        waitKey(1);
    }
    return 0;
}

程序效果

我们任意点击图像上的点,即可获取其深度图

参考

http://www.tchaser.com/wordpress/artificial-intelligence/cv/depth-image-locate.html

你可能感兴趣的:(机器视觉)