主要介绍
起因
毕业设计需要自己采集RGB图像和深度图像,就在咸鱼上面淘到了一个RGBD摄像机,230块钱。在使用过程中走了一些弯路,在这分享以下,为后来人提个醒。文章后面会分享完整的C++代码,文章里面也会介绍到python的方法。
实现方法
数据采集
由于摄像头硬件问题,官方给的软件也无法同时采集深度和彩色图像,所以只能自己写程序。深度图的采集使用OpenNI框架,彩色图像使用OpenCV。
单纯采集深度图和RGB图像可以直接使用python,分别调用opencv库和openni库就行,其中openni库就是C++函数库的python封装。程序可以参考这篇文章,具体代码就自己看吧,openni的包直接可以在奥比中光的官网下载。
python虽然封装的比较号,但是在使用硬件配准的时候,传值总是报错。不知道是不是一代比较垃圾的原因。但是使用C++编写采集程序的时候就可以使用硬件配准。
下面是C++采集图像的完整程序:
//test.cpp
#include <opencv2/opencv.hpp>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include <string>
#include "OpenNI.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include<sys/stat.h>
#include<sys/types.h>
using namespace std;
using namespace cv;
using namespace openni;
void writeMatToXML(const std::string xmlName, const cv::Mat & mat)
{
FileStorage fs(xmlName, FileStorage::WRITE);
fs << "Mat" << mat;
fs.release();
};
void CheckOpenNIError( Status result, string status )
{
if( result != STATUS_OK )
cerr << status << " Error: " << OpenNI::getExtendedError() << endl;
};
class ScenceNumber
{
// use a file(number.txt) to recoder different scence
public:
int scNum;
string fileName = "./data/number.txt";
ScenceNumber()
{
ifstream f;
f.open(this->fileName);
if(f.good<