您的位置:首页 > 其它

tof 相机的数据读取,depth data和amplitude data以及3D数据

2018-10-19 13:52 771 查看

1.开发前提

如果相机带有SDK 也就是开发需要的工具以及包,就要用相机带的开发包,里面包含了相应的读取文件的函数,以及设置的相机的相关函数。

本文使用的是TTF相机,C++头文件代码如下:

[code]#include "../../include/TTF_API.h"
#include <unistd.h>

using namespace std;
using namespace Voxel;

class Depth_Camera
{

public:
Depth_Camera();
~Depth_Camera();

TTF_API::_ttfDeviceInfo m_pDevInfo[MAX_DEVICE];

DepthFrame          *m_pDepthFrame;
XYZIPointCloudFrame *m_pPCLFrame;

int ReadDepthFrame(const DepthFrame* pDepthFrame);
int ReadPointCloudFrame(const XYZIPointCloudFrame* pXYZIPointCloudFrame);

bool System_init();
int Buffer_init();

void Data_Depth();
void Data_PCL();

void Device_Close();
void Device_Stop();

};

其中TTF::相关函数是相机自带的接口。

 

2.读取数据:

函数的实现部分,c++代码如下:

[code]#include "Depth_Camera.h"

Depth_Camera::Depth_Camera()
{

}

Depth_Camera::~Depth_Camera()
{

}

//读取相关内容
int Depth_Camera::ReadDepthFrame(const DepthFrame* pDepthFrame)
{
const DepthFrame *d = pDepthFrame;
const float* data;

data = d->depth.data();
cout<< "depth:"<< d->id << "@" << d->timestamp << " data:" <<  data[320*120+160] << endl;

return TTF_API::ERROR_NO;
}

int Depth_Camera::ReadPointCloudFrame(const XYZIPointCloudFrame* pXYZIPointCloudFrame)
{
const XYZIPointCloudFrame *d = pXYZIPointCloudFrame;
cout<< "pcl:" << d->id << "@" << d->timestamp << " data:" << d->points[320*120+160].z << endl;
return TTF_API::ERROR_NO;
}
//函数初始化
bool Depth_Camera::System_init()
{
int nDevCount;//device count
int nDevOpened;

TTF_API::ttfGetDeviceList(m_pDevInfo, nDevCount);

if (nDevCount < 1)
{
std::cerr << "No Camera Found!, Please connection check and restart." "Connection Error" << std::endl;
}
else
{
nDevOpened = TTF_API::ttfDeviceOpen(m_pDevInfo[0].hnd);

if (nDevOpened > 0)
{

//Callback Register //回调函数,

TTF_API::ttfRegister_Callback_DepthFrame(std::bind(&Depth_Camera::ReadDepthFrame, this, std::placeholders::_1));
TTF_API::ttfRegister_Callback_PointCloudFrame(std::bind(&Depth_Camera::ReadPointCloudFrame, this, std::placeholders::_1));

std::cout << "Camera[VID:" << m_pDevInfo[0].nVendorId << ", PID:" << m_pDevInfo[0].nProductId <<
", SerialNum:" << m_pDevInfo[0].szSerialNum << "] Open Success" << endl;

}
else
{
return TTF_API::ERROR_OPEN;
}

Buffer_init();
}

Data_Depth();//start Depth
//imshow("Binary", pDepthFrame);
return TTF_API::ERROR_NO;
}

int Depth_Camera::Buffer_init()
{
m_pDepthFrame   = (DepthFrame*)malloc(sizeof(DepthFrame));;
m_pPCLFrame     = (XYZIPointCloudFrame*)malloc(sizeof(XYZIPointCloudFrame));

if(m_pDepthFrame == NULL || m_pPCLFrame == NULL)
{
cout << "Failed to create nescecary buffers to display the image" << endl;
return TTF_API::ERROR_FAIL;
}
else
return TTF_API::ERROR_NO;
}

void Depth_Camera::Data_Depth()
{
TTF_API::ttfClearCallback(m_pDevInfo[0].hnd, TTF_API::FrameType::FRAME_XYZI_POINT_CLOUD_FRAME);
TTF_API::ttfDeviceStart(m_pDevInfo[0].hnd, TTF_API::FrameType::FRAME_DEPTH_FRAME);
}

void Depth_Camera::Data_PCL()
{
TTF_API::ttfClearCallback(m_pDevInfo[0].hnd, TTF_API::FrameType::FRAME_DEPTH_FRAME);
TTF_API::ttfDeviceStart(m_pDevInfo[0].hnd, TTF_API::FrameType::FRAME_XYZI_POINT_CLOUD_FRAME);
}

void Depth_Camera::Device_Close()
{
TTF_API::ttfDeviceStop(m_pDevInfo[0].hnd);
TTF_API::ttfDeviceClose(m_pDevInfo[0].hnd);
}

void Depth_Camera::Device_Stop()
{
TTF_API::ttfDeviceStop(m_pDevInfo[0].hnd);
}

3.主函数的实现:

主函数未使用线程,只是比较简单的数据的读取 代码如下,

[code]#include "Depth_Camera.h"

int main()
{
Depth_Camera *depth_camera = new Depth_Camera;

int nRet;
nRet = depth_camera->System_init();
if(nRet < 1)
{
cout << "init Error :" << nRet << endl;
exit(0);
}

char getkey;
while(1)
{
getkey = getchar();

//Reading end Process
if(getkey == 'q')
{
depth_camera->Device_Close();
break;
}
else if(getkey == '2')//Depth
{
depth_camera->Data_Depth();
}
else if(getkey == '3')//PCL
{
depth_camera->Data_PCL();
}
else if(getkey == 's')
{
depth_camera->Device_Stop();
}

}

if(depth_camera != NULL)
{
delete depth_camera;
depth_camera = NULL;
}

return 0;
}

4.今天只写数据的简单读取,下次完成深度数据以及3D数据的显示。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: