基于不同模板的高斯滤波函数

VS2008

int GuassSmooth(unsigned char *ptrSrc, unsigned char *ptrDst, int iWidth, int iHeight, int templateSize)
{
int guassTemp3[9] = { 1, 2, 1,
2, 4, 2,
1, 2, 1 };

/*int guassTemp5[25] = { 1, 2, 3, 2, 1,
2, 5, 6, 5, 2,
3, 6, 8, 6, 3,
2, 5, 6, 5, 2,
1, 2, 3, 2, 1 };*/
int guassTemp5[25] = { 1, 4, 7, 4, 1,
4, 16, 26, 16, 4,
7, 26, 41, 26, 7,
4, 16, 26, 16, 4,
1, 4, 7, 4, 1 };
int guassTemp7[49] = { 1, 4, 7, 10, 7, 4, 1,
4, 12, 26, 33, 26, 12, 4,
7, 26, 55, 71, 55, 26, 7,
10, 33, 71, 91, 71, 33, 10,
7, 26, 55, 71, 55, 26, 7,
4, 12, 26, 33, 26, 12, 4,
1, 4, 7, 10, 7, 4, 1,};
int i,j;
int step;
int tmp;
int weigth;
int index;
step = iWidth*iHeight*3*sizeof(unsigned char);
memcpy(ptrDst,ptrSrc,iWidth*iHeight*3*sizeof(unsigned char));
if (3 == templateSize)
{
weigth = 0;
for (i = 0;i<9;i++)
{
weigth +=guassTemp3[i];
}
for (i = 1; i<iHeight-1;i++)
{
for (j = 1;j<iWidth-1;j++)
{
tmp = guassTemp3[0]*(*(ptrSrc+(i-1)*iWidth*3+(j-1)*3))
+guassTemp3[1]*(*(ptrSrc+(i-1)*iWidth*3+(j)*3))
+guassTemp3[2]*(*(ptrSrc+(i-1)*iWidth*3+(j+1)*3))
+guassTemp3[3]*(*(ptrSrc+(i)*iWidth*3+(j-1)*3))
+guassTemp3[4]*(*(ptrSrc+(i)*iWidth*3+(j)*3))
+guassTemp3[5]*(*(ptrSrc+(i)*iWidth*3+(j+1)*3))
+guassTemp3[6]*(*(ptrSrc+(i+1)*iWidth*3+(j-1)*3))
+guassTemp3[7]*(*(ptrSrc+(i+1)*iWidth*3+(j)*3))
+guassTemp3[8]*(*(ptrSrc+(i+1)*iWidth*3+(j+1)*3));

tmp = tmp/weigth;
tmp = tmp > 255? 255:tmp;
tmp = tmp < 0? 0:tmp;
*(ptrDst+i*iWidth*3+j*3) = tmp;
}
}
//给边界行和列赋值;
//...
}
else if (5 == templateSize)
{
weigth = 0;
for (i = 0;i<25;i++)
{
weigth +=guassTemp5[i];
}

for (i = 2; i<iHeight-2;i++)
{
for (j = 2;j<iWidth-2;j++)
{
tmp = 0;
index = 0;
for (int m = i-2;m<i+3;m++)
{
for (int n = j-2;n<j+3;n++)
{
tmp += *(ptrSrc+m*iWidth*3+n*3)*guassTemp5[index++];
}
}

tmp = tmp/weigth;
tmp = tmp > 255? 255:tmp;
tmp = tmp < 0? 0:tmp;
*(ptrDst+i*iWidth*3+j*3) = tmp;
}
}
}
else if (7 == templateSize)
{
weigth = 0;
for (i = 0;i<49;i++)
{
weigth +=guassTemp7[i];
}

for (i = 3; i<iHeight-3;i++)
{
for (j = 3;j<iWidth-3;j++)
{
tmp = 0;
index = 0;
for (int m = i-3;m<i+4;m++)
{
for (int n = j-3;n<j+4;n++)
{
tmp += *(ptrSrc+m*iWidth*3+n*3)*guassTemp7[index++];
}
}
tmp = tmp/weigth;
tmp = tmp > 255? 255:tmp;
tmp = tmp < 0? 0:tmp;
*(ptrDst+i*iWidth*3+j*3) = tmp;
}
}
}

return 0;
}

你可能喜欢

  • 高斯滤波
  • 平滑滤波
  • 世界500强企业
  • 工程电路
  • matlab实例教程
  • 中值滤波
  • 高斯函数竞赛

基于不同模板的高斯滤波函数相关文档

最新文档

返回顶部