2017-04-14 123 views
2

我正在使用KinectPV2庫,它很棒但很差。將深度映射到RGB空間

參考下面複製的示例「MapDepthToColor」,我試圖檢索每個RGB像素的深度。

設法調整原始示例以將深度映射到RGB空間,剩下一個奇怪的重複邊緣(請參見左下圖)。

有人能指出發生了什麼事嗎?

/* 
Thomas Sanchez Lengeling. 
<a href="http://codigogenerativo.com/" target="_blank" rel="nofollow">http://codigogenerativo.com/</a> 

KinectPV2, Kinect for Windows v2 library for processing 

Color to fepth Example, 
Color Frame is aligned to the depth frame 
*/ 

import KinectPV2.*; 

KinectPV2 kinect; 

int [] depthZero; 

//BUFFER ARRAY TO CLEAN DE PIXLES 
PImage depthToColorImg; 

void setup() { 
    size(1024, 848, P3D); 

    depthToColorImg = createImage(512, 424, PImage.RGB); 
    depthZero = new int[ KinectPV2.WIDTHDepth * KinectPV2.HEIGHTDepth]; 

    //SET THE ARRAY TO 0s 
    for (int i = 0; i < KinectPV2.WIDTHDepth; i++) { 
    for (int j = 0; j < KinectPV2.HEIGHTDepth; j++) { 
     depthZero[424*i + j] = 0; 
    } 
    } 

    kinect = new KinectPV2(this); 
    kinect.enableDepthImg(true); 
    kinect.enableColorImg(true); 
    kinect.enablePointCloud(true); 

    kinect.init(); 
} 

void draw() { 
    background(0); 

    float [] mapDCT = kinect.getMapDepthToColor(); // Size: 434,176 (512*424) 

    //get the raw data from depth and color 
    int [] colorRaw = kinect.getRawColor(); // Size: 2,073,600 (1920*1080) 

    int [] depthRaw = kinect.getRawDepthData(); // 434176 

    //clean de pixels 
    PApplet.arrayCopy(depthZero, depthToColorImg.pixels); 

    int count = 0; 
    depthToColorImg.loadPixels(); 
    for (int i = 0; i < KinectPV2.WIDTHDepth; i++) { 
    for (int j = 0; j < KinectPV2.HEIGHTDepth; j++) { 

     //incoming pixels 512 x 424 with position in 1920 x 1080 
     float valX = mapDCT[count * 2 + 0]; 
     float valY = mapDCT[count * 2 + 1]; 


     //maps the pixels to 512 x 424, not necessary but looks better 
     int valXDepth = (int)((valX/1920.0) * 512.0); 
     int valYDepth = (int)((valY/1080.0) * 424.0); 

     int valXColor = (int)(valX); 
     int valYColor = (int)(valY); 

     if (valXDepth >= 0 && valXDepth < 512 && valYDepth >= 0 && valYDepth < 424 && 
     valXColor >= 0 && valXColor < 1920 && valYColor >= 0 && valYColor < 1080) { 
     float col = map(depthRaw[valYDepth * 512 + valXDepth], 0, 4500, 0, 255); 
     //color colorPixel = colorRaw[valYColor * 1920 + valXColor]; // This works as intended 
     color colorPixel = color(col); // This doesn't 
     depthToColorImg.pixels[valYDepth * 512 + valXDepth] = colorPixel; 
     } 
     count++; 
    } 
    } 
    depthToColorImg.updatePixels(); 

    image(depthToColorImg, 0, 424); 
    image(kinect.getColorImage(), 0, 0, 512, 424); 
    image(kinect.getDepthImage(), 512, 0); 

    text("fps: "+frameRate, 50, 50); 
} 

Current faulty result

+0

我不熟悉witht他庫,所以只能提供一些猜測:這有可能你在這樣一個迭代你錯過了那些像素(由於舍入錯誤)。另外,我認爲關於「深度」一詞有些混淆。在某些地方,你似乎使用它來表示寬度:'valXDepth <512',在其他情況下,它意味着距離kinext的距離int [] depthRaw = kinect.getRawDepthData();'。嘗試改變你的內部'if(valXDepth> = 0 && ...'block忽略深度數據並輸出一個純色,如果問題出在數據上,你會得到一個純色圖像。 – Basic

+0

...另一方面,如果您的迭代方式存在問題,則輸出應該具有相同的波段。 – Basic

+0

請您澄清「忽略深度數據」的含義嗎?上面的大多數代碼是從一個例子中,我仍然不確定'kinect.getMapDepthToColor()'返回的是什麼,我稍微調整了上面的代碼,希望進一步說明問題,當使用'colorRaw [ ]'那麼它的工作原理與深度效果不同,''''''會導致dup效果。 –

回答