奥比中光深度相机获取鼠标点击处三维坐标
以下内容是在下载对应SDK上直接添加代码。
一、代码部分
(1)下载对应平台的SDK
在DeepReaderEvevt.cpp输出拍摄到的图像的中心点的深度值,图像中心点深度值索引如下所示。深度值以数组形式存储,按行扫描。
int middleIndex = (frame.getHeight() + 1)*frame.getWidth() / 2;
(2)在SimpleViewer中添加自己需要的代码
在SimpleViewer中将深度图与彩色图对齐,0为硬件对齐,1为软件对齐,2为默认不对齐。在获取深度流之后获取其深度值信息。
int nRet = pD2CSwapper->GetStreamData(cv_rgb, cv_depth);
DepthPixel* pDepth;
pDepth = (DepthPixel*)cv_depth.data;
在图像显示框下利用cvSetMouseCallback()函数
mDraw.ShowImagesSideBySide(_winTitle, imgsOne, "", 10, 450);
cvSetMouseCallback("SimpleViewer", onMouse, pDepth);
定义鼠标回调函数
// opencv鼠标点击输出像素坐标
int onMouse(int event, int x, int y, int flags, void* param)
{
if (event == CV_EVENT_LBUTTONDOWN)
{
int d;
DepthPixel* pDepth;
pDepth = (DepthPixel*)param;
d = y * 640 + x - 1; // 鼠标点击点对应深度值的索引
printf("(%d,%d,%d)\n", x, y, pDepth[d]);
return 0;
}
}
结果如图:
点击配准后的图像可输出三维坐标,展示的图片截掉了图像,只放了显示三维坐标的图片。
二、相关知识
(1) cvSetMouseCallback,注册一个回调函数。cvSetMouseCallback(“TestWin”, TestCallBack, Context),第一个参数是创建的显示框窗口的名字,第二个参数是回调函数,第三个参数会作为参数传递给回调函数,可以在主进程与回调函数之间交换上下文。
(2)鼠标回调函数功能需要自己开发,原型为:
void OnMouse(int event, int x, int y, int flags, void* param);
第一个参数为鼠标事件:
//! Mouse Events see cv::MouseCallback
enum MouseEventTypes {
EVENT_MOUSEMOVE = 0, //!< indicates that the mouse pointer has moved over the window.
EVENT_LBUTTONDOWN = 1, //!< indicates that the left mouse button is pressed.
EVENT_RBUTTONDOWN = 2, //!< indicates that the right mouse button is pressed.
EVENT_MBUTTONDOWN = 3, //!< indicates that the middle mouse button is pressed.
EVENT_LBUTTONUP = 4, //!< indicates that left mouse button is released.
EVENT_RBUTTONUP = 5, //!< indicates that right mouse button is released.
EVENT_MBUTTONUP = 6, //!< indicates that middle mouse button is released.
EVENT_LBUTTONDBLCLK = 7, //!< indicates that left mouse button is double clicked.
EVENT_RBUTTONDBLCLK = 8, //!< indicates that right mouse button is double clicked.
EVENT_MBUTTONDBLCLK = 9, //!< indicates that middle mouse button is double clicked.
EVENT_MOUSEWHEEL = 10,//!< positive and negative values mean forward and backward scrolling, respectively.
EVENT_MOUSEHWHEEL = 11 //!< positive and negative values mean right and left scrolling, respectively.
};
第二个和第三个参数表示当前鼠标位置,此时的鼠标位置是相对于“图像”的,不是屏幕也不是显示框。
第四个参数flags是EVENT_FLAG的组合
//! Mouse Event Flags see cv::MouseCallback
enum MouseEventFlags {
EVENT_FLAG_LBUTTON = 1, //!< indicates that the left mouse button is down.
EVENT_FLAG_RBUTTON = 2, //!< indicates that the right mouse button is down.
EVENT_FLAG_MBUTTON = 4, //!< indicates that the middle mouse button is down.
EVENT_FLAG_CTRLKEY = 8, //!< indicates that CTRL Key is pressed.
EVENT_FLAG_SHIFTKEY = 16,//!< indicates that SHIFT Key is pressed.
EVENT_FLAG_ALTKEY = 32 //!< indicates that ALT Key is pressed.
};
第五个参数,void* param可接收来自cvSetMouseCallback()传递而来的数据。
相关参考: