void MeanFilterSize(unsigned short *input,unsigned short *output,int ksize,int width,int height)
{
//CV_Assert(ksize % 2 == 1);
int *kernel = new int[ksize*ksize];
for (int i = 0; i < ksize*ksize; i++) // 均值滤波所以都为1
kernel[i] = 1;
int len = ksize / 2;
int ptwidth = width +len*2;
int ptheight = height +len*2;
unsigned short* pt = new unsigned short[ptwidth*ptheight];
memset(pt,0, ptheight*ptwidth*sizeof(unsigned short));
//memcpy(pd, input, height*width * sizeof(unsigned short));
int channel=1;
for (int row = 0; row < ptheight; row++)//添加边框的过程
{
for (int col = 0; col < ptwidth; col++)
{
for (int c = 0; c < channel; c++)
{
if (row >= len && row < ptheight - len && col >= len && col < ptwidth - len)
pt[(ptwidth* row + col)*channel + c] = input[(width * (row - len) + col - len) * channel + c];
else
pt[(ptwidth * row + col)*channel + c] =32768;
}
}
}
//创建结果矩阵
unsigned short* pd = new unsigned short[width*height];
memset(pd, 32768, height*width * sizeof(unsigned short));
//memcpy(pd, input, height*width * sizeof(unsigned short));
for (int row = len; row < ptheight - len; row++)//卷积的过程
{
for (int col = len; col < ptwidth - len; col++)
{
for (int c = 0; c < channel; c++)
{
float t = 0;
for (int x = -len; x <= len; x++)
{
for (int y = -len; y <= len; y++)
{
//cout << "v=" << pt[((row + x) * ptwidth + col + y) * channel + c] << endl;
t += kernel[(len + x) * ksize + y + len] *(double) pt[((row + x) * ptwidth + col + y) * channel + c];
}
}
//cout << "vbalue- " << unsigned short(t / (ksize*ksize)) << endl;
pd[(width * (row - len) + col - len) * channel + c] = unsigned short(t / (ksize*ksize*1.0));//防止数据溢出ushort是16为数据
}
}
}
memcpy(output,pd,width*height*sizeof(unsigned short));
delete[] pd;
delete[] pt;
delete[] kernel;
}