kinect彩色数据的获取以及在opencv中显示
来源:互联网 发布:中医方剂大辞典数据库 编辑:程序博客网 时间:2024/04/26 06:18
一个非常简单的kinect彩色图像获取的示例
- 一个非常简单的kinect彩色图像获取的示例
- 说明
- 其它kinect教程分享
- 代码
说明
刚开始接触kinect,发现官方的教程写得都比较难,作为小白的我不怎么看得懂,所以就准备写几个非常的示例,方便自己的学习。
平台:
- kinect2.0
- kinect for windows sdk2.0
- opencv2.4.9
- win8.1
- vs2013
其它kinect教程分享
http://guoming.me/category/technique/kinect
http://blog.csdn.net/column/details/k4w2dn.html
代码
下面的示例属于win32控制台应用代码 base.h
#pragma once#include <iostream>#include "opencv2/opencv.hpp"#include <Kinect.h>#pragma comment(lib,"Kinect20.lib")//分两个模式引入文件不然会出问题#ifdef _DEBUG#pragma comment(lib,"opencv_core249d.lib")#pragma comment(lib,"opencv_highgui249d.lib")#pragma comment(lib,"opencv_imgproc249d.lib")#endif#ifndef _DEBUG#pragma comment(lib,"opencv_core249.lib")#pragma comment(lib,"opencv_highgui249.lib")#pragma comment(lib,"opencv_imgproc249.lib")#endifusing namespace std;using namespace cv;// Safe release for interfacestemplate<class Interface>inline void SafeRelease(Interface *& pInterfaceToRelease){ if (pInterfaceToRelease != NULL) { pInterfaceToRelease->Release(); pInterfaceToRelease = NULL; }}
main.cpp
#include"base.h"HRESULT main(){ /* 定义 */ IKinectSensor* m_pKinectSensor = NULL; IColorFrameSource* m_pColorFrameSource = NULL; IColorFrameReader* m_pColorFrameReader = NULL; IColorFrame* m_pColorFrame; IFrameDescription* m_pFrameDescription = NULL; RGBQUAD* m_pColorRGBX = NULL; static const int cColorWidth = 1920; static const int cColorHeight = 1080; ColorImageFormat imageFormat = ColorImageFormat_None; int nWidth = 0; int nHeight = 0; UINT nBufferSize = 0; RGBQUAD *pBuffer = NULL; /* 初始化 */ m_pColorRGBX = new RGBQUAD[cColorWidth * cColorHeight]; HRESULT hr; hr = GetDefaultKinectSensor(&m_pKinectSensor); if (FAILED(hr)) { return hr; cout << "获得默认的Kinect设备失败" << endl; } else { cout << "获得默认的Kinect设备成功" << endl; } hr = m_pKinectSensor->Open(); if (SUCCEEDED(hr)) { hr = m_pKinectSensor->get_ColorFrameSource(&m_pColorFrameSource); } if (SUCCEEDED(hr)) { hr = m_pColorFrameSource->OpenReader(&m_pColorFrameReader); } if (!m_pKinectSensor || FAILED(hr)) { cout << "没有可用的Kinect" << endl; } else { cout << "打开Kinect成功" << endl; } SafeRelease(m_pColorFrameSource); /* 读取数据 */ while (true) { if (!m_pColorFrameReader) { return S_FALSE; } //不是每一次都能获取到m_pColorFrame,因为kinect的彩色帧的帧率是固定的30或者15(看灯光) 而且刚开始时候过来的数据是有问题的不能被正常解析 hr = m_pColorFrameReader->AcquireLatestFrame(&m_pColorFrame); if ( SUCCEEDED(hr) ) { hr = m_pColorFrame->get_FrameDescription(&m_pFrameDescription); cout << "获取数据成功" << endl; } else { cout << " 获取数据失败" << endl; } if (SUCCEEDED(hr)) { hr = m_pFrameDescription->get_Width(&nWidth); } if (SUCCEEDED(hr)) { hr = m_pFrameDescription->get_Height(&nHeight); } if (SUCCEEDED(hr)) { hr = m_pColorFrame->get_RawColorImageFormat(&imageFormat); } if (SUCCEEDED(hr)) { if (imageFormat == ColorImageFormat_Bgra) { hr = m_pColorFrame->AccessRawUnderlyingBuffer(&nBufferSize, reinterpret_cast<BYTE**>(&pBuffer)); } else if (m_pColorRGBX) { pBuffer = m_pColorRGBX; nBufferSize = cColorWidth * cColorHeight * sizeof(RGBQUAD); hr = m_pColorFrame->CopyConvertedFrameDataToArray(nBufferSize,reinterpret_cast<BYTE*>(pBuffer),ColorImageFormat_Bgra); } else { return E_FAIL; } } SafeRelease(m_pColorFrame); //关键的地方每次要记得释放 //转化成Mat Mat showImg; if( pBuffer && ( nWidth == cColorWidth) && (nHeight == cColorHeight)) { Mat img(cColorHeight, cColorWidth, CV_8UC4, pBuffer); resize(img, showImg, Size(cColorWidth / 2, cColorHeight / 2)); } else { cout << "pBuffer = " << pBuffer << endl; cout << "nWidth = " << nWidth << " " << "nHeight = " << cColorWidth<< endl; cout << "nHeight = " << nHeight << " " << "cColorHeight = " << cColorHeight << endl; cout << "数据转化有误" << endl; showImg = Mat(cColorHeight / 2, cColorWidth / 2,CV_8UC4,Scalar(0,0,0,0)); } imshow("ColorImg", showImg); waitKey(1000/30); } /* 释放 上面申请的Kinect接口都应该释放 */ //waitKey(); return S_OK;}
效果图:
图只是截了了一半,环境有点乱,见笑了。。。
0 0
- kinect彩色数据的获取以及在opencv中显示
- Kinect V2彩色数据用OpenCV显示
- Kinect V2彩色数据用OpenCV显示
- kinect,openni,opencv 获取并显示深度与彩色图像
- Ogre中显示Kinect的彩色图像
- Kinect基础之在WinForm(C#)中显示彩色数据和深度数据
- Kinect+OpenNI学习笔记之3(获取kinect的数据并在Qt中显示的类的设计)
- 在pcl用例中获取kinect深度图像数据,并用opencv显示出来
- Kinect获取彩色图像(Kinect + OpenCV 2.3.0)
- OpenCV——Opencv彩色图像在matplot中显示问题的解决
- Kinect for Windows V2和V1对比开发___彩色数据获取并用OpenCV2.4.10显示
- Kinect for Windows V2和V1对比开发___彩色数据获取并用OpenCV2.4.10显示
- Kinect SDK1.0 OpenCV显示彩色、深度、骨骼图
- OpenCV--鼠标响应Kinect彩色图像显示深度信息
- OpenNI结合OpenCV显示Kinect彩色、深度及融合图像
- Kinect 2 SDK + OpenCV 获取深度彩色图像
- 透过 OpneNI 合并 Kinect 深度以及彩色影像数据
- Kinect for Windows SDK——FreeImage显示彩色数据
- HDFS文件的合并
- Java学习(1)
- 创建了博客
- 大话数据结构-宏观把控
- HDU5074(DP)
- kinect彩色数据的获取以及在opencv中显示
- rust里调试panic
- Codeforces Round #323 (Div. 2) B. Robot's Task
- 指针的指针
- 从Azure虚拟机下载文件到本地五种方法
- MFC之时间类demo
- java.lang.SecurityException: Binder invocation to an incorrect interface进程通信异常
- Codeforces Round #323 (Div. 2) C.GCD Table
- thinkphp3.x中import方法导入类库的用法