#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include "stdafx.h"
#include "ImageRenderer.h"
using namespace cv;
using namespace std;
//申明全局函数
cv::Mat ConvertMat(const UINT16* pBuffer, int nWidth, int nHeight, USHORT nMinDepth, USHORT nMaxDepth);// 转换depth图像到cv::Mat
cv::Mat ConvertMat(const RGBQUAD* pBuffer, int nWidth, int nHeight);// 转换color图像到cv::Mat
int main()
{
int depth_width = 512; //depth图像就是这么小
int depth_height = 424;
int color_widht = 1920; //color图像就是辣么大
int color_height = 1080;
cv::Mat depthImg_show = cv::Mat::zeros(depth_height, depth_width, CV_8UC3);//原始UINT16 深度图像不适合用来显示,所以需要砍成8位的就可以了,但是显示出来也不是非常好,最好能用原始16位图像颜色编码,凑合着看了
cv::Mat depthImg = cv::Mat::zeros(depth_height, depth_width, CV_16UC1);//the depth image
cv::Mat colorImg = cv::Mat::zeros(color_height, color_widht, CV_8UC3);//the color image
HRESULT hr;
// Current Kinect
IKinectSensor* m_pKinectSensor = NULL;
// Depth reader
IDepthFrameReader* m_pDepthFrameReader = NULL;
// Color reader
IColorFrameReader* m_pColorFrameReader = NULL;
hr = GetDefaultKinectSensor(&m_pKinectSensor);
if (FAILED(hr))
{
return hr;
}
if (m_pKinectSensor)
{
// Initialize the Kinect and get the depth reader
IDepthFrameSource* pDepthFrameSource = NULL;
hr = m_pKinectSensor->Open();
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource);
}
if (SUCCEEDED(hr))
{
hr = pDepthFrameSource->OpenReader(&m_pDepthFrameReader);
}
SafeRelease(pDepthFrameSource);
// for color
// Initialize the Kinect and get the color reader
IColorFrameSource* pColorFrameSource = NULL;
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->get_ColorFrameSource(&pColorFrameSource);
}
if (SUCCEEDED(hr))
{
hr = pColorFrameSource->OpenReader(&m_pColorFrameReader);
}
SafeRelease(pColorFrameSource);
}
//valify the depth reader
if (!m_pDepthFrameReader)
{
cout << "FUCK! Can not find the m_pDepthFrameReader!" << endl;
cv::waitKey(0);
exit(0);
}
//valify the color reader
if (!m_pDepthFrameReader)
{
cout << "FUCK! Can not find the m_pColorFrameReader!" << endl;
cv::waitKey(0);
exit(0);
}
// get the data!
UINT nBufferSize_depth = 0;
UINT16 *pBuffer_depth = NULL;
UINT nBufferSize_coloar = 0;
RGBQUAD *pBuffer_color = NULL;
char key = 0;
while (true)
{
IDepthFrame* pDepthFrame = NULL;
HRESULT hr = m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame);
if (SUCCEEDED(hr))
{
USHORT nDepthMinReliableDistance = 0;
USHORT nDepthMaxReliableDistance = 0;
if (SUCCEEDED(hr))
{
hr = pDepthFrame->get_DepthMinReliableDistance(&nDepthMinReliableDistance);
}
if (SUCCEEDED(hr))
{
hr = pDepthFrame->get_DepthMaxReliableDistance(&nDepthMaxReliableDistance);
}
if (SUCCEEDED(hr))
{
hr = pDepthFrame->AccessUnderlyingBuffer(&nBufferSize_depth, &pBuffer_depth);
depthImg_show = ConvertMat(pBuffer_depth, depth_width, depth_height, nDepthMinReliableDistance, nDepthMaxReliableDistance);
}
}
SafeRelease(pDepthFrame);
//for color
IColorFrame* pColorFrame = NULL;
hr = m_pColorFrameReader->AcquireLatestFrame(&pColorFrame);
ColorImageFormat imageFormat = ColorImageFormat_None;
if (SUCCEEDED(hr))
{
ColorImageFormat imageFormat = ColorImageFormat_None;
if (SUCCEEDED(hr))
{
hr = pColorFrame->get_RawColorImageFormat(&imageFormat);
}
RGBQUAD* m_pColorRGBX = NULL;
m_pColorRGBX = new RGBQUAD[color_widht * color_height];
if (SUCCEEDED(hr))
{
if (imageFormat == ColorImageFormat_Bgra)//这里有两个format,不知道具体含义,大概一个预先分配内存,一个需要自己开空间吧
{
hr = pColorFrame->AccessRawUnderlyingBuffer(&nBufferSize_coloar, reinterpret_cast<BYTE**>(&pBuffer_color));
}
else if (m_pColorRGBX)
{
pBuffer_color = m_pColorRGBX;
nBufferSize_coloar = color_widht * color_height * sizeof(RGBQUAD);
hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize_coloar, reinterpret_cast<BYTE*>(pBuffer_color), ColorImageFormat_Bgra);
}
else
{
hr = E_FAIL;
}
colorImg = ConvertMat(pBuffer_color, color_widht, color_height);
}
SafeRelease(pColorFrame);
delete[] m_pColorRGBX;
}
namedWindow("depth", 0);
cv::imshow("depth", depthImg_show);
namedWindow("color", 0);
cv::imshow("color", colorImg);
key = cv::waitKey(1);
if (key == 27)
{
break;
}
}
return 0;
}
// 转换depth图像到cv::Mat
cv::Mat ConvertMat(const UINT16* pBuffer, int nWidth, int nHeight, USHORT nMinDepth, USHORT nMaxDepth)
{
cv::Mat img(nHeight, nWidth, CV_8UC3);
uchar* p_mat = img.data;
const UINT16* pBufferEnd = pBuffer + (nWidth * nHeight);
while (pBuffer < pBufferEnd)
{
USHORT depth = *pBuffer;
BYTE intensity = static_cast<BYTE>((depth >= nMinDepth) && (depth <= nMaxDepth) ? (depth % 256) : 0);
*p_mat = intensity;
p_mat++;
*p_mat = intensity;
p_mat++;
*p_mat = intensity;
p_mat++;
++pBuffer;
}
return img;
}
// 转换color图像到cv::Mat
cv::Mat ConvertMat(const RGBQUAD* pBuffer, int nWidth, int nHeight)
{
cv::Mat img(nHeight, nWidth, CV_8UC3);
uchar* p_mat = img.data;
const RGBQUAD* pBufferEnd = pBuffer + (nWidth * nHeight);
while (pBuffer < pBufferEnd)
{
*p_mat = pBuffer->rgbBlue;
p_mat++;
*p_mat = pBuffer->rgbGreen;
p_mat++;
*p_mat = pBuffer->rgbRed;
p_mat++;
++pBuffer;
}
return img;
}
没有合适的资源?快使用搜索试试~ 我知道了~
kinect2.0获取深度图、彩色图,并利用Opencv显示
共57个文件
tlog:12个
h:12个
lib:8个
5星 · 超过95%的资源 需积分: 33 338 下载量 164 浏览量
2016-12-15
09:59:49
上传
评论 30
收藏 9.6MB 7Z 举报
温馨提示
利用Kinect sdk2.0和opencv,获取深度图和彩色图,并实时显示。 详见博文http://blog.csdn.net/hust_bochu_xuchao/article/details/53665838 代码下载后可直接运行
资源推荐
资源详情
资源评论
收起资源包目录
kinect.7z (57个子文件)
test1214
Release
test1214.pdb 1.08MB
test1214.exe 18KB
test1214.v12.suo 27KB
Lib
x86
Kinect20.VisualGestureBuilder.lib 3KB
Kinect20.Fusion.lib 5KB
Kinect20.Face.lib 4KB
Kinect20.lib 6KB
x64
Kinect20.VisualGestureBuilder.lib 3KB
Kinect20.Fusion.lib 4KB
Kinect20.Face.lib 3KB
Kinect20.lib 5KB
inc
NuiKinectFusionApi.h 1KB
Kinect.h 356KB
Kinect.INPC.h 8KB
NuiKinectFusionVolume.h 30KB
Kinect.Face.h 107KB
NuiKinectFusionColorVolume.h 31KB
NuiKinectFusionDepthProcessor.h 24KB
Kinect.VisualGestureBuilder.h 47KB
NuiKinectFusionBase.h 3KB
NuiKinectFusionCameraPoseFinder.h 17KB
test1214.sdf 52MB
test1214
ImageRenderer.h 2KB
Release
vc120.pdb 900KB
ImageRenderer.obj 271KB
main.obj 1.36MB
test1214.log 2KB
test1214.tlog
CL.write.1.tlog 1KB
CL.read.1.tlog 51KB
cl.command.1.tlog 1KB
link.write.1.tlog 480B
test1214.lastbuildstate 172B
link.command.1.tlog 2KB
link.read.1.tlog 6KB
stdafx.h 2KB
ImageRenderer.cpp 5KB
test1214.vcxproj.filters 1KB
main.cpp 6KB
opencv2413debug.props 1KB
opencv2413release.props 1KB
Debug
vc120.pdb 1.97MB
ImageRenderer.obj 128KB
main.obj 289KB
test1214.log 2KB
vc120.idb 1.08MB
test1214.tlog
CL.write.1.tlog 1KB
CL.read.1.tlog 51KB
cl.command.1.tlog 3KB
link.write.1.tlog 570B
test1214.lastbuildstate 170B
link.command.1.tlog 2KB
link.read.1.tlog 5KB
test1214.vcxproj 5KB
test1214.sln 970B
Debug
test1214.pdb 2.32MB
test1214.ilk 540KB
test1214.exe 78KB
共 57 条
- 1
资源评论
- rongyu_wen2018-10-23解决燃眉之急,非常感谢
- 阮小末2017-05-11不错,可以参考一下
- Amber-coding2018-06-27资源下载了还没有开始用。
- lyangucas922018-05-16不错,谢谢分享
- jinbowei2018-05-25谢谢分享,参考下
徐大大平凡之路
- 粉丝: 355
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 52193906181644561181711717834527.jpg
- 图片缓冲技术(android)源码
- 花数据集+数据集汇总+标签txt+数据集汇总代码+迁移学习最佳模型+全部迭代最佳模型
- 20240329224412.zip
- switch.docx `switch`语句是C++中的一种流程控制语句,通常用于根据表达式的值选择执行不同的代码块 下面是`
- python绘制直方图-02-进程之间不共享全局变量.ev4.rar
- python绘制直方图-01-第三天知识点回顾.ev4.rar
- 01背包问题动态规划.docx
- 表达式求值.docx表达式求值涉及许多不同的情况和方法,具体取决于表达式的形式和要求的精度 下面是一个简单的例子
- python绘制直方图-08-软件的卸载.ev4.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功