kinect 2.0 学习笔记_深度相机内参

深度相机内参和红外相机是一样的。今天来学习一下如何通过kinect的API求得内参。

代码:

#include "kinect.h"
#include 
#include 
#include 
#include 


#include   

using namespace cv;
using namespace std;
// 安全释放指针
template
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
	if (pInterfaceToRelease != NULL)
	{
		pInterfaceToRelease->Release();
		pInterfaceToRelease = NULL;
	}
}


int main() {
	int flag = 1;
	// 获取Kinect设备
	IKinectSensor*  m_pKinectSensor;
	// 映射器
	ICoordinateMapper*  m_pCoordinateMapper = NULL;
	HRESULT hr;
	hr = GetDefaultKinectSensor(&m_pKinectSensor);
	if (FAILED(hr) )
	{
		return hr;
	}
	IMultiSourceFrameReader* m_pMultiFrameReader = NULL;
	if (m_pKinectSensor)
	{
		hr = m_pKinectSensor->Open();
		if (SUCCEEDED(hr))
		{
			// 获取多数据源到读取器  
			hr = m_pKinectSensor->OpenMultiSourceFrameReader(
				FrameSourceTypes::FrameSourceTypes_Color |
				FrameSourceTypes::FrameSourceTypes_Infrared |
				FrameSourceTypes::FrameSourceTypes_Depth,
				&m_pMultiFrameReader);
		}
	}

	if (!m_pKinectSensor || FAILED(hr))
	{
		return E_FAIL;
	}

	// 三个数据帧及引用
	IInfraredFrame* m_pInfraredFrame = NULL;
	IDepthFrame* m_pDepthFrame = NULL;
	IColorFrame* m_pColorFrame = NULL;


	CameraIntrinsics* m_pCameraIntrinsics = new CameraIntrinsics();
	hr = m_pKinectSensor->get_CoordinateMapper(&m_pCoordinateMapper);
	if (FAILED(hr))
	{
		return hr;
	}
	//得到内参
	while (flag==1) {
		//获取深度相机内参(要好几千帧照片才能得到,否则数据全部为0)
		m_pCoordinateMapper->GetDepthCameraIntrinsics(m_pCameraIntrinsics);//GetDepthCameraIntrinsics:获得ir相机参数(出厂已写入设备)
		if (m_pCameraIntrinsics->FocalLengthX && flag ==1) 
		{
			// 打印
			cout << "FocalLengthX : " << m_pCameraIntrinsics->FocalLengthX << endl; //摄像机的X焦距,以像素为单位
			cout << "FocalLengthY : " << m_pCameraIntrinsics->FocalLengthY << endl; //相机的Y焦距,以像素为单位
			cout << "PrincipalPointX : " << m_pCameraIntrinsics->PrincipalPointX << endl;//相机在X维中的主要点,以像素为单位。
			cout << "PrincipalPointY : " << m_pCameraIntrinsics->PrincipalPointY << endl; //相机在Y轴上的主要点,以像素为单位。
			cout << "RadialDistortionFourthOrder : " << m_pCameraIntrinsics->RadialDistortionFourthOrder << endl;//相机的四阶径向畸变参数。
			cout << "RadialDistortionSecondOrder : " << m_pCameraIntrinsics->RadialDistortionSecondOrder << endl;//相机的二阶径向畸变参数。
			cout << "RadialDistortionSixthOrder : " << m_pCameraIntrinsics->RadialDistortionSixthOrder << endl;//摄像机的六阶径向畸变参数。
			flag = 0;
		}	
	}
	 //保存到01.txt文本
	ofstream fout("01.txt"); ////以输出方式打开文件    如果想以输入/输出方式来打开,就用fstream来定义
	fout << "FocalLengthX :  " << m_pCameraIntrinsics->FocalLengthX <FocalLengthY << endl; //向文件写入数据
	fout << "PrincipalPointX : " << m_pCameraIntrinsics->PrincipalPointX << endl;//相机在X维中的主要点,以像素为单位。
	fout << "PrincipalPointY : " << m_pCameraIntrinsics->PrincipalPointY << endl; //相机在Y轴上的主要点,以像素为单位。
	fout << "RadialDistortionFourthOrder : " << m_pCameraIntrinsics->RadialDistortionFourthOrder << endl;//相机的四阶径向畸变参数。
	fout << "RadialDistortionSecondOrder : " << m_pCameraIntrinsics->RadialDistortionSecondOrder << endl;//相机的二阶径向畸变参数。
	fout << "RadialDistortionSixthOrder : " << m_pCameraIntrinsics->RadialDistortionSixthOrder << endl;//摄像机的六阶径向畸变参数。
	// 关闭窗口,设备
	cv::destroyAllWindows();
	m_pKinectSensor->Close();
	SafeRelease(m_pCoordinateMapper); // 安全释放指针
	SafeRelease(m_pKinectSensor);// 安全释放指针
	return 0;
}
结果:

kinect 2.0 学习笔记_深度相机内参_第1张图片

API介绍:

https://docs.microsoft.com/en-us/previous-versions/windows/kinect/dn785315(v=ieb.10)


版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/qq_37791134

你可能感兴趣的:(KinectV2)