Boxfilter 快速計算
它可以使復雜度為O(MN)的求和,求方差等運算降低到O(1)或近似於O(1)的復雜度,它的缺點是不支持多尺度。
Boxfilter 的原理有點類似 Integral Image,而且比它還要快,但是實現步驟比較復雜。在計算矩形特征之前,Boxfilter 與 Integral Image 都需要對圖像進行初始化(即對數組A賦值), 不同於 Integral Image, Boxfilter 的數組 A 中的每個元素的值是該像素鄰域內的像素和(或像素平方和), 在需要求某個矩形內像素和的時候,直接訪問數組中對應的位置就可以了。因此可以看出它的復雜度是O(1)。
Boxfilter 的初始化過程如下:
1、給定一張圖像,寬高為(M,N),確定待求矩形模板的寬高(m,n),如圖紫色矩形。圖中每個黑色方塊代表一個像素,紅色方塊是假想像素。
2、開辟一段大小為 M 的數組,記為 buff, 用來存儲計算過程的中間變量,用紅色方塊表示.
3、將矩形模板(紫色)從左上角(0,0)開始,逐像素向右滑動,到達行末時,矩形移動到下一行的開頭(0,1),如此反復,每移動到一個新位置時,計算矩形內的像素和,保存在數組 A ( buff 只是用來緩存中間變量的)中。以 (0,0) 位置為例進行說明:首先將綠色矩形內的每一列像素求和,結果放在 buff 內(紅色方塊),再對藍色矩形內的像素求和,結果即為紫色特征矩形內的像素和,把它存放到數組A中,如此便完成了第一次求和運算。
4、每次紫色矩形向右移動時,實際上就是求對應的藍色矩形的像素和,此時只要把上一次的求和結果減去藍色矩形內的第一個紅色塊,再加上它右面的一個紅色塊,就是當前位置的和了,用公式表示 sum[i] = sum[i-1] - buff[j] + buff[j+m].
5、當紫色矩形移動到行末時,需要對 buff 進行更新。因為整個綠色矩形下移了一個像素,所以對於每個buff[i], 需要加上一個新進來的像素,再減去一個出去的像素,然后便開始新的一行的計算了。
Boxfilter 的初始化過程非常快速,每個矩形的計算基本上只需要一加一減兩次運算。從初始化的計算速度上來說,Boxfilter 比 Integral Image 要快一些,大約 25%。在具體求某個矩形特征時,Boxfilter 比 Integral Image 快 4 倍,所謂的 4 倍其實就是從 4 次加減運算降低到 1 次,雖然這個優化非常渺小,但是把它放到幾層大循環里面,還是能節省一些時間的。對於那些實時跟蹤檢測算法,一幀的處理時間要嚴格在 40ms 以下,正是這些細小的優化決定了程序的效率,積少成多,聚沙成塔。
下面的程序是Boxfilter的示例代碼,謹供參考
.hpp
#pragma once
typedef unsigned char uchar;
class Boxfilter
{
public:
Boxfilter(void);
~Boxfilter(void);
void init(int width, int height, int mwidth=5, int mheight=5);
void boxfilter(unsigned char* img);
public:
float getMean(int x, int y); //以x,y為中心點,mwidth,mheight為直徑的局部區域,下同
float getVar(int x, int y);
int getSum(int x, int y);
int getSquareSum(int x, int y);
int getLocalSize();
private:
int mwidth ;
int mheight ;
unsigned char* img;
int width;
int height;
int* f_sum;
int* f_sum2;
};
.cpp
#include "Boxfilter.h"
#include <assert.h>
#include <string>
int* buff = 0;
int* buff2 = 0;
int boxwidth;
int boxheight;
Boxfilter::Boxfilter(void)
{
f_sum = 0;
f_sum2 = 0;
}
Boxfilter::~Boxfilter(void)
{
if(f_sum) delete[] f_sum;
if(f_sum2) delete[] f_sum2;
if(buff) delete[] buff;
if(buff2) delete[] buff2;
}
void Boxfilter::init(int width, int height, int mwidth, int mheight)
{
this->mwidth = mwidth;
this->mheight = mheight;
this->width = width;
this->height = height;
boxwidth = width - mwidth;
boxheight = height - mheight;
f_sum = new int[boxwidth *boxheight];
f_sum2 = new int[boxwidth *boxheight];
buff = new int[width];
buff2= new int[width];
}
void Boxfilter::boxfilter (unsigned char* img)
{
int j,x,y;
memset(buff, 0, width *sizeof(int));
memset(buff2, 0, width *sizeof(int));
memset(f_sum, 0, boxwidth *boxheight);
memset(f_sum2, 0, boxwidth *boxheight);
for(y=0; y<mheight; y++)
{
for(x=0; x<width; x++)
{
uchar pixel = img[y *width + x];
buff[x] += pixel;
buff2[x] += pixel*pixel;
}
}
for(y=0; y<height - mheight;y++)
{
int Xsum=0;
int Xsum2=0;
for(j=0; j<mwidth; j++)
{
Xsum += buff[j];
Xsum2 += buff2[j];
}
for(x=0; x<width - mwidth; x++)
{
if(x!=0)
{
Xsum = Xsum-buff[x-1]+buff[mwidth-1+x];
Xsum2 = Xsum2-buff2[x-1]+buff2[mwidth-1+x];
}
f_sum[y*(width - mwidth)+x] = (float) Xsum ;
f_sum2[y*(width - mwidth)+x] = Xsum2;
}
for(x=0; x<width; x++)
{
uchar pixel = img[y *width + x];
uchar pixel2 = img[(y+mheight) *width + x];
buff[x] = buff[x] - pixel + pixel2;
buff2[x] = buff2[x] - pixel*pixel + pixel2*pixel2;
}
}
}
float Boxfilter::getMean(int x, int y)
{
return getSum(x,y) / (float)(mwidth*mheight);
}
float Boxfilter::getVar(int x, int y)
{
float mean = getMean(x, y);
return (float)getSquareSum(x, y)/(mwidth *mheight) - mean*mean;
}
int Boxfilter::getSquareSum(int x, int y)
{
if(y>mheight/2 && y<height - mheight/2 && x>mwidth/2 && x<width - mwidth/2)
return f_sum2[(y - mheight/2) *boxwidth + (x - mwidth/2)];
else
return -1;
}
int Boxfilter::getSum(int x, int y)
{
if(y>mheight/2 && y<height - mheight/2 && x>mwidth/2 && x<width - mwidth/2)
return f_sum[(y - mheight/2) *boxwidth + (x - mwidth/2)];
else
return -1;
}
int Boxfilter::getLocalSize()
{
return mwidth > mheight ? mwidth : mheight;
}
測試程序
// cv2.4 test.cpp : 定義控制台應用程序的入口點。
//
#include "stdafx.h"
#include "opencv2/opencv.hpp"
#include "Boxfilter.h"
#include <iostream>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Mat src = imread("C:\\Documents and Settings\\Administrator\\桌面\\img1.png",0);
Boxfilter box;
box.init(src.cols, src.rows, 5, 5);
box.boxfilter((uchar*)src.data);
int x = 50, y = 50;
float a = box.getMean(x, y); //求出以(x,y)為中心的矩形的均值
float b = box.getVar(x, y);
int c = box.getSum(x, y);
int d = box.getSquareSum(x, y);
int e = box.getLocalSize();
cout<<"mean: " <<a<<endl;
cout<<"var: " <<b<<endl;
cout<<"sum: " <<c<<endl;
cout<<"squaresum: " <<d<<endl;
cout<<"size: " <<e<<endl;
getchar();
return 0;
}