Kinect2+opencv之BodyIndex篇:显示Kinect2的BodyIndex

目录

一、目的:

1、显示出Kinect2的BodyIndex

二、参考

1、【翻译】Kinect v2程序设计(C++) BodyIndex篇

①总结:good:作者翻译了一个系列的Kinect2的文章,目前测试Color、Depth、BodyIndex篇,都能实现,下面是参考后直接实现的代码

2、Kinect2+opencv之Color篇:显示Kinect2的画面

①总结:good:这是我总结的Color,有直接实现的代码

3、Kinect2+opencv之Depth篇:显示Kinect2的深度图

①总结:good:这是我总结的Depth,有直接实现的代码

三、步骤

1、创建MFC工程,配置Kinect2和Opencv的环境

2、头文件

①导入Kinect、opencv所需的头文件

 ②测试Kinect代码

3、cpp源文件文件

①安全释放函数:ColorBasics-D2D里面有的直接复制过来

②OnInitDialog函数中使用到Test_kinect函数

③Test_kinect:函数

4、运行效果:


一、目的:

1、显示出Kinect2的BodyIndex

 

二、参考

1、【翻译】Kinect v2程序设计(C++) BodyIndex篇

https://www.cnblogs.com/TracePlus/p/4136357.html

①总结:good:作者翻译了一个系列的Kinect2的文章,目前测试Color、Depth、BodyIndex篇,都能实现,下面是参考后直接实现的代码

 

2、Kinect2+opencv之Color篇:显示Kinect2的画面

https://blog.csdn.net/qq_40544338/article/details/105392479

①总结:good:这是我总结的Color,有直接实现的代码

 

3、Kinect2+opencv之Depth篇:显示Kinect2的深度图

https://blog.csdn.net/qq_40544338/article/details/105447079

①总结:good:这是我总结的Depth,有直接实现的代码

 

三、步骤

1、创建MFC工程,配置Kinect2和Opencv的环境

这个再次就不赘述了,网上很多的教程

 

2、头文件

①导入Kinect、opencv所需的头文件

#include "Kinect.h"
#include <iostream>
#include "cv.h"
#include "opencv2/opencv.hpp"

 ②测试Kinect代码

//Kinect2	
public:
	int Test_kinect2();

 

3、cpp源文件文件

①安全释放函数:ColorBasics-D2D里面有的直接复制过来

// Safe release for interfaces
template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
	if (pInterfaceToRelease != NULL)
	{
		pInterfaceToRelease->Release();
		pInterfaceToRelease = NULL;
	}
}

②OnInitDialog函数中使用到Test_kinect函数

③Test_kinect:函数

//功能:显示Kinect2的BodyIndex
//参考:https://mp.csdn.net/console/editor/html/105447909     https://www.cnblogs.com/TracePlus/p/4136357.html
int  CMyKinect2Dlg::Test_kinect2()
{
	//第一步:取得「Sensor」
	IKinectSensor* pSensor;//处理Kinect v2预览版的Sensor接口。
	HRESULT hResult = S_OK;
	hResult = GetDefaultKinectSensor(&pSensor);//取得默认的Sensor。
	if (FAILED(hResult)) 
	{
		std::cerr << "Error : GetDefaultKinectSensor" << std::endl;
		return -1;
	}
	hResult = pSensor->Open();//打开Sensor。
	if (FAILED(hResult))
	{
		std::cerr << "Error : IKinectSensor::Open()" << std::endl;
		return -1;
	}

	//第二步:从「Sensor」取得「Source」。
	IBodyIndexFrameSource* pBodyIndexSource;//取得BodyIndex Frame的Source接口。
	hResult = pSensor->get_BodyIndexFrameSource(&pBodyIndexSource);//从Sensor取得Source。
	if (FAILED(hResult))
	{
		std::cerr << "Error : IKinectSensor::get_BodyIndexFrameSource()" << std::endl;
		return -1;
	}

	//第三步:「Source」从打开「Reader」。
	IBodyIndexFrameReader* pBodyIndexReader;//取得BodyIndex  Frame的Reader接口。
	hResult = pBodyIndexSource->OpenReader(&pBodyIndexReader);//从Source打开Reader
	if (FAILED(hResult))
	{
		std::cerr << "Error : IBodyIndexFrameSource::OpenReader()" << std::endl;
		return -1;
	}

	//第四步:从「Reader」取得最新的「Frame」。
	int width = 512;//BodyIndex的尺寸(512×424)。这里为了简化说明,画像尺寸用硬代码来设定,示例程序可以Source取得着Frame信息。
	int height = 424;
	cv::Mat bodyIndexMat(height, width, CV_8UC3);// 为了从BodyIndex获得人体区域来绘制,使用OpenCV的cv::Mat。
	cv::namedWindow("BodyIndex");
	// Color Table
	cv::Vec3b color[6];//绘制人体区域的color table。
	color[0] = cv::Vec3b(255, 0, 0);
	color[1] = cv::Vec3b(0, 255, 0);
	color[2] = cv::Vec3b(0, 0, 255);
	color[3] = cv::Vec3b(255, 255, 0);
	color[4] = cv::Vec3b(255, 0, 255);
	color[5] = cv::Vec3b(0, 255, 255);
	while (1)
	{
		// Frame
		IBodyIndexFrame* pBodyIndexFrame = nullptr;//取得BodyIndex用的Frame接口。
		hResult = pBodyIndexReader->AcquireLatestFrame(&pBodyIndexFrame);//从Reader取得最新的Frame。
		if (SUCCEEDED(hResult))
		{
			unsigned int bufferSize = 0;
			unsigned char* buffer = nullptr;
			hResult = pBodyIndexFrame->AccessUnderlyingBuffer(&bufferSize, &buffer);//从Frame取得BodyIndex。  取得保存BodyIndex数组的指针。
			if (SUCCEEDED(hResult))
			{
				for (int y = 0; y < height; y++)
				{
					for (int x = 0; x < width; x++)
					{
						unsigned int index = y * width + x;
						if (buffer[index] != 0xff)
						{
							bodyIndexMat.at<cv::Vec3b>(y, x) = color[buffer[index]];//绘制BodyIndex的人体区域。 每个BodyIndex参照color table着色。
						}
						else {
							bodyIndexMat.at<cv::Vec3b>(y, x) = cv::Vec3b(0, 0, 0);
						}
					}
				}
			}
		}
		SafeRelease(pBodyIndexFrame);
		// Show Window
		cv::imshow("BodyIndex", bodyIndexMat);
		if (cv::waitKey(30) == VK_ESCAPE) {
			break;
		}
	}

	return 0;
}

 

4、运行效果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值