積分圖是圖像中十分經常使用的方法,最初是在Haar特徵的快速計算中學到(參考博文:利用積分圖像法快速計算Haar特徵),後來發如今均值濾波,二值化等圖像處理方法中也十分常見。spa
積分圖的簡要介紹能夠參考博文:利用積分圖像法快速計算Haar特徵,這裏再也不重複了。本篇主要是小記一下積分圖的計算方法。.net
因爲積分圖中每一個單元存儲的信息是原圖中此位置左上角全部像素之和,因此對一張W*H的圖像直接求取積分圖,須要:code
(1+2+...+W-1)*H+(1+2+...+W)*(H=1) = (w-2)*H/(W-1)+2(W+1)*(H-1)/W blog
次加法。get
void integral(unsigned char* inputMatrix, unsigned long* outputMatrix, int width, int height){ // calculate integral of the first line for(int i=0;i<width;i++){ outputMatrix[i] = inputMatrix[i]; if(i>0){ outputMatrix[i] += outputMatrix[i-1]; } } for (int i=1;i<height;i++){ int offset = i*width; // first column of each line outputMatrix[offset] = outputMatrix[offset-width]+inputMatrix[offset]; // other columns outputMatrix[offset+j] = outputMatrix[offset+j-1] + outputMatrix[offset-width] + outputMatrix[offset-width-1] + inputMatrix[offset]; } } return ; }
void fastIntegral(unsigned char* inputMatrix, unsigned long* outputMatrix, int width, int height){ unsigned long *columnSum = new unsigned long[width]; // sum of each column // calculate integral of the first line for(int i=0;i<width;i++){ columnSum[i]=inputMatrix[i]; outputMatrix[i] = inputMatrix[i]; if(i>0){ outputMatrix[i] += outputMatrix[i-1]; } } for (int i=1;i<height;i++){ int offset = i*width; // first column of each line columnSum[0] +=inputMatrix[offset]; outputMatrix[offset] = columnSum[0]; // other columns for(int j=1;j<width;j++){ columnSum[j] += inputMatrix[offset+j]; outputMatrix[offset+j] = outputMatrix[offset+j-1] + columnSum[j]; } } return ; }