2014-01-06 27 views
1

我有一種計算計算機視覺應用中常用的積分圖像(description here)的方法。iOS - C/C++ - 加速積分圖像計算

float *Integral(unsigned char *grayscaleSource, int height, int width, int widthStep) 
{ 
    // convert the image to single channel 32f 
    unsigned char *img = grayscaleSource; 

    // set up variables for data access 
    int step = widthStep/sizeof(float); 
    uint8_t *data = (uint8_t *)img; 
    float *i_data = (float *)malloc(height * width * sizeof(float)); 

    // first row only 
    float rs = 0.0f; 
    for(int j=0; j<width; j++) 
    { 
     rs += (float)data[j]; 
     i_data[j] = rs; 
    } 

    // remaining cells are sum above and to the left 
    for(int i=1; i<height; ++i) 
    { 
     rs = 0.0f; 
     for(int j=0; j<width; ++j) 
     { 
      rs += data[i*step+j]; 
      i_data[i*step+j] = rs + i_data[(i-1)*step+j]; 
     } 
    } 

    // return the integral image 
    return i_data; 
} 

我想讓它儘可能快。在我看來,這應該能夠利用蘋果的Accelerate.framework,或者可能是霓虹內在的東西,但是我看不出如何。看起來嵌套循環可能很慢(至少對於實時應用程序而言)。

有沒有人認爲這是可以加快使用任何其他技術?

+0

這不可能是C++ *和* objective c。選一個。 – Proxy

+1

@Proxy有一個叫做Objective-C++ –

+1

@Proxy的東西。抱歉。是的,iOS上的C/C++(或Bryan提到的Objective-C++)。 – Brett

回答

3

你當然可以矢量化逐行求和。這是vDSP_vadd()。水平方向是vDSP_vrsum()。

如果你想編寫你自己的矢量代碼,水平和可能會加快像psadbw,但這是英特爾。另外,請看看prefix sum algorithms,這是着名的可並行化。

+0

關於如何實現這種方法的任何指針? – Brett