简体   繁体   English

使用openNi / NITE从Kinect生成jpeg图像

[英]generating jpeg images from Kinect with openNi/NITE

I am trying to generate pass the image frames to a face-detection/tracking algorithm from the Kinect. 我正在尝试将图像帧从Kinect传递到面部检测/跟踪算法。 I've seen that the best way to generate jpeg images from openGL textures is via libjpeg but I'm clueless on how to start. 我已经看到从openGL纹理生成jpeg图像的最佳方法是通过libjpeg,但是我对如何开始一无所知。 This is what I have on my code now: 这就是我现在的代码:

//draw image frame to texture       
    const XnRGB24Pixel* pImageRow = g_imageMD.RGB24Data();
    XnRGB24Pixel* pTexRow = g_pTexMap + g_imageMD.YOffset() * g_nTexMapX;

    for (XnUInt y = 0; y < g_imageMD.YRes(); ++y)
    {
        const XnRGB24Pixel* pImage = pImageRow;
        XnRGB24Pixel* pTex = pTexRow + g_imageMD.XOffset();

        for (XnUInt x = 0; x < g_imageMD.XRes(); ++x, ++pImage, ++pTex)
        {
            *pTex = *pImage;
        }

        pImageRow += g_imageMD.XRes();
        pTexRow += g_nTexMapX;
    }

    // Create the OpenGL texture map
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, g_nTexMapX, g_nTexMapY, 0, GL_RGB, GL_UNSIGNED_BYTE, g_pTexMap);

So how can I pass on this g_pTexMap thing to be saved as a jpeg image? 那么,如何传递此g_pTexMap内容保存为jpeg图像?

OpenGL does not deal with image files. OpenGL不处理图像文件。 If you want to store an image to a file, passing it to OpenGL is not doing the right thing to do. 如果要将图像存储到文件中,则将图像传递给OpenGL并不是正确的选择。 Instead of passing the data to glTexImage2D you should pass it to libjpeg or another image file access library. 不要将数据传递给glTexImage2D,而应将其传递给libjpeg或其他图像文件访问库。

Have a look at imlib2 http://docs.enlightenment.org/api/imlib2/html/ 看看imlib2 http://docs.enlightenment.org/api/imlib2/html/

I did try the imlib2 but it seemed difficult to convert openNi XnRGB24Pixel type to the data stream imlib2 is looking for. 我确实尝试了imlib2,但似乎很难将openNi XnRGB24Pixel类型转换为imlib2寻找的数据流。 Imlib2 seems to work more easily with files (eg imageName.jpg) rather than data streams in the memory. Imlib2似乎更容易处理文件(例如imageName.jpg),而不是内存中的数据流。 I then moved to try out openCV and here is the code I got and edited from an openni-discussion page [link here]. 然后,我尝试了openCV,这是我从openni讨论页面[此处链接]获得并编辑的代码。

void generateJpeg(const xn::ImageMetaData& g_imageMD){ void generateJpeg(const xn :: ImageMetaData&g_imageMD){

    //opencv to convert image to jpeg
    printf("Converting image to jpeg.\n");
    cv::Mat colorArr[3]; 
    cv::Mat colorImage; 
    const XnRGB24Pixel* pPixel; 
    const XnRGB24Pixel* pImageRow; 
    pImageRow = g_imageMD.RGB24Data();


    colorArr[0] = cv::Mat(g_imageMD.YRes(),g_imageMD.XRes(),CV_8U); 
    colorArr[1] = cv::Mat(g_imageMD.YRes(),g_imageMD.XRes(),CV_8U); 
    colorArr[2] = cv::Mat(g_imageMD.YRes(),g_imageMD.XRes(),CV_8U); 

    for (int y=0; y<g_imageMD.YRes(); y++){ 
        pPixel = pImageRow; 
        uchar* Bptr = colorArr[0].ptr<uchar>(y); 
        uchar* Gptr = colorArr[1].ptr<uchar>(y); 
        uchar* Rptr = colorArr[2].ptr<uchar>(y); 

        for(int x=0;x<g_imageMD.XRes();++x , ++pPixel){ 
                Bptr[x] = pPixel->nBlue; 
                Gptr[x] = pPixel->nGreen; 
                Rptr[x] = pPixel->nRed; 
        } 

        pImageRow += g_imageMD.XRes(); 
    } 

    cv::merge(colorArr,3,colorImage); 
     IplImage bgrIpl = colorImage;    
    cvSaveImage("image.jpg",&bgrIpl);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM