#include <stdio.h>
#include <Kinect.h>
#include <windows.h>
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
#include <opencv2\core.hpp>
#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;
}
}
void StoreColorDepthInfraredVideo()
{
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);
VideoWriter writer;
// writer.open("color.mp4", CAP_OPENCV_MJPEG, 30, Size(1920, 1080));
// writer.open("depth.mp4", CAP_OPENCV_MJPEG, 30, Size(512, 424));
writer.open("infrare.mp4", CAP_OPENCV_MJPEG, 20, Size(512, 424));
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);
// 實現視頻的存儲時必須要添加如下兩行,因爲視頻存儲只支持uchar類型
//轉換爲8位圖像
InfraredImg.convertTo(InfraredImg, CV_8UC1, 255.0/13000);
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);
// 存儲彩色圖像
// writer.write(ColorImg);
// 存儲深度圖像
// writer.write(DepthImg);
// 存儲紅外圖像
writer.write(InfraredImg);
//顯示圖像
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;
}
writer.release();
}
int main()
{
// 深度、彩色、紅外圖像顯示
// GetColorDepthInfrared();
// 深度、彩色、紅外圖像存儲
StoreColorDepthInfraredVideo();
}