Steven Wang's Blog
C'est la vie
rss
email
twitter
新浪微博
  • Home
  • About
  • Google Profile
  • 新浪微博

高斯滤波的C语言实现

4 Comments
Posted on 三月 21 2010

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

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

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

一般的模板为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模板的高斯滤波函数,输入的参数依次为:当前灰度图像数据指针、图像宽度、图像高度。函数更新了灰度图像数据指针所指向的数据。

void gaussianFilter(uchar* data, int width, int height)
{
	int i, j, index, sum;
	int templates[9] = { 1, 2, 1,
						 2, 4, 2,
						 1, 2, 1 };
	sum = height * width * sizeof(uchar);
	uchar *tmpdata = (uchar*)malloc(sum);
	memcpy((char*)tmpdata,(char*)data, sum);
	for(i = 1;i < height - 1;i++)
	{
		for(j = 1;j < width - 1;j++)
		{
			index = sum = 0;
			for(int m = i - 1;m < i + 2;m++)
			{
				for(int n = j - 1; n < j + 2;n++)
				{
					sum += 
						tmpdata[m * width + n] * 
						templates[index++];
				}
			}
			data[i * width + j] = sum / 16;
		}
	}
	free(tmpdata);
}

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

--End--
作者:Steven Wang | 可以转载, 转载时务必以超链接形式标明文章原始出处和作者信息及版权声明
网址:http://blog.stevenwang.name/gaussianFilter-65001.html

Tags: 高斯滤波 
Categories: 数字图像处理 

4 Comments

  1. 她说我有神经病 says:
    三月 23 2010 at 19:34 Reply

    沙发。看不懂- -
    那个。博客连接你了,以后会常来学习的。

  2. Steven Wang says:
    三月 23 2010 at 21:43 Reply

    @她说我有神经病:握手!互相学习~你的名字很有性格~

  3. CJC says:
    八月 30 2010 at 08:54 Reply

    请问7×7的模板是怎样的?

  4. jytwt says:
    九月 2 2011 at 15:50 Reply

    {1.f},
    {0.25f, 0.5f, 0.25f},
    {0.0625f, 0.25f, 0.375f, 0.25f, 0.0625f},
    {0.03125f, 0.109375f, 0.21875f, 0.28125f, 0.21875f, 0.109375f, 0.03125f}

Leave a Reply



About Me

    Steven Wang
    Student in Computer Software and Theory
    Life@Wuxi, Jiangsu
    Study@Jiangnan University
    more...

Feeds

  • Entries (RSS)
  • Comments (RSS)
  • 订阅到 Google Reader
  • 订阅到 抓虾
  • 订阅到 鲜果
  • 订阅到 QQ

Popular Posts

  • 围着脖子推GTalk机器人V1.0发布(26629)
  • 通过SSH + Chrome + Proxy Switchy!代理上网(19478)
  • 在GAE上部署Micolog博客系统(11098)
  • 围着脖子推V2.0 Beta1版发布 支持Twitter,新浪微博,人人网,嘀咕,做啥 同步更新(10660)
  • 围着脖子推GTalk机器人V1.0更新-增加接收Twitter更新等功能(10464)
  • 围着脖子推更新-增加同步更新网易微博、腾讯微博和搜狐微博(10092)
  • 在Matlab中实现Hough变换检测直线(8247)
  • Micolog主题(theme) —— translucence(7416)

Recent Posts

  • Steven Wang's 2011
  • 工作
  • T400升级Intel SSD
  • Java中的时区转换小结
  • 二值图像连通区标记之区域生长法
  • Steven Wang's 2010
  • 微博提醒应用上线
  • 双喜临门

Recent Comments

  • zhoudi:惊诧了,看来你应该和我是同门师兄弟。。。...
  • zhoudi:哇,校友。Wang Shitong的研究...
  • xiaozhang:第37这一行:int jMax = mi...
  • 小张:这一行:int jMax = min(w...
  • 小张:为什么运行出错呢...

Categories

  • Google App Engine(10)
  • 数字图像处理(11)
  • Micolog(7)
  • VPS(6)
  • 围着脖子推(15)
  • 人工神经网络(5)
  • 算法(11)
  • MyLife(16)
  • 媒体检索(4)
  • Others(8)
  • Python(2)

Archives

  • January 2012(1)
  • December 2011(2)
  • May 2011(1)
  • February 2011(1)
  • December 2010(3)
  • November 2010(1)
  • October 2010(1)
  • September 2010(4)
  • August 2010(2)
  • July 2010(5)
  • June 2010(4)
  • May 2010(7)

Blog roll

  • ~Issue
  • Fenng
  • 刘未鹏 | Mind Hacks
  • Grace Hopper
  • 林海听松
  • Yu Zheng
  • Johnny Han
  • xiang_闹闹
  • 静静的安静
  • Herei
  • Dbger
  • land of promise
  • 小龙的SEO学习之路
  • 星星
  • ISHENS|TECH
  • 天天软件园
  • joyone
  • leezhenchong's notes

  • Home
  • About
  • Google Profile
  • Twitter
  • 新浪微博
  • Login
Powered by Google App Engine  |   Designed by WebTreats  |   由 xuming 提供 Micolog程序