基于VS2025+Opencv+Kinect V2 SDK实现的彩色、深度、红外图像的存储代码

#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();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章