#include <stdio.h>
#include <Kinect.h>
#include <windows.h>
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <strstream>
using namespace cv;
using namespace std;
void GetColorDepthInfrared()
{
IKinectSensor* m_pKinectSensor;
IDepthFrameReader* m_pDepthFrameReader;
IDepthFrameSource* pDepthFrameSource = NULL;
IInfraredFrameSource* pInfraredFrameSource = NULL;
IInfraredFrameReader* InfraredFrameReader;
IColorFrameReader* ColorFrameReader = NULL;
IColorFrameSource* ColorFrameSource = NULL;
IFrameDescription* FrameDescription = NULL;
GetDefaultKinectSensor(&m_pKinectSensor);
//打开传感器
m_pKinectSensor->Open();
m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource);
m_pKinectSensor->get_InfraredFrameSource(&pInfraredFrameSource);
m_pKinectSensor->get_ColorFrameSource(&ColorFrameSource);
pDepthFrameSource->OpenReader(&m_pDepthFrameReader);
pInfraredFrameSource->OpenReader(&InfraredFrameReader);
ColorFrameSource->OpenReader(&ColorFrameReader);
while (true)
{
IInfraredFrame* InfraredFrame = NULL;
IDepthFrame* pDepthFrame = NULL;
IColorFrame* pColorFrame = NULL;
while (pDepthFrame == NULL){
m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame);
}
pDepthFrameSource->get_FrameDescription(&FrameDescription);
int depth_width, depth_height;
FrameDescription->get_Width(&depth_width);
FrameDescription->get_Height(&depth_height);
Mat DepthImg(depth_height, depth_width, CV_16UC1);
pDepthFrame->CopyFrameDataToArray(depth_height * depth_width, (UINT16 *)DepthImg.data);
//转换为8位图像
DepthImg.convertTo(DepthImg, CV_8UC1, 255.0 / 4500);
//均衡化
equalizeHist(DepthImg, DepthImg);
while (InfraredFrame == NULL){
InfraredFrameReader->AcquireLatestFrame(&InfraredFrame);
}
InfraredFrame->get_FrameDescription(&FrameDescription);
int nWidth, nHeight;
FrameDescription->get_Width(&nWidth);
FrameDescription->get_Height(&nHeight);
Mat InfraredImg(nHeight, nWidth, CV_16UC1);
InfraredFrame->CopyFrameDataToArray(nWidth * nHeight, (UINT16 *)InfraredImg.data);
while (pColorFrame == NULL){
ColorFrameReader->AcquireLatestFrame(&pColorFrame);
}
pColorFrame->get_FrameDescription(&FrameDescription);
int CWidth, CHeight;
FrameDescription->get_Width(&CWidth);
FrameDescription->get_Height(&CHeight);
Mat ColorImg(CHeight, CWidth, CV_8UC4);
pColorFrame->CopyConvertedFrameDataToArray(CWidth * CHeight * 4, (BYTE *)ColorImg.data, ColorImageFormat_Bgra);
//显示图像
resize(ColorImg, ColorImg, Size(512, 424));
imshow("InfraredImg", InfraredImg);
imshow("depthImg", DepthImg);
imshow("ColorImg", ColorImg);
DepthImg.release();
ColorImg.release();
InfraredImg.release();
if (pDepthFrame)
pDepthFrame->Release();
if (pColorFrame)
pColorFrame->Release();
if (InfraredFrame)
InfraredFrame->Release();
if (27 == waitKey(50))
break;
}
}
int main()
{
GetColorDepthInfrared();
}
kinect v2 同时获取红外图、深度图、彩色图
最新推荐文章于 2022-05-03 21:35:07 发布