2011-06-20 40 views
0

我試圖從Kinect生成將圖像幀傳遞給人臉檢測/跟蹤算法。我已經看到,從openGL紋理生成jpeg圖像的最好方法是通過libjpeg,但我對如何開始無能爲力。這就是我對我現在的代碼:使用openNi/NITE從Kinect生成jpeg圖像

//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); 

所以,我怎麼可以把這塊g_pTexMap東西保存爲JPEG圖像?

+0

這是什麼語言? C ? C++? C# ?阿達? Fortran語言?爲您的問題添加適當的標籤。 – karlphillip

+0

啊對不起。我已經添加了C++標籤。 –

回答

3

OpenGL不處理圖像文件。如果你想將圖像存儲到一個文件中,將它傳遞給OpenGL並不是正確的做法。不應將數據傳遞給glTexImage2D,而應將其傳遞給libjpeg或其他圖像文件訪問庫。

看一看imlib2 http://docs.enlightenment.org/api/imlib2/html/

+0

該鏈接看起來非常有幫助,我會試試看,謝謝! –

+0

@Esther Goh:請注意,您可以在不支持X的情況下編譯imlib2,因此您甚至不需要安裝X庫(與鏈接頁面中所寫的內容相反)。 – datenwolf

0

我曾嘗試imlib2但它似乎很難openNi XnRGB24Pixel類型轉換爲數據流imlib2所期待的。 Imlib2似乎更容易處理文件(例如imageName.jpg),而不是內存中的數據流。然後我開始嘗試openCV,這裏是我從openni討論頁面獲得並編輯的代碼[鏈接]。

無效generateJpeg(常量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); 

}