Kinect2.0点云数据获取

接上一篇: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;
}

我们实验室的一帧数据,哈哈!  

上面的代码有内存泄露,程序运行一段时间把我的机器物理内存都占满了,下面代码更新一下!

代码在这里

posted @   太一吾鱼水  阅读(9412)  评论(2编辑  收藏  举报
编辑推荐:
· 基于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 仿射变换的一个练习题!
点击右上角即可分享
微信分享提示