2015-04-25 100 views
1

我是新來的Kinect項目 而且我實現一個深度閾值,當距離大於大於400mmKinect的深度分割幀速率

for (UINT y = 0; y < pImg->rows; ++y) 
{ 
    // Get row pointers for Mats 
    const USHORT* pDepthRow = depth->ptr<USHORT>(y); 

    for (UINT x = 0; x < pImg->cols; ++x) 
    { 
     USHORT raw_depth = pDepthRow[x]; 
     SHORT realDepth = NuiDepthPixelToDepth(raw_depth); 
     // If depth value is valid, convert and copy it 
     if (raw_depth != 65535) 
     { 
      if(realDepth >400) //greater than 400mm 
      { 
       pImg->at<Vec4b>(y,x)[0] = 255; 
       pImg->at<Vec4b>(y,x)[1] = 255; 
       pImg->at<Vec4b>(y,x)[2] = 255; 
       pImg->at<Vec4b>(y,x)[3] = 255; 
      } 
      else 
      { 
       pImg->at<Vec4b>(y,x)[0] = 0; 
       pImg->at<Vec4b>(y,x)[1] = 0; 
       pImg->at<Vec4b>(y,x)[2] = 0; 
       pImg->at<Vec4b>(y,x)[3] = 0; 
      } 
     } 

    } 

似乎得到正確的結果,但大量降低幀速率。 當我想通過使用cv :: inRange來擺脫循環時,但是這個函數僅在原始深度爲16U時支持8U1C。 那麼還有什麼可以根據真實距離來分割深度?

回答

0

嘗試通過存儲對像素的引用來提高性能。 更改此:

if (realDepth > 400) //greater than 400mm 
{ 
    pImg->at<Vec4b>(y,x)[0] = 255; 
    pImg->at<Vec4b>(y,x)[1] = 255; 
    pImg->at<Vec4b>(y,x)[2] = 255; 
    pImg->at<Vec4b>(y,x)[3] = 255; 
} 
else 
{ 
    pImg->at<Vec4b>(y,x)[0] = 0; 
    pImg->at<Vec4b>(y,x)[1] = 0; 
    pImg->at<Vec4b>(y,x)[2] = 0; 
    pImg->at<Vec4b>(y,x)[3] = 0; 
} 

要這樣:。
(我不知道T是什麼,因爲我不知道什麼是pImg T應該等於at方法的返回值,我認爲它是Vec4b。)

T& pixel = pImg->at<Vec4b>(y, x); // probably Vec4b& pixel = .. 
if (realDepth > 400) //greater than 400mm 
{ 
    pixel[0] = 255; 
    pixel[1] = 255; 
    pixel[2] = 255; 
    pixel[3] = 255; 
} 
else 
{ 
    pixel[0] = 0; 
    pixel[1] = 0; 
    pixel[2] = 0; 
    pixel[3] = 0; 
} 
+0

謝謝,它確實提高了fps從8到15! –

+0

@ChengMa很高興我能幫到你。如果解決了您的問題,請接受答案。 –