Draw the RGB data from kinect C++ via opengl

In order to improve my English writing skills,I am going to  write the blogs in English form now!

-------------------------------------------------------------------------------------------Luxuriant line-----------------------------------------------------------------------

Today,we will learn how to initialize the kinect and get RGB data form it,then convert the data to a texture,which will be drawn to the windows.

We have two real pieces of kinect-specific code. I will go over these in some detail, and give a fairly hight level overview of the display code

include the header files:

#include <Windows.h>
#include <Ole2.h>

#include <gl/GL.h>
#include <gl/GLU.h>
#include <gl/glut.h>

#include <NuiApi.h>
#include <NuiImageCamera.h>
#include <NuiSensor.h>

Constants and global variables:

#define width 640
#define height 480

// OpenGL Variables
GLuint textureId;              // ID of the texture to contain Kinect RGB Data
GLubyte data[width*height*4];  // BGRA array containing the texture data

// Kinect variables
HANDLE rgbStream;              // The identifier of the Kinect's RGB Camera
INuiSensor* sensor;            // The kinect sensor

Kinect Initialization:

bool initKinect() {
    // Get a working kinect sensor
    int numSensors;
    if (NuiGetSensorCount(&numSensors) < 0 || numSensors < 1) return false;
    if (NuiCreateSensorByIndex(0, &sensor) < 0) return false;

    // Initialize sensor
    sensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_DEPTH | NUI_INITIALIZE_FLAG_USES_COLOR);
    sensor->NuiImageStreamOpen(
        NUI_IMAGE_TYPE_COLOR,            // Depth camera or rgb camera?
        NUI_IMAGE_RESOLUTION_640x480,    // Image resolution
        0,      // Image stream flags, e.g. near mode
        2,      // Number of frames to buffer
        NULL,   // Event handle
        &rgbStream);
    return sensor;
}

get an RGB frame from the Kinect:

void getKinectData(GLubyte* dest) {
    NUI_IMAGE_FRAME imageFrame;
    NUI_LOCKED_RECT LockedRect;
    if (sensor->NuiImageStreamGetNextFrame(rgbStream, 0, &imageFrame) < 0) return;
    INuiFrameTexture* texture = imageFrame.pFrameTexture;
    texture->LockRect(0, &LockedRect, NULL, 0);
 if (LockedRect.Pitch != 0)
    {
        const BYTE* curr = (const BYTE*) LockedRect.pBits;
        const BYTE* dataEnd = curr + (width*height)*4;

        while (curr < dataEnd) {
            *dest++ = *curr++;
        }
    }
  texture->UnlockRect(0);
    sensor->NuiImageStreamReleaseFrame(rgbStream, &imageFrame);
}

Something about the window:

void draw() {
   drawKinectData();
   glutSwapBuffers();
}

void execute() {
    glutMainLoop();
}

bool init(int argc, char* argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(width,height);
    glutCreateWindow("Kinect SDK Tutorial");
    glutDisplayFunc(draw);
    glutIdleFunc(draw);
    return true;
}

Display via OpenGL:

  // Initialize textures
    glGenTextures(1, &textureId);
    glBindTexture(GL_TEXTURE_2D, textureId);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height,
                 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*) data);
    glBindTexture(GL_TEXTURE_2D, 0);

    // OpenGL setup
    glClearColor(0,0,0,0);
    glClearDepth(1.0f);
    glEnable(GL_TEXTURE_2D);

    // Camera setup
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, height, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
int main(int argc, char* argv[]) {
    if (!init(argc, argv)) return 1;
    if (!initKinect()) return 1;

    /* ...OpenGL texture and camera initialization... */

    // Main loop
    execute();
    return 0;
}

Draw a frame to the screen:

void drawKinectData() {
    glBindTexture(GL_TEXTURE_2D, textureId);
    getKinectData(data);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)data);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(0, 0, 0);
        glTexCoord2f(1.0f, 0.0f);
        glVertex3f(width, 0, 0);
        glTexCoord2f(1.0f, 1.0f);
        glVertex3f(width, height, 0.0f);
        glTexCoord2f(0.0f, 1.0f);
        glVertex3f(0, height, 0.0f);
    glEnd();
}
    

The End! Build and run,making sure that your Kinect is plugged in.You should see a window containing a viseo stream of what your Kinect sees.

时间: 2024-10-07 16:30:34

Draw the RGB data from kinect C++ via opengl的相关文章

Sensor信号输出YUV、RGB、RAW DATA、JPEG【转】

本文转载自:http://blog.csdn.net/southcamel/article/details/8305873 简单来说,YUV: luma (Y) + chroma (UV) 格式, 一般情况下sensor支持YUV422格式,即数据格式是按Y-U-Y-V次序输出的RGB: 传统的红绿蓝格式,比如RGB565,其16-bit数据格式为5-bit R + 6-bit G + 5-bit B.G多一位,原因是人眼对绿色比较敏感.RAW RGB: sensor的每一像素对应一个彩色滤光片

嵌入式开发之davinci--- 8148/8168/8127 中的图像采集格式Sensor信号输出YUV、RGB、RAW DATA、JPEG 4种方式区别

简单来说,YUV: luma (Y) + chroma (UV) 格式, 一般情况下sensor支持YUV422格式,即数据格式是按Y-U-Y-V次序输出的RGB: 传统的红绿蓝格式,比如RGB565,其16-bit数据格式为5-bit R + 6-bit G + 5-bit B.G多一位,原因是人眼对绿色比较敏感.RAW RGB: sensor的每一像素对应一个彩色滤光片,滤光片按Bayer pattern分布.将每一个像素的数据直接输出,即RAW RGB dataJPEG: 有些sensor

最简单的视音频播放示例2:GDI播放YUV, RGB

前一篇文章对"Simplest Media Play"工程作了概括性介绍.后续几篇文章打算详细介绍每个子工程中的几种技术.在记录Direct3D,OpenGL这两种相对复杂的技术之前,打算先记录一种和它们属于同一层面的的简单的技术--GDI作为热身. GDI简介 下面这段文字摘自维基百科: 图形设备接口(Graphics Device Interface或Graphical Device Interface,缩写GDI),是微软公司视窗操作系统(Microsoft Windows)的三

Kinect 开发 &mdash;&mdash; Kinect studio

This tool can record all the data coming into an application from a Kinect unit. You can then view, review and store the data. Kinect Studio lets you inject the captured data streams back into a Kinect-enabled application, allowing you to test your c

最简单的视音频播放示例4:OpenGL播放RGB/YUV

本文记录OpenGL播放视频的技术.OpenGL是一个和Direct3D同一层面的技术.相比于Direct3D,OpenGL具有跨平台的优势.尽管在游戏领域,DirectX的影响力已渐渐超越OpenGL并被大多数PC游戏开发商所采用,但在专业高端绘图领域,OpenGL因为色彩准确,仍然是不能被取代的主角. OpenGL简介 从网上搜集了一些有关OpenGL简介方面的知识,在这里列出来.开放图形库(英语:Open Graphics Library,缩写为OpenGL)是个定义了一个跨编程语言.跨平

YUY2(YUV) 与 RGB 格式图片的相互转换 以及 基于YUY2(YUV)的blending

这是一个项目里使用的,API里从pool里取出的格式都是YUY2,但是图像处理的API库中要求都是jepg格式. YUY2经常用于电视制式以及许多摄像头的输出格式.而我们在处理时经常需要将其转化为RGB进行处理,这里简单介绍下YUY2(YUV)与RGB之间相互转化的关系: http://msdn2.microsoft.com/en-us/library/ms893078.aspx YUY2(YUV) To RGB: C = Y - 16 D = U - 128 E = V - 128 R = c

图像RGB格式与YUV格式互转

1 // rgb2yuv.cpp : 定义控制台应用程序的入口点. 2 // 3 4 #include "stdafx.h" 5 6 7 #define Level 256 //直方图bin数 8 #define SIZEX 176 //image列尺寸 9 #define SIZEY 256 //image列尺寸 10 #define READ 0 11 #define WRITE 1 12 13 unsigned char image_in[SIZEY*SIZEX*3]; 14 u

THC63LVD827:RGB转双路LVDS芯片

General Description  The THC63LVD827 transmitter is designed to supportpixel data transmission between Host and Flat PanelDisplay and Dual Link transmission between Host andFlat Panel Display up to 1080p/1920x1440 resolutions.   The THC63LVD827 conve

项目中利用AnyChat SDK实现将RGB数据作为视频源的实时推送功能

一.前期调研工作: 查看官网简介www.anychat.cn 可以知道AnyChat支持外部音视频功能,具体描述如下: AnyChat Platform Core SDK V4.2版本增加了外部音视频数据输入功能,该功能主要满足一些特殊应用场合下的需求,通常普通用户不会使用到,使用该功能,可以由上层应用程序输入视频数据.音频数据到AnyChat,然后AnyChat再对这些数据进行编码.传输,即使用上层应用的数据做为数据源,而不使用AnyChat从本地声卡.摄像头采集的音视频数据. 通过外部音视频