OpenNI2和OpenCV2获取深度图rc = depth.readFrame(&frame);

关于OpenNI2和OpenCV2的那些事——获取彩色图和深度图(XtionProLive)

 

上一节讲述了搭环境时遇到的挫折,这一节我们来讲讲如何使用XtionProLive(XtionPro没有彩色摄像头,Live版才有)获取彩色图和数度图,以及彩色图的放大与水平镜像。(PS: 对比两代OpenNI,2真的比1要简洁得多,使用OpenNI2编程序,代码简单易懂。)

首先初始化环境:

OpenNI::initialize();

创建状态:

Status rc = STATUS_OK;

接着声明并打开设备:

 

 
  1. Device xtion;

  2. const char * deviceURL = openni::ANY_DEVICE; //设备名

  3. rc = xtion.open(deviceURL);

创建深度数据流和彩色数据流:

 
  1. VideoStream streamDepth;

  2. VideoStream streamColor;

打开数据流并对齐彩色/深度图像:

 
  1. rc = streamDepth.start();

  2. rc = streamColor.start();

  3.  
  4. // 图像模式注册,彩色图与深度图对齐

  5. if (xtion.isImageRegistrationModeSupported(

  6. <span style="white-space:pre"> </span>IMAGE_REGISTRATION_DEPTH_TO_COLOR))

  7. {

  8. xtion.setImageRegistrationMode(IMAGE_REGISTRATION_DEPTH_TO_COLOR);

  9. }

从数据流中读取数据保存到VideoFrameRef中并用opencv显示出来:

 
  1. while (true)

  2. {

  3. // 读取数据流

  4. rc = streamDepth.readFrame(&frameDepth);

  5. if (rc == STATUS_OK)

  6. {

  7. // 将深度数据转换成OpenCV格式

  8. const Mat mImageDepth(frameDepth.getHeight(), frameDepth.getWidth(), CV_16UC1, (void*)frameDepth.getData());

  9. Mat mScaledDepth, hScaledDepth;

  10. mImageDepth.convertTo(mScaledDepth, CV_8U, 255.0 / iMaxDepth);

  11. // 显示出深度图像

  12. <span style="white-space:pre"> </span>imshow("Depth Image", hScaledDepth);

  13. }

  14.  
  15. rc = streamColor.readFrame(&frameColor);

  16. if (rc == STATUS_OK)

  17. {

  18. // 同样的将彩色图像数据转化成OpenCV格式

  19. const Mat mImageRGB(frameColor.getHeight(), frameColor.getWidth(), CV_8UC3, (void*)frameColor.getData());

  20. // 首先将RGB格式转换为BGR格式

  21. Mat cImageBGR,bImageBGR,hImageBGR;

  22. cvtColor(mImageRGB, cImageBGR, CV_RGB2BGR);

  23. // 然后显示彩色图像

  24. imshow("Color Image", hImageBGR);

  25. }

  26.  
  27. // 终止快捷键

  28. if (waitKey(1) == 27)

  29. break;

  30. }

如图所示,彩色图和深度图默认都是320*240:

接着我们对彩色流和深度流做一些格式设置:

 
  1. // 设置深度图像视频模式

  2. VideoMode mModeDepth;

  3. // 分辨率大小

  4. mModeDepth.setResolution(640, 480);

  5. // 每秒30帧

  6. mModeDepth.setFps(30);

  7. // 像素格式

  8. mModeDepth.setPixelFormat(PIXEL_FORMAT_DEPTH_1_MM);

  9.  
  10. streamDepth.setVideoMode(mModeDepth);

 
  1. // 同样的设置彩色图像视频模式

  2. VideoMode mModeColor;

  3. mModeColor.setResolution(320, 240);

  4. mModeColor.setFps(30);

  5. mModeColor.setPixelFormat(PIXEL_FORMAT_RGB888);

  6. streamColor.setVideoMode(mModeColor);

结果如图所示:

 

经过尝试,发现对于深度图来说,320*240/640*480都可以,而彩色图像只能320*240。所以我们使用resize()把彩色的320*240强制转化为640*480,默认采用最近邻差值来补充缺失的像素点,结果如图:

这时候我们发现图像左右相反,这是因为体感RGBD相机一般是捕捉人体的,所以是面向人体的捕捉图像。而此时我们把相机面向了我们的前方,用于以后捕捉环境图像三维重建,所以我们还需要对彩色图和深度图进行水平镜像处理,函数如下:

 
  1. void hMirrorTrans(const Mat &src, Mat &dst)

  2. {

  3. dst.create(src.rows, src.cols, src.type());

  4.  
  5. int rows = src.rows;

  6. int cols = src.cols;

  7.  
  8. switch (src.channels())

  9. {

  10. case 1: //1通道比如深度图像

  11. const uchar *origal;

  12. uchar *p;

  13. for (int i = 0; i < rows; i++){

  14. origal = src.ptr<uchar>(i);

  15. p = dst.ptr<uchar>(i);

  16. for (int j = 0; j < cols; j++){

  17. p[j] = origal[cols - 1 - j];

  18. }

  19. }

  20. break;

  21. case 3: //3通道比如彩色图像

  22. const Vec3b *origal3;

  23. Vec3b *p3;

  24. for (int i = 0; i < rows; i++) {

  25. origal3 = src.ptr<Vec3b>(i);

  26. p3 = dst.ptr<Vec3b>(i);

  27. for (int j = 0; j < cols; j++){

  28. p3[j] = origal3[cols - 1 - j];

  29. }

  30. }

  31. break;

  32. default:

  33. break;

  34. }

  35. }

处理结果如下:

 

完整代码如下:

 
  1. #include <iostream>

  2. #include <OpenNI.h>

  3. #include <opencv2/core/core.hpp>

  4. #include <opencv2/highgui/highgui.hpp>

  5. #include <opencv2/imgproc/imgproc.hpp>

  6.  
  7. using namespace std;

  8. using namespace openni;

  9. using namespace cv;

  10.  
  11. void showdevice(){

  12. // 获取设备信息

  13. Array<DeviceInfo> aDeviceList;

  14. OpenNI::enumerateDevices(&aDeviceList);

  15.  
  16. cout << "电脑上连接着 " << aDeviceList.getSize() << " 个体感设备." << endl;

  17.  
  18. for (int i = 0; i < aDeviceList.getSize(); ++i)

  19. {

  20. cout << "设备 " << i << endl;

  21. const DeviceInfo& rDevInfo = aDeviceList[i];

  22. cout << "设备名: " << rDevInfo.getName() << endl;

  23. cout << "设备Id: " << rDevInfo.getUsbProductId() << endl;

  24. cout << "供应商名: " << rDevInfo.getVendor() << endl;

  25. cout << "供应商Id: " << rDevInfo.getUsbVendorId() << endl;

  26. cout << "设备URI: " << rDevInfo.getUri() << endl;

  27.  
  28. }

  29. }

  30.  
  31. void hMirrorTrans(const Mat &src, Mat &dst)

  32. {

  33. dst.create(src.rows, src.cols, src.type());

  34.  
  35. int rows = src.rows;

  36. int cols = src.cols;

  37.  
  38. switch (src.channels())

  39. {

  40. case 1: //1通道比如深度图像

  41. const uchar *origal;

  42. uchar *p;

  43. for (int i = 0; i < rows; i++){

  44. origal = src.ptr<uchar>(i);

  45. p = dst.ptr<uchar>(i);

  46. for (int j = 0; j < cols; j++){

  47. p[j] = origal[cols - 1 - j];

  48. }

  49. }

  50. break;

  51. case 3: //3通道比如彩色图像

  52. const Vec3b *origal3;

  53. Vec3b *p3;

  54. for (int i = 0; i < rows; i++) {

  55. origal3 = src.ptr<Vec3b>(i);

  56. p3 = dst.ptr<Vec3b>(i);

  57. for (int j = 0; j < cols; j++){

  58. p3[j] = origal3[cols - 1 - j];

  59. }

  60. }

  61. break;

  62. default:

  63. break;

  64. }

  65.  
  66. }

  67.  
  68. int main()

  69. {

  70. Status rc = STATUS_OK;

  71.  
  72. // 初始化OpenNI环境

  73. OpenNI::initialize();

  74.  
  75. showdevice();

  76.  
  77. // 声明并打开Device设备。

  78. Device xtion;

  79. const char * deviceURL = openni::ANY_DEVICE; //设备名

  80. rc = xtion.open(deviceURL);

  81.  
  82. // 创建深度数据流

  83. VideoStream streamDepth;

  84. rc = streamDepth.create(xtion, SENSOR_DEPTH);

  85. if (rc == STATUS_OK)

  86. {

  87. // 设置深度图像视频模式

  88. VideoMode mModeDepth;

  89. // 分辨率大小

  90. mModeDepth.setResolution(640, 480);

  91. // 每秒30帧

  92. mModeDepth.setFps(30);

  93. // 像素格式

  94. mModeDepth.setPixelFormat(PIXEL_FORMAT_DEPTH_1_MM);

  95.  
  96. streamDepth.setVideoMode(mModeDepth);

  97.  
  98. // 打开深度数据流

  99. rc = streamDepth.start();

  100. if (rc != STATUS_OK)

  101. {

  102. cerr << "无法打开深度数据流:" << OpenNI::getExtendedError() << endl;

  103. streamDepth.destroy();

  104. }

  105. }

  106. else

  107. {

  108. cerr << "无法创建深度数据流:" << OpenNI::getExtendedError() << endl;

  109. }

  110.  
  111. // 创建彩色图像数据流

  112. VideoStream streamColor;

  113. rc = streamColor.create(xtion, SENSOR_COLOR);

  114. if (rc == STATUS_OK)

  115. {

  116. // 同样的设置彩色图像视频模式

  117. VideoMode mModeColor;

  118. mModeColor.setResolution(320, 240);

  119. mModeColor.setFps(30);

  120. mModeColor.setPixelFormat(PIXEL_FORMAT_RGB888);

  121.  
  122. streamColor.setVideoMode(mModeColor);

  123.  
  124. // 打开彩色图像数据流

  125. rc = streamColor.start();

  126. if (rc != STATUS_OK)

  127. {

  128. cerr << "无法打开彩色图像数据流:" << OpenNI::getExtendedError() << endl;

  129. streamColor.destroy();

  130. }

  131. }

  132. else

  133. {

  134. cerr << "无法创建彩色图像数据流:" << OpenNI::getExtendedError() << endl;

  135. }

  136.  
  137. if (!streamColor.isValid() || !streamDepth.isValid())

  138. {

  139. cerr << "彩色或深度数据流不合法" << endl;

  140. OpenNI::shutdown();

  141. return 1;

  142. }

  143.  
  144. // 图像模式注册,彩色图与深度图对齐

  145. if (xtion.isImageRegistrationModeSupported(

  146. IMAGE_REGISTRATION_DEPTH_TO_COLOR))

  147. {

  148. xtion.setImageRegistrationMode(IMAGE_REGISTRATION_DEPTH_TO_COLOR);

  149. }

  150.  
  151.  
  152. // 创建OpenCV图像窗口

  153. namedWindow("Depth Image", CV_WINDOW_AUTOSIZE);

  154. namedWindow("Color Image", CV_WINDOW_AUTOSIZE);

  155.  
  156. // 获得最大深度值

  157. int iMaxDepth = streamDepth.getMaxPixelValue();

  158.  
  159. // 循环读取数据流信息并保存在VideoFrameRef中

  160. VideoFrameRef frameDepth;

  161. VideoFrameRef frameColor;

  162.  
  163. while (true)

  164. {

  165. // 读取数据流

  166. rc = streamDepth.readFrame(&frameDepth);

  167. if (rc == STATUS_OK)

  168. {

  169. // 将深度数据转换成OpenCV格式

  170. const Mat mImageDepth(frameDepth.getHeight(), frameDepth.getWidth(), CV_16UC1, (void*)frameDepth.getData());

  171. // 为了让深度图像显示的更加明显一些,将CV_16UC1 ==> CV_8U格式

  172. Mat mScaledDepth, hScaledDepth;

  173. mImageDepth.convertTo(mScaledDepth, CV_8U, 255.0 / iMaxDepth);

  174.  
  175. //水平镜像深度图

  176. hMirrorTrans(mScaledDepth, hScaledDepth);

  177. // 显示出深度图像

  178. imshow("Depth Image", hScaledDepth);

  179. }

  180.  
  181. rc = streamColor.readFrame(&frameColor);

  182. if (rc == STATUS_OK)

  183. {

  184. // 同样的将彩色图像数据转化成OpenCV格式

  185. const Mat mImageRGB(frameColor.getHeight(), frameColor.getWidth(), CV_8UC3, (void*)frameColor.getData());

  186. // 首先将RGB格式转换为BGR格式

  187. Mat cImageBGR,bImageBGR,hImageBGR;

  188. cvtColor(mImageRGB, cImageBGR, CV_RGB2BGR);

  189.  
  190. //水平镜像深度图

  191. hMirrorTrans(cImageBGR, hImageBGR);

  192. resize(hImageBGR, hImageBGR, Size(640, 480));

  193. // 然后显示彩色图像

  194. imshow("Color Image", hImageBGR);

  195. }

  196.  
  197. // 终止快捷键

  198. if (waitKey(1) == 27)

  199. break;

  200. }

  201.  
  202. // 关闭数据流

  203. streamDepth.destroy();

  204. streamColor.destroy();

  205. // 关闭设备

  206. xtion.close();

  207. // 最后关闭OpenNI

  208. OpenNI::shutdown();

  209.  
  210. return 0;

  211. }

 

今天到此为止,接下来研究如果用深度图和彩色图生成三维点云。欢迎使用xtion的小伙伴或者使用openni2的小伙伴和我讨论相关问题~

 

======================================================================================================

2015/11/3号补充:

关于解决镜像(左右相反)问题,我们还可以直接使用videostream的函数:setMirroringEnabled(false) 例如如下:

 

 
  1. // 同样的设置彩色图像视频模式

  2. VideoMode mModeColor;

  3. mModeColor.setResolution(320, 240);

  4. mModeColor.setFps(30);

  5. mModeColor.setPixelFormat(PIXEL_FORMAT_RGB888);

  6.  
  7. streamColor.setVideoMode(mModeColor);

  8. // 解决镜像问题

  9. streamColor.setMirroringEnabled(false);

  10. // 打开彩色图像数据流

  11. rc = streamColor.start();

这样获得的图像就正常了

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值