栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

关于KINECT V2.0 C++ SDK 基础教程的笔记 EP2

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于KINECT V2.0 C++ SDK 基础教程的笔记 EP2

最近忙着搞老师的任务,没来得及更新点云系列。
目前在做Kinect,在这里接着做个笔记。
原文地址: Kinect Tutorials
这仅仅是做一个笔记以及自己的实际操作记录


关于KINECT V2.0 C++ SDK 基础教程的笔记 EP2
  • 1、概述
    • 1.1 更改代码
      • 1.1.1 改变数据源
      • 1.1.2 获取深度数据帧
    • 1.2 完整代码
    • 1.3 结果图


跟着网上的教程,学习Kinect c++ SDK。
⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇
➡➡➡➡➡<<< Kinect Tutorials >>>⬅⬅⬅⬅⬅⬅
⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆⬆
跟着网上的教程,学习Kinect c++ SDK。


1、概述

在上一章获取RGB的基础上进行了深度图展示

1.1 更改代码 1.1.1 改变数据源
IDepthframeReader* reader;     // Kinect depth data source

bool initKinect() {
    if (FAILED(GetDefaultKinectSensor(&sensor))) {
        return false;
    }
    if (sensor) {
        sensor->Open();
        IDepthframeSource* framesource = NULL;
        sensor->get_DepthframeSource(&framesource);
        framesource->OpenReader(&reader);
        if (framesource) {
            framesource->Release();
            framesource = NULL;
        }
        return true;
    } else {
        return false;
    }
}
1.1.2 获取深度数据帧
void getKinectData(GLubyte* dest) {
    IDepthframe* frame = NULL;
    if (SUCCEEDED(reader->AcquireLatestframe(&frame))) {
        unsigned int sz;
        unsigned short* buf;
        frame->AccessUnderlyingBuffer(&sz, &buf);

        const unsigned short* curr = (const unsigned short*)buf;
        const unsigned short* dataEnd = curr + (width*height);

        while (curr < dataEnd) {
            // Get depth in millimeters
            unsigned short depth = (*curr++);

            // Draw a grayscale image of the depth:
            // B,G,R are all set to depth%256, alpha set to 1.
            for (int i = 0; i < 3; ++i)
                *dest++ = (BYTE)depth % 256;
            *dest++ = 0xff;
        }
    }
    if (frame) frame->Release();
}

遇到找不到freeglut.dll的错误,把freeglut.dll放到Debug文件夹下就可以了

1.2 完整代码
//这是一个基于gult显示深度图的代码

// SDL
//包含文件
//为了Kinect正常工作

#include 
#include 
//为可视化
#include 
#include 
#include 
//kinect主要头文件
#include 

//常量和全局变量
#define width 512
#define height 424

// OpenGL Variables 变量
GLuint textureId;              // ID of the texture to contain Kinect RGB Data 包含 Kinect RGB 数据的纹理 ID
GLubyte data[width * height * 4];  // BGRA array containing the texture data  包含纹理数据的 BGRA 数组

// Kinect variables 变量
IKinectSensor* sensor;         // Kinect sensor  Kinect传感器
IDepthframeReader* reader;     // Kinect color data source    Kinect 颜色数据源

//Kinect初始化
//initKinect()函数的作用是初始化一个 Kinect 设备。它包含了两个部分:首先,我们需要找到一个已连接到电脑的 Kinect 传感器,然后将其初始化并准备从中读取数据。
bool initKinect() {
    if (FAILED(GetDefaultKinectSensor(&sensor))) {
		return false;
	}
	if (sensor) {
		sensor->Open();
		IDepthframeSource* framesource = NULL;
		sensor->get_DepthframeSource(&framesource);
		framesource->OpenReader(&reader);
		if (framesource) {
			framesource->Release();
			framesource = NULL;
		}
		return true;
	} else {
		return false;
	}
}

//从 Kinect 中获取 RGB 帧
void getKinectData(GLubyte* dest) {
	IDepthframe* frame = NULL;
	if (SUCCEEDED(reader->AcquireLatestframe(&frame))) {
		unsigned int sz;
		unsigned short* buf;
		frame->AccessUnderlyingBuffer(&sz, &buf);

		const unsigned short* curr = (const unsigned short*)buf;
		const unsigned short* dataEnd = curr + (width * height);

		while (curr < dataEnd) {
			// Get depth in millimeters
			unsigned short depth = (*curr++);

			// Draw a grayscale image of the depth:
			// B,G,R are all set to depth%256, alpha set to 1.
			for (int i = 0; i < 3; ++i)
				*dest++ = (BYTE)depth % 256;
			*dest++ = 0xff;
		}
	}
	if (frame) frame->Release();
}

void drawKinectData() {
	glBindTexture(GL_TEXTURE_2D, textureId);
	getKinectData(data);
	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, (GLvoid*)data);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(0, 0, 0);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(width, 0, 0);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(width, height, 0.0f);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(0, height, 0.0f);
	glEnd();
}
//具体的初始化代码取决于使用那种实现方式 (GLUT 或 SDL)。它只是使用适当的 API 初始化一个窗口,失败时返回 false。GLUT 版本的实现还会通过指定draw()函数在每次循环迭代中被调用来设置主循环。
//主循环在execute()函数中启动。在 GLUT中,循环是在后台处理的,所以我们需要做的就是调用glutMainLoop()函数。
void draw() {
	drawKinectData();
	glutSwapBuffers();
}

void execute() {
	glutMainLoop();
}

bool init(int argc, char* argv[]) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowSize(width, height);
	glutCreateWindow("Kinect SDK Tutorial");
	glutDisplayFunc(draw);
	glutIdleFunc(draw);
	return true;
}


int main(int argc, char* argv[]) {
	if (!init(argc, argv)) return 1;
	if (!initKinect()) return 1;

	// Initialize textures
	//代码中描述了三个步骤——设置纹理以包含图像帧,准备 OpenGL 来绘制纹理,以及设置摄像机视点(对 2D 图像使用正投影)。
	glGenTextures(1, &textureId);
	glBindTexture(GL_TEXTURE_2D, textureId);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height,
		0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, (GLvoid*)data);
	glBindTexture(GL_TEXTURE_2D, 0);

	// OpenGL setup
	glClearColor(0, 0, 0, 0);
	glClearDepth(1.0f);
	glEnable(GL_TEXTURE_2D);

	// Camera setup
	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, width, height, 0, 1, -1);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	// Main loop
	execute();
	return 0;
}
1.3 结果图

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/665764.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号