1. 程式人生 > >tof 相機的資料讀取,depth data和amplitude data以及3D資料

tof 相機的資料讀取,depth data和amplitude data以及3D資料

1.開發前提

如果相機帶有SDK 也就是開發需要的工具以及包,就要用相機帶的開發包,裡面包含了相應的讀取檔案的函式,以及設定的相機的相關函式。

本文使用的是TTF相機,C++標頭檔案程式碼如下:

#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++程式碼如下:

#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.主函式的實現:

主函式未使用執行緒,只是比較簡單的資料的讀取 程式碼如下,

#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資料的顯示。