Kinect2.0点云数据获取
1.个人资源索引2.SLAM学习笔记(1)基本概念3.SLAM学习笔记(2)SLAM算法4.[ROS]2 尝试编译OrbSLAM5.[ROS]1 小乌龟6.SLAM最近的工作7.SLAM学习笔记(3)相关概念8.[SLAM]2D激光线特征提取9.[SLAM] GMapping SLAM源码阅读(草稿)10.[QGLViewer]First Demo11.SLAM数据集12.[SLAM]2D激光扫描匹配方法13.[g2o]一个备忘14.[ROS] slam_gmapping15.[ROS]3 Linux编程练习16.Linux学习和ROS安装(1)17.小豆包的学习之旅:机器人定位18.小豆包的学习之旅:传感器观测模型19.Kinect2.0相机标定20.小豆包的学习之旅:里程计运动模型21.小豆包的学习之旅:入门篇
22.Kinect2.0点云数据获取
23.[硬件]Robot运动控制24.[硬件]Urg_viewer数据读取25.[硬件]三维点云数据获取26.Kinect2.0获取数据27.ndt histogram_direction28.rplidar & hector slam without odometry29.rplidar测试30.[原创]NDT方法在SLAM中的应用31.[SLAM]Karto SLAM算法学习(草稿)32.ROS学习备忘33.[ROS]激光驱动安装34.OrbSLAM2采集点云数据35.Cartographer源码阅读(8):imu_tracker36.[硬件]点云数据采集237.Cartographer源码阅读(9):图优化的前端——闭环检测38.Cartographer源码阅读(7):轨迹推算和位姿推算的原理39.Ethzasl MSF源码阅读(3):MSF_Core和PoseMeasurement40.Ethzasl MSF源码阅读(2):百川汇海41.Ethzasl MSF源码阅读(1):程序入口和主题订阅42.Cartographer源码阅读(6):LocalTrajectoryBuilder和PoseExtrapolator43.Cartographer源码阅读(5):PoseGraph位姿图44.Cartographer源码阅读(4):Node和MapBuilder对象245.Cartographer源码阅读(3):程序逻辑结构46.Cartographer源码阅读(2):Node和MapBuilder对象47.Cartographer源码阅读(1):程序入口48.实时Cartographer测试(1) - rplidar49.Cartographer安装50.ROS安装(2)51.[ROS]一些传感器数据读取融合问题的思考52.小豆包的学习之旅:占用概率栅格地图和cost-map53.小豆包的学习之旅:开发记录54.[概述]移动机器人自主探索55.MRPT编译接上一篇:Kinect2.0获取数据
http://blog.csdn.net/jiaojialulu/article/details/53087988
博主好细心,代码基本上帖过来就可以用,注释掉的部分改成文件输出就可以了!
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | #include "stdafx.h" #include "kinect.h" #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> using namespace cv; using namespace std; // 安全释放指针 template < class Interface> inline void SafeRelease(Interface *& pInterfaceToRelease) { if (pInterfaceToRelease != NULL) { pInterfaceToRelease->Release(); pInterfaceToRelease = NULL; } } int _tmain( int argc, _TCHAR* argv[]) { // 获取Kinect设备 IKinectSensor* m_pKinectSensor; HRESULT hr; hr = GetDefaultKinectSensor(&m_pKinectSensor); if (FAILED(hr)) { return hr; } IMultiSourceFrameReader* m_pMultiFrameReader=NULL; if (m_pKinectSensor) { hr = m_pKinectSensor->Open(); if (SUCCEEDED(hr)) { // 获取多数据源到读取器 hr = m_pKinectSensor->OpenMultiSourceFrameReader( FrameSourceTypes::FrameSourceTypes_Color | FrameSourceTypes::FrameSourceTypes_Infrared | FrameSourceTypes::FrameSourceTypes_Depth, &m_pMultiFrameReader); } } if (!m_pKinectSensor || FAILED(hr)) { return E_FAIL; } // 三个数据帧及引用 IDepthFrameReference* m_pDepthFrameReference = NULL; IColorFrameReference* m_pColorFrameReference = NULL; IInfraredFrameReference* m_pInfraredFrameReference = NULL; IInfraredFrame* m_pInfraredFrame = NULL; IDepthFrame* m_pDepthFrame = NULL; IColorFrame* m_pColorFrame = NULL; // 三个图片格式 Mat i_rgb(1080, 1920, CV_8UC4); //注意:这里必须为4通道的图,Kinect的数据只能以Bgra格式传出 Mat i_depth(424, 512, CV_8UC1); Mat i_ir(424, 512, CV_16UC1); UINT16 *depthData = new UINT16[424 * 512]; IMultiSourceFrame* m_pMultiFrame = nullptr ; while ( true ) { // 获取新的一个多源数据帧 hr = m_pMultiFrameReader->AcquireLatestFrame(&m_pMultiFrame); if (FAILED(hr) || !m_pMultiFrame) { //cout << "!!!" << endl; continue ; } // 从多源数据帧中分离出彩色数据,深度数据和红外数据 if (SUCCEEDED(hr)) hr = m_pMultiFrame->get_ColorFrameReference(&m_pColorFrameReference); if (SUCCEEDED(hr)) hr = m_pColorFrameReference->AcquireFrame(&m_pColorFrame); if (SUCCEEDED(hr)) hr = m_pMultiFrame->get_DepthFrameReference(&m_pDepthFrameReference); if (SUCCEEDED(hr)) hr = m_pDepthFrameReference->AcquireFrame(&m_pDepthFrame); if (SUCCEEDED(hr)) hr = m_pMultiFrame->get_InfraredFrameReference(&m_pInfraredFrameReference); if (SUCCEEDED(hr)) hr = m_pInfraredFrameReference->AcquireFrame(&m_pInfraredFrame); // color拷贝到图片中 UINT nColorBufferSize = 1920 * 1080 * 4; if (SUCCEEDED(hr)) hr = m_pColorFrame->CopyConvertedFrameDataToArray(nColorBufferSize, reinterpret_cast < BYTE *>(i_rgb.data), ColorImageFormat::ColorImageFormat_Bgra); // depth拷贝到图片中 if (SUCCEEDED(hr)) { hr = m_pDepthFrame->CopyFrameDataToArray(424 * 512, depthData); for ( int i = 0; i < 512 * 424; i++) { // 0-255深度图,为了显示明显,只取深度数据的低8位 BYTE intensity = static_cast < BYTE >(depthData[i] % 256); reinterpret_cast < BYTE *>(i_depth.data)[i] = intensity; } ICoordinateMapper* m_pCoordinateMapper=NULL; hr = m_pKinectSensor->get_CoordinateMapper(&m_pCoordinateMapper); ColorSpacePoint* m_pColorCoordinates = new ColorSpacePoint[512 * 424]; HRESULT hr = m_pCoordinateMapper->MapDepthFrameToColorSpace(512 * 424, depthData, 512 * 424, m_pColorCoordinates); Mat i_depthToRgb(424, 512, CV_8UC4); if (SUCCEEDED(hr)) { for ( int i = 0; i < 424 * 512; i++) { ColorSpacePoint p = m_pColorCoordinates[i]; if (p.X != -std::numeric_limits< float >::infinity() && p.Y != -std::numeric_limits< float >::infinity()) { int colorX = static_cast < int >(p.X + 0.5f); int colorY = static_cast < int >(p.Y + 0.5f); if ((colorX >= 0 && colorX < 1920) && (colorY >= 0 && colorY < 1080)) { i_depthToRgb.data[i * 4] = i_rgb.data[(colorY * 1920 + colorX) * 4]; i_depthToRgb.data[i * 4 + 1] = i_rgb.data[(colorY * 1920 + colorX) * 4 + 1]; i_depthToRgb.data[i * 4 + 2] = i_rgb.data[(colorY * 1920 + colorX) * 4 + 2]; i_depthToRgb.data[i * 4 + 3] = i_rgb.data[(colorY * 1920 + colorX) * 4 + 3]; } } } } imshow( "rgb2depth" , i_depthToRgb); if (waitKey(1) == VK_ESCAPE) break ; CameraSpacePoint* m_pCameraCoordinates = new CameraSpacePoint[512 * 424]; if (SUCCEEDED(hr)) { HRESULT hr = m_pCoordinateMapper->MapDepthFrameToCameraSpace(512 * 424, depthData, 512 * 424, m_pCameraCoordinates); } if (SUCCEEDED(hr)) { for ( int i = 0; i < 512 * 424; i++) { CameraSpacePoint p = m_pCameraCoordinates[i]; if (p.X != -std::numeric_limits< float >::infinity() && p.Y != -std::numeric_limits< float >::infinity() && p.Z != -std::numeric_limits< float >::infinity()) { float cameraX = static_cast < float >(p.X); float cameraY = static_cast < float >(p.Y); float cameraZ = static_cast < float >(p.Z); //cout << "x: " << cameraX << "y: " << cameraY << "z: " << cameraZ << endl; //GLubyte *rgb = new GLubyte(); //rgb[2] = i_depthToRgb.data[i * 4 + 0]; //rgb[1] = i_depthToRgb.data[i * 4 + 1]; //rgb[0] = i_depthToRgb.data[i * 4 + 2]; //// 显示点 //glColor3ubv(rgb); //glVertex3f(cameraX, -cameraY, cameraZ); } } } } // 显示 /*imshow("rgb", i_rgb); if (waitKey(1) == VK_ESCAPE) break;*/ imshow( "depth" , i_depth); if (waitKey(1) == VK_ESCAPE) break ; // 释放资源 SafeRelease(m_pColorFrame); SafeRelease(m_pDepthFrame); SafeRelease(m_pInfraredFrame); SafeRelease(m_pColorFrameReference); SafeRelease(m_pDepthFrameReference); SafeRelease(m_pInfraredFrameReference); SafeRelease(m_pMultiFrame); } // 关闭窗口,设备 cv::destroyAllWindows(); m_pKinectSensor->Close(); std:: system ( "pause" ); return 0; } |
我们实验室的一帧数据,哈哈!
上面的代码有内存泄露,程序运行一段时间把我的机器物理内存都占满了,下面代码更新一下!
作者:太一吾鱼水
文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。
欢迎大家留言交流,转载请注明出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
2012-03-30 仿射变换的一个练习题!