kinect2.0開発(一)深さ画像を読み取る
8326 ワード
コード実行環境
このコードはwindows 8 64ビットシステム上で実行され、vs 2013集積開発環境を採用し、win 32プログラムにコンパイルしてopencvライブラリを利用して画像を表示する
運転前準備
1.Microsoft kinect 2をインストールする.0 SDK 2.Opencv環境変数の構成は、次の手順を参照してください.http://tanghenxin.blog.163.com/blog/static/213511105201421244826743/ 3.kinect.hのライブラリファイルKinect 20.libはディレクトリを含む:C:Program FilesMicrosoft SDKsKinectv 2.0_1409incライブラリディレクトリ:C:Program FilesMicrosoft SDKsKinectv 2.0_1409\Lib\x86
深度画像を読み込むには kinectセンサ を取得するセンサ を開く.深さ情報センサ を取得する.深さフレームリーダ を開く.は、最も近いフレーム を得る.深さ情報をMAT形式 に変換する. opencvのimshowで を表示メモリの回収と解放 コードは次のとおりです.
実験結果は図:
このコードはwindows 8 64ビットシステム上で実行され、vs 2013集積開発環境を採用し、win 32プログラムにコンパイルしてopencvライブラリを利用して画像を表示する
運転前準備
1.Microsoft kinect 2をインストールする.0 SDK 2.Opencv環境変数の構成は、次の手順を参照してください.http://tanghenxin.blog.163.com/blog/static/213511105201421244826743/ 3.kinect.hのライブラリファイルKinect 20.libはディレクトリを含む:C:Program FilesMicrosoft SDKsKinectv 2.0_1409incライブラリディレクトリ:C:Program FilesMicrosoft SDKsKinectv 2.0_1409\Lib\x86
深度画像を読み込むには
// kinectSensorTest.cpp : 。
//
#include <stdio.h>
#include <Kinect.h>
#include <windows.h>
#include <highgui.h>
#include <cv.h>
using namespace cv;
// depth cv::Mat
Mat ConvertMat(const UINT16* pBuffer, int nWidth, int nHeight)
{
Mat img(nHeight, nWidth, CV_8UC1);
uchar* p_mat = img.data;//
const UINT16* pBufferEnd = pBuffer + (nWidth * nHeight);//
while (pBuffer < pBufferEnd)//16 65536
{
*p_mat++ = *pBuffer++ /65536.0 * 256;
}
return img;
}
int main()
{
IKinectSensor* m_pKinectSensor;
IDepthFrameReader* m_pDepthFrameReader;
IDepthFrame* pDepthFrame = NULL;
IFrameDescription* pFrameDescription = NULL;
IDepthFrameSource* pDepthFrameSource = NULL;
HRESULT hr = GetDefaultKinectSensor(&m_pKinectSensor);// kinect
assert(hr >= 0);
printf(" kinect
");
hr = m_pKinectSensor->Open();//
assert(hr >= 0);
hr = m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource);//
assert(hr >= 0);
hr = pDepthFrameSource->OpenReader(&m_pDepthFrameReader);//
assert(hr >= 0);
while (hr < 0 || pDepthFrame == NULL)
hr = m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame);// ,
assert(hr >= 0);
hr = pDepthFrame->get_FrameDescription(&pFrameDescription);// ( )
int depth_width, depth_height;
pFrameDescription->get_Width(&depth_width);
pFrameDescription->get_Height(&depth_height);
printf("width=%d height=%d
", depth_width, depth_height);
USHORT nDepthMinReliableDistance = 0;// 、
USHORT nDepthMaxReliableDistance = 0;
assert(hr >= 0);
hr = pDepthFrame->get_DepthMinReliableDistance(&nDepthMinReliableDistance);
assert(hr >= 0);
hr = pDepthFrame->get_DepthMaxReliableDistance(&nDepthMaxReliableDistance);
printf("nDepthMinReliableDistance=%d nDepthMaxReliableDistance=%d
", nDepthMinReliableDistance, nDepthMaxReliableDistance);
UINT nBufferSize_depth = 0;
UINT16 *pBuffer_depth = NULL;
pDepthFrame->AccessUnderlyingBuffer(&nBufferSize_depth, &pBuffer_depth);//
// MAT
Mat depthImg_show = ConvertMat(pBuffer_depth, depth_width, depth_height);// 8 mat
equalizeHist(depthImg_show, depthImg_show);// ,
imwrite("MyFirstKinectImg.jpg", depthImg_show);//
// opencv
namedWindow("display");
imshow("display", depthImg_show);
if (27 == waitKey(0))
return 0;
}
実験結果は図: