下面还是参考的小斤的博客(似乎是一模一样的,汗)
#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 Capture
Status result = STATUS_OK;
//OpenNI2 image
VideoFrameRef oniDepthImg;
//OpenCV image
cv::Mat cvDepthImg;
char key = 0;
//【1】
// initialize OpenNI2
result = 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 mode
VideoMode modeDepth;
modeDepth.setResolution(640, 480);//大小 640X480
modeDepth.setFps(30);//设置帧率三十帧
modeDepth.setPixelFormat(PIXEL_FORMAT_DEPTH_1_MM);
oniDepthStream.setVideoMode(modeDepth);
// start depth stream
result = oniDepthStream.start();
//【4】
// set depth and color imge registration mode
if (device.isImageRegistrationModeSupported(IMAGE_REGISTRATION_DEPTH_TO_COLOR))
{
device.setImageRegistrationMode(IMAGE_REGISTRATION_DEPTH_TO_COLOR);
}
// start color stream
int i = 240, j = 320;
//确定16位的openni数据图像与8位opencv图像的转换
double Unit = 255.0 / (oniDepthStream.getMaxPixelValue());
while (key != 27)
{
// read frame
if (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 BGR
cv::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 destroy
oniDepthStream.destroy();
device.close();
OpenNI::shutdown();
#elif defined Show
cv::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);
#endif
return 0;
}
可以看到,图片读取过程如下:
1、初始化
2、设置数据流格式
3、开始传输
4、读取
5、转换为Opencv图片显示
具体的环境就需要我们自己配置啦