资源简介
自己做的基于VS2013+opencv2.4.10+kinect 2.0的深度图和彩色图的显示以及二者的对齐(或配准registration)。希望对大家有帮助。
代码片段和文件信息
#include “mykinect.h“
#include
using namespace std;
using namespace cv;
/// Constructor 构造函数
CMapBasics::CMapBasics()
{
sensor = NULL;
mapper = NULL;
reader = NULL;
dpoint = NULL;
// create heap storage for color pixel data in RGBX format
m_pColorRGBX = new RGBQUAD[cColorWidth * cColorHeight];
// create heap storage for the coorinate mapping from color to depth
dpoint = new DepthSpacePoint[cColorWidth * cColorHeight];
//初始化OpenCV数组
depthImg.create(cDepthHeight cDepthWidth CV_8UC1);
colorImg.create(cColorHeight cColorWidth CV_8UC3);
depthImg.setTo(0); colorImg.setTo(0);
}
/// Destructor 析构函数
CMapBasics::~CMapBasics()
{
if (m_pColorRGBX)
{
delete[] m_pColorRGBX;
m_pColorRGBX = NULL;
}
if (dpoint)
{
delete[] dpoint;
dpoint = NULL;
}
// done with frame reader
SafeRelease(reader);
// done with coordinate mapper
SafeRelease(mapper);
if (sensor)
{
sensor->Close();
}
SafeRelease(sensor);
}
// 转换depth图像到Mat
Mat ConvertMat(const UINT16* pBuffer int nWidth int nHeight USHORT nMinDepth USHORT nMaxDepth)
{
Mat img(nHeight nWidth CV_8UC1);
uchar* p_mat = img.data;
const UINT16* pBufferEnd = pBuffer + (nWidth * nHeight);
while (pBuffer < pBufferEnd)
{
USHORT depth = *pBuffer;
//方法1:将符合边界条件的uint16去尾赋值给Mat,不符合边界的置0
BYTE intensity = static_cast(depth >= nMinDepth) && (depth <= nMaxDepth) ? depth % 256 : 0;
//方法2:直接将uint16去尾赋值给Mat
//BYTE intensity = depth % 256;
*p_mat = intensity;
p_mat++;
++pBuffer;
}
return img;
}
//转换color图像到Mat
Mat ConvertMat(const RGBQUAD* pBuffer int nWidth int nHeight)
{
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;
}
int main()
{
CMapBasics myKinect;
HRESULT hr = myKinect.InitKinect();
if (SUCCEEDED(hr)){
while (1){
myKinect.Update();
}
}
else{
cout << “kinect initialization failed!“ << endl;
system(“pause“);
}
}
HRESULT CMapBasics::InitKinect()
{
HRESULT hr;
hr = GetDefaultKinectSensor(&sensor);
if (FAILED(hr))
{
return hr;
}
if (sensor)
{
hr = sensor->Open();
// Initialize the Kinect and get coordinate mapper and the frame reader
if (SUCCEEDED(hr))
{
hr = sensor->get_CoordinateMapper(&mapper);
}
if (SUCCEEDED(hr))
{
hr = sensor->OpenMultiSourceframeReader(
frameSourceTypes::frameSourceTypes_Depth | frameSourceTypes::frameSourceTypes_Color &reader);
}
}
if (!sensor || FAILED(hr))
{
return E_FAIL;
}
return hr;
}
void CMapBasics::Update()
{
if (!reader)
{
return;
}
HRESULT hr = reader->AcquireLatestframe(&pMultiSourceframe);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1174 2016-05-21 16:05 registration\registration\mykinect.h
文件 1346 2016-05-12 12:26 registration\registration\opencv2410x64.Debug.props
文件 1247 2016-05-12 12:32 registration\registration\opencv2410x64.Release.props
文件 41144 2016-05-12 15:18 registration\registration\PCLx64.Debug.props
文件 7338 2016-05-20 11:17 registration\registration\registration.vcxproj
文件 1065 2016-05-20 11:17 registration\registration\registration.vcxproj.filters
文件 1912 2016-05-21 16:30 registration\registration\x64\Debug\registration.log
文件 612 2016-05-21 16:30 registration\registration\x64\Debug\registration.tlog\cl.command.1.tlog
文件 27268 2016-05-21 16:30 registration\registration\x64\Debug\registration.tlog\CL.read.1.tlog
文件 488 2016-05-21 16:30 registration\registration\x64\Debug\registration.tlog\CL.write.1.tlog
文件 2080 2016-05-21 16:30 registration\registration\x64\Debug\registration.tlog\li
文件 5042 2016-05-21 16:30 registration\registration\x64\Debug\registration.tlog\li
文件 480 2016-05-21 16:30 registration\registration\x64\Debug\registration.tlog\li
文件 162 2016-05-21 16:30 registration\registration\x64\Debug\registration.tlog\registration.lastbuildstate
文件 1108992 2016-05-21 16:30 registration\registration\x64\Debug\vc120.idb
文件 1642496 2016-05-21 16:30 registration\registration\x64\Debug\vc120.pdb
文件 331023 2016-05-21 16:30 registration\registration\x64\Debug\源.obj
文件 1899 2016-05-22 13:43 registration\registration\x64\Release\registration.log
文件 632 2016-05-22 13:43 registration\registration\x64\Release\registration.tlog\cl.command.1.tlog
文件 27206 2016-05-22 13:43 registration\registration\x64\Release\registration.tlog\cl.read.1.tlog
文件 366 2016-05-22 13:43 registration\registration\x64\Release\registration.tlog\cl.write.1.tlog
文件 2116 2016-05-22 13:43 registration\registration\x64\Release\registration.tlog\li
文件 5336 2016-05-22 13:43 registration\registration\x64\Release\registration.tlog\li
文件 374 2016-05-22 13:43 registration\registration\x64\Release\registration.tlog\li
文件 164 2016-05-22 13:43 registration\registration\x64\Release\registration.tlog\registration.lastbuildstate
文件 1249280 2016-05-22 13:43 registration\registration\x64\Release\vc120.pdb
文件 1401796 2016-05-22 13:43 registration\registration\x64\Release\源.obj
文件 7069 2016-05-22 13:43 registration\registration\源.cpp
文件 45809664 2016-05-22 16:56 registration\registration.sdf
文件 1336 2016-05-20 11:17 registration\registration.sln
............此处省略19个文件信息
- 上一篇:Cosmo Worlds
- 下一篇:增长秘籍3.0 终版
评论
共有 条评论