Kinect开发(3)-RGB数据获取

    RGB数据的获取与景深数据获取类似,不同的就是数据帧的数据格式是openni::RGB888Pixel类型的。直接通过代码来了解:

 1 #include <conio.h>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 #include <OpenNI.h>
 6 #pragma comment(lib, "openni2.lib")
 7 
 8 #include <opencv/cv.h>
 9 #include <opencv/highgui.h>
10 #pragma comment(lib, "opencv_core244.lib")
11 #pragma comment(lib, "opencv_highgui244.lib")
12 #pragma comment(lib, "opencv_imgproc244.lib")
13 
14 class NewColorFrameListener
15     : public openni::VideoStream::NewFrameListener
16 {
17 public:
18     virtual void onNewFrame(openni::VideoStream& stream)
19     {
20         openni::VideoFrameRef frame;
21         stream.readFrame(&frame);
22         if (frame.isValid())
23         {
24             if (frame.getSensorType() == openni::SENSOR_COLOR)
25             {
26                 cv::Mat rgbImg(frame.getHeight(), frame.getWidth(), CV_8UC3, (void*)frame.getData());
27                 cv::Mat bgrImg;
28                 cvtColor(rgbImg, bgrImg, CV_RGB2BGR);
29                 IplImage* img = cvCreateImage(cvSize(frame.getWidth(), frame.getHeight()), 8, 3);
30                 img->imageData = (char*)bgrImg.data;
31                 cvShowImage("wnd", img);
32                 cvReleaseImage(&img);
33             }
34         }
35     }
36 };
37 
38 int main()
39 {
40     openni::Status rc = openni::STATUS_OK;
41     NewColorFrameListener listener;
42     rc = openni::OpenNI::initialize();
43     if (rc != openni::STATUS_OK)
44     {
45         cout<<"initialize failed:"<<openni::OpenNI::getExtendedError()<<endl;
46         return 1;
47     }
48     openni::Device device;
49     rc = device.open(openni::ANY_DEVICE);
50     if (rc != openni::STATUS_OK)
51     {
52         openni::OpenNI::shutdown();
53         cout<<"open device failed:"<<openni::OpenNI::getExtendedError()<<endl;
54         return 0;
55     }
56     openni::VideoStream colorStream;
57     rc = colorStream.create(device, openni::SENSOR_COLOR);
58     if (rc != openni::STATUS_OK)
59     {
60         openni::OpenNI::shutdown();
61         cout<<"create color stream failed:"<<openni::OpenNI::getExtendedError()<<endl;
62         return 0;
63     }
64     cvNamedWindow("wnd", CV_WINDOW_AUTOSIZE);
65     openni::VideoFrameRef frame;
66     colorStream.start();
67     colorStream.addNewFrameListener(&listener);
68     while (true)
69     {
70         if (cvWaitKey(30) == 27)
71             break;
72     }
73 
74     cvDestroyWindow("wnd");
75     colorStream.stop();
76     colorStream.destroy();
77     device.close();
78     openni::OpenNI::shutdown();
79     return 0;
80 }

    其中,38-80行的main函数就不用多说了,基本与景深数据的获取类似。

    在NewColorFrameListener类中的onNewFrame方法里处理并显示RGB数据。我们直接把相应的数据通过OpenCV来展示,注意,由于Kinect获取的格式是RGB的,而OpenCV处理数据的内部结构是BGR方式排列的,所以,在显示之前,需要进行一次颜色空间的转换。直接通过cvtColor方法转换即可。

posted @ 2013-05-30 13:48  tszhao  阅读(737)  评论(0编辑  收藏  举报