在立体视觉开发的时候,我们常常需要观察摄像头所获取图像的深度值规律,以便进一步提取特征
而在我们提取深度值的时候,往往更希望能够快速准确的获取指定点的深度值
本程序基于Kinect2所获取的深度图,其余深度相机获取的深度图可通用
程序原理
通过设置一个onmouse时间,当触发点击事件后,可以获取到点击点所在图像的长宽位置,再根据该位置提取深度值
程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include<iostream> #include<opencv2/opencv.hpp> #include<fstream> using namespace cv; using namespace std; typedef unsigned short UINT16, *PUINT16; vector<Point> 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<Mat*>(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; } |
程序效果
我们任意点击图像上的点,即可获取其深度图
运行之后报这个错误 请问楼主知道是什么情况嘛?? OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)pt.y < (unsigned) size.p[0] && (unsigned)(pt.x*DataType::channels) < (unsigned)(size.p[1]*cha nnels()) && ((((sizeof(size_t)<> ((DataType::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file e:\environment\opencv- 2.4.12\build\install\include\opencv2\core\mat.hpp, line 554