当前位置: 代码迷 >> 综合 >> Kinect (三)读取深度图数据
  详细解决方案

Kinect (三)读取深度图数据

热度:50   发布时间:2023-11-23 19:18:02.0

下面还是参考的小斤的博客(似乎是一模一样的,汗)

#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"using namespace std;
using namespace cv;
using namespace openni;#define Capture
//#define Show
//检查OPenNI是否初始化成功
void CheckOpenNIError(Status result, string status)
{if (result != STATUS_OK)cerr << status << " Error: " << OpenNI::getExtendedError() << endl;
}int main(int argc, char** argv)
{
#ifdef CaptureStatus result = STATUS_OK;//OpenNI2 imageVideoFrameRef oniDepthImg;//OpenCV imagecv::Mat cvDepthImg;char key = 0;//【1】// initialize OpenNI2result = OpenNI::initialize();CheckOpenNIError(result, "initialize context");// open device  Device device;result = device.open(openni::ANY_DEVICE);//【2】制造深度数据流// create depth stream VideoStream oniDepthStream;result = oniDepthStream.create(device, openni::SENSOR_DEPTH);//【3】设置深度数据的格式// set depth video modeVideoMode modeDepth;modeDepth.setResolution(640, 480);//大小 640X480modeDepth.setFps(30);//设置帧率三十帧modeDepth.setPixelFormat(PIXEL_FORMAT_DEPTH_1_MM);oniDepthStream.setVideoMode(modeDepth);// start depth streamresult = oniDepthStream.start();//【4】// set depth and color imge registration modeif (device.isImageRegistrationModeSupported(IMAGE_REGISTRATION_DEPTH_TO_COLOR)){device.setImageRegistrationMode(IMAGE_REGISTRATION_DEPTH_TO_COLOR);}// start color streamint i = 240, j = 320;//确定16位的openni数据图像与8位opencv图像的转换double  Unit = 255.0 / (oniDepthStream.getMaxPixelValue());while (key != 27){// read frameif (oniDepthStream.readFrame(&oniDepthImg) == STATUS_OK){cv::Mat cvRawImg16U(oniDepthImg.getHeight(), oniDepthImg.getWidth(), CV_16UC1, (void*)oniDepthImg.getData());cvRawImg16U.convertTo(cvDepthImg, CV_8U,Unit);//【5】// convert depth image GRAY to BGRcv::imshow("depth", cvDepthImg);imwrite("1.jpg", cvDepthImg);//得到像素值得范围-----[0,10000]cout << "the max pixel value is " << oniDepthStream.getMaxPixelValue()<< endl;cout << "the min pixel value is " << oniDepthStream.getMinPixelValue ()<< endl;cout << "coordinate i,j is "<<((int) (cvDepthImg.at<uchar>(j,i)))/Unit << endl;}key = cv::waitKey(20);}//cv destroy//OpenNI2 destroyoniDepthStream.destroy();device.close();OpenNI::shutdown();
#elif defined  Showcv::Mat picture=cv::imread("C:\\Users\\Administrator\\Desktop\\Photo\\1.jpg");cv::Mat picture_gray = Mat(picture.rows ,picture.cols,CV_8UC1 );cv::Mat color = cv::imread("E:\\Kinect学习\\1.png");cv::cvtColor(picture, picture_gray, CV_BGR2GRAY);int k = 0;if (!color .data)                              // Check for invalid input{cout << "Could not open or find the image" << std::endl;return -1;}else//imshow("picture", picture);{cv::imshow("picture_gray", picture_gray);cv::imshow("good", color);}cvWaitKey(0);
#endifreturn 0;
}

可以看到,图片读取过程如下:

1、初始化

2、设置数据流格式

3、开始传输

4、读取

5、转换为Opencv图片显示

具体的环境就需要我们自己配置啦

  相关解决方案