IamEasy_Man

Filled With Confidence , And Never Say Give Up !

【摘录】高斯滤波的C语言实现

http://steven-wang.appspot.com/gaussianFilter-65001.html 

高斯滤波是一种线性平滑滤波,适用于消除高斯噪声,广泛应用于图像处理的减噪过程。关于高斯滤波的数学原理说明可以从文章底部的参考资料中获得。

通俗的讲,高斯滤波就是对整幅图像进行加权平均的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到。

高斯滤波的具体操作是:用一个模板(或称卷积、掩模)扫描图像中的每一个像素,用模板确定的邻域内像素的加权平均灰度值去替代模板中心像素点的值。

一般的模板为3×3或5×5大小,其权值分布如下图:

高斯滤波模板

 

若使用3×3模板,则计算公式如下:
g(x,y)={f(x-1,y-1)+f(x-1,y+1)+f(x+1,y-1)+f(x+1,y+1)+[f(x-1,y)+f(x,y-1)+f(x+1,y)+f(x,y+1)]*2+f(x,y)*4}/16;
其中,f(x,y)为图像中(x,y)点的灰度值,g(x,y)为该点经过高斯滤波后的值。

以下是一段在OpenCV中实现的C语言程序,为一个使用3×3模板的高斯滤波函数,输入的参数依次为:当前灰度图像数据指针、图像宽度、图像高度。函数更新了灰度图像数据指针所指向的数据。

01void gaussianFilter(uchar* data, int width, int height)
02{
03    int i, j, index, sum;
04    int templates[9] = { 1, 2, 1,
05                         2, 4, 2,
06                         1, 2, 1 };
07    sum = height * width * sizeof(uchar);
08    uchar *tmpdata = (uchar*)malloc(sum);
09    memcpy((char*)tmpdata,(char*)data, sum);
10    for(i = 1;i < height - 1;i++)
11    {
12        for(j = 1;j < width - 1;j++)
13        {
14            index = sum = 0;
15            for(int m = i - 1;m < i + 2;m++)
16            {
17                for(int n = j - 1; n < j + 2;n++)
18                {
19                    sum +=
20                        tmpdata[m * width + n] *
21                        templates[index++];
22                }
23            }
24            data[i * width + j] = sum / 16;
25        }
26    }
27    free(tmpdata);
28}

Resources & Reference:
1、维基百科:高斯模糊

posted on 2010-08-17 14:31  IamEasy_Man  阅读(6395)  评论(0编辑  收藏  举报

导航