之前一直不知道如何实时保存RGB-D数据,每次都写入PCD文件不现实,对于30fps的,每秒就有30个PCD文件,硬盘速度绝逼跟不上。保存color和depth视频吧,总觉得不方便,而且depth压缩与解压缩会有精度损失。

后来老外提醒我:

/OpenNI2/Source/Tools/NiViewer    press s to record oni file

感觉很方便,于是自己参考了《openni cookbook》,开始罗代码了。没有解释,有 问题自己看那本书吧。

生成含有RBG-D的ONI文件:

#include <OpenNI.h>
#include <stdio.h>
using namespace openni;
char ReadLastCharOfLine()
{int newChar = 0;int lastChar;fflush(stdout);do{lastChar = newChar;newChar = getchar();}while ((newChar != '\n')&& (newChar != EOF));return (char)lastChar;
}
bool HandleStatus(Status status)
{if (status == STATUS_OK)return true;printf("ERROR: #%d, %s", status,OpenNI::getExtendedError());ReadLastCharOfLine();return false;
}int main()
{Status status = STATUS_OK;printf("Scanning machine for devices and loading " "modules/drivers ...\r\n");status = OpenNI::initialize();if (!HandleStatus(status))return 1;printf("Completed.\r\n");Device device;printf("Opening first device ...\r\n");status = device.open(ANY_DEVICE);if (!HandleStatus(status)) return 1;printf("%s Opened, Completed.\r\n",device.getDeviceInfo().getName());printf("Checking if stream is supported ...\r\n");if (!device.hasSensor(SENSOR_DEPTH)){printf("Stream not supported by this device.\r\n");return 1;}printf("Asking device to create a depth stream ...\r\n");VideoStream depthSensor;VideoStream colorSensor;status = depthSensor.create(device, SENSOR_DEPTH);if (!HandleStatus(status)) return 1;status = colorSensor.create(device, SENSOR_COLOR);if (!HandleStatus(status)) return 1;printf("Starting stream ...\r\n");status = depthSensor.start();if (!HandleStatus(status)) return 1;status = colorSensor.start();if (!HandleStatus(status)) return 1;printf("Done.\r\n");printf("Creating a recorder ...\r\n");Recorder recorder;status = recorder.create("sample.oni");if (!HandleStatus(status))return 1;printf("Done.\r\n");printf("Attaching to depth sensor ...\r\n");status = recorder.attach(depthSensor);if (!HandleStatus(status)) return 1;status = recorder.attach(colorSensor);if (!HandleStatus(status)) return 1;printf("Done.\r\n");printf("Starting recorder ...\r\n");status = recorder.start();if (!HandleStatus(status)) return 1;printf("Done. Now recording ...\r\n");ReadLastCharOfLine();recorder.destroy();depthSensor.destroy();colorSensor.destroy();device.close();OpenNI::shutdown();return 0;
}

读取上述ONI文件的RGB信息,并保存到PNG文件【代码是转载的,如果要读取深度信息,再加个 openni::SENSOR_DEPTH分支就行了】

#include <iostream>
#include <OpenNI.h>
#include <opencv2/photo.hpp>
#include <opencv2/highgui.hpp>  using namespace std;  int main()
{  //定义oni文件中视频的总帧数以及得到的图片的保存目录  int total = 0;  char* imagefile = "/home/jst/Data";  //初始化OpenNI环境  openni::OpenNI::initialize();  //声明设备并打开oni文件  openni::Device fromonifile;  fromonifile.open("sample.oni");  //声明控制对象,这对视频流的控制起到了关键作用  openni::PlaybackControl* pController = fromonifile.getPlaybackControl();  //声明视频流对象以及帧对象  openni::VideoStream streamColor;  openni::VideoFrameRef frameColor;  //验证是否有彩色传感器(是否有彩色视频)和建立与设备想关联的视频流  if(fromonifile.hasSensor(openni::SENSOR_COLOR))  {         if(streamColor.create( fromonifile, openni::SENSOR_COLOR ) == openni::STATUS_OK )  {  cout<<"建立视频流成功"<<endl;  }  else  {  cerr<<"ERROR: 建立视频流没有成功"<<endl;  system("pause");  return -1;  }  }  else  {  cerr << "ERROR: 该设备没有彩色传感器" << endl;  system("pause");  return -1;  }  //建立显示窗口  cv::namedWindow("Image");  //获取总的视频帧数并将该设备的速度设为-1以便能留出足够的时间对每一帧进行处理、显示和保存  total = pController->getNumberOfFrames(streamColor);  pController->setSpeed(-1);  //开启视频流  streamColor.start();  for (int i = 1;i <= total; ++ i)  {  //读取视频流的当前帧  streamColor.readFrame(&frameColor);  cout<<"当前正在读的帧数是:"<<frameColor.getFrameIndex()<<endl;  cout<<"当前的循环次数是:  "<<i<<endl;  //将帧保存到Mat中并且将其转换到BGR模式,因为在OpenCV中图片的模式是BGR  cv::Mat rgbImg(frameColor.getHeight(), frameColor.getWidth(), CV_8UC3, (void*)frameColor.getData());  cv::Mat bgrImg;  cvtColor(rgbImg, bgrImg, CV_RGB2BGR);  //将每一帧按顺序帧保存到图片目录下  char imagefullname[255];  char imagenum[50];  sprintf(imagenum,"/%03d.png",i);  strcpy(imagefullname,imagefile);  strcat(imagefullname,imagenum);  cv::imwrite(imagefullname,bgrImg);  //显示当前帧  cv::imshow("Image",bgrImg);  if (cv::waitKey(30) == 27)  {  break;  }  }  //销毁显示窗口  cv::destroyWindow("Image");   //关闭视频流  streamColor.destroy();  //关闭设备  fromonifile.close();  //关闭OpenNI  openni::OpenNI::shutdown();  return 0;
}

ONI文件生成与读取相关推荐

  1. Tensorflow—TFRecord文件生成与读取

    Tensorflow-TFRecord文件生成与读取 微信公众号:幼儿园的学霸 个人的学习笔记,关于OpenCV,关于机器学习, -.问题或建议,请公众号留言; 目录 文章目录 Tensorflow- ...

  2. Python先生,你好!(4)——npz、npy文件生成与读取

    Python先生,你好!(4)--npz.npy文件生成与读取 (一)前 言 (二)np.save()函数 (1)生成一个数组 (2)储存数组 (三)np.savez()函数 (1)生成两个数组 (2 ...

  3. npz、npy文件生成与读取

    在Numpy中提供了多种文件操作函数,我们可以通过这些文件操作函数,快速地对numpy数组进行存取,十分方便.下面介绍如何生成和读取npz.npy文件 1.np.save()函数可以存储一个np.ar ...

  4. plist文件生成与读取

    废话不多说,这里简明扼要的介绍一下plist文件的生成与读取 1.创建plist文件 2.代码读取plist文件 NSString *plistPath = [[NSBundle mainBundle ...

  5. C#如何实现XML文件生成和读取,XML文件的打开方式,解决方案!!!!!!

    以前对XML文件没什么了解,觉得很神奇,其实C#中的XML文件一般就是你们项目下的配置文件,由根节点和子节点组成. 接下来给大家说说XML文件最基础的读取和生成! 以下为C#程序XML文件生成的控制台 ...

  6. tfrecord文件生成与读取

    参考博客--tensorflow-TFRecord 文件详解 1. 生成tfrecord文件 代码 #1.创建tfrecord对象 tf_record=tf.python_io.TFRecordWri ...

  7. java中RSA数字证书生成,jks文件生成以及读取。

    一.Java代码生成cer证书文件: public class GetCertFile { //证书颁发者     static String CertificateIssuer = "C= ...

  8. python生成表格文件_python 读取excel文件生成sql文件实例详解

    python 读取excel文件生成sql文件实例详解 学了python这么久,总算是在工作中用到一次.这次是为了从excel文件中读取数据然后写入到数据库中.这个逻辑用java来写的话就太重了,所以 ...

  9. 利用opencv中的类FileStorage生成和读取XML和YAML文件

    有时候程序中的变量值.字符串.数组等数据也需要独立于源代码本身保存,这个时候就需要用到XML和YAML文件进行保存. OpenCV4提供了用于生成和读取XML文件和YAML文件的类FileStorag ...

最新文章

  1. 渗透测试工作流程渗透测试类型法律边界
  2. 【NLP】推荐一些NER的英文数据集
  3. boost::callable_traits的has_member_qualifiers的测试程序
  4. C++异常(exception)第一篇--综合讲解
  5. Js引擎解析执行 阅读笔记
  6. html5 视频 showtime,利用function showTime显示不出时间是为什么?
  7. install cuda5 on ubuntu12.04
  8. 设置tableview的滚动范围--iOS开发系列---项目中成长的知识三
  9. 【报告分享】2021年度私域经营洞察报告.pdf(附下载链接)
  10. jquery 自定义插件!
  11. 5.卷1(套接字联网API)---TCP客户/服务器程序示例
  12. TypeScript:语句
  13. Leetcode: One Edit Distance
  14. wsimport命令介绍
  15. B站下载视频之you-get的使用
  16. Building package xxx:xxx-windows failed with: BUILD_FAILED
  17. Ubuntu安装字体
  18. 思科FTP服务器如何传输文件,与FXP配置示例的ASA文件传输
  19. Windows平台下Fits格式文件读写C++库CCfits编译过程
  20. 用户评分系统设计与实现(风控方向)

热门文章

  1. 【博客话题】谈谈我工作的 入门恩师---“小武”
  2. java保存音频,文件保存音频数据
  3. mongodb评论功能实现
  4. iD学习 - 要素及属性
  5. 新建一个日期对象,格式为年月日
  6. Python代码阅读(第21篇):将变量名称转换为蛇式命名风格
  7. 对STP 的原理与配置的浅薄认识
  8. 计算机技术三大支柱,信息技术三大支柱常见七大传感器全解
  9. Device IPC-1
  10. 佳能相机里误删的照片怎么恢复