文档章节

高斯模糊的简单算法

ifeixiang
 ifeixiang
发布于 2014/10/31 17:49
字数 896
阅读 58
收藏 0
来自 http://www.swageroo.com/wordpress/how-to-program-a-gaussian-blur-without-using-3rd-party-libraries/

What is a Gaussian Blur?

Something I found fairly difficult to find online was a simple explanation on how to implement my own Gaussian Blur function. This article will explain how to implement one. The basic idea behind a Gaussian Blur is that each pixel becomes the average of the pixels around it, sort of. Instead of simply taking the average of all the pixels around it, you take a weighted average. The weighting of each pixel is greater if it is closer to the pixel you are currently blurring. The Gaussian Blur technique simply describes how to weigh each neighboring pixel. Imagine the pixel you are currently blurring is located at the peak of the hump in the image below and the pixels around it are receiving less weight as they get farther away. You can consider the image below to be considering up to 5 pixels away, this means the Gaussian blur has a ‘window’ of size 10, also known as a kernel size. This is where the Gaussian equation comes in, using it we can find out how much weight we want each pixel to receive and pixels receive less weight depending on its distance to the center pixel. Let’s explain what everything in this equation means: σ (lowercase sigma) – This is the blurring factor, the larger this number is, the smoother/blurrier the image becomes. e - This is simply euler’s number, a constant, 2.71828182846 x – This is the distance from the origin — The horizontal distance to the center pixel. y – This is the distance from the origin — The vertical distance to the center pixel. This means that x and y in this equation will be zero for the center pixel (the current pixel we want to blur), and x^2 + y^2 increases as we get farther away from the center, causing lower weights for pixels farther away.

Calculating a Gaussian Matrix, also known as a Kernel

Let’s say we wanted to find out how we would weigh neighboring pixels if we wanted a ‘window’ or ‘kernel size’ of 3 for our Gaussian blur. Of course the center pixel (the pixel we are actually blurring) will receive the most weight. Lets choose a  σ of 1.5 for how blurry we want our image. Here’s what our weight window would look like: With each weighting evaluated it looks like this: (Notice that the weighting for the center pixel is greatest) If you’re pretty observant you’ll notice that this matrix doesn’t add up 1. For this to represent a weights, all the weights when summed together will have to add up to 1. We can multiply each number by 1/sum to ensure this is true. The sum of this matrix is 0.4787147. This means we need to multiply the matrix by 1/0.4787147 so that all elements end up adding up to 1. We finally get the following matrix which represents how we will weight each pixel during a blur.

Applying this Gaussian Kernel Matrix to an image.

Lets say this is our image: (Each number can represent a pixel color from 0-255) To blur this image we need to ‘apply’ our kernel matrix to each pixel, i.e. we need to blur each pixel. Let’s say we want to blur pixel #25 (the pixel whose color is 25 in our image matrix). This means we get pixel 25 and replace 25 with the average of its neighbors. We weigh each of the neighbors (and 25 itself) with the kernel matrix we created earlier. So it would go as follows: Now that we have weighed each neighbor appropriately, we need to add up all the values we have and replace 25 with this new value! Loop this process with every single pixel and you will have a blurred image.

Caveats

Corner Pixels

When you need to apply this kernel matrix to a pixel in a corner, where you don’t have enough room to apply the matrix, a neat trick is to either ‘wrap around’ the image and use the pixels on the opposite side. This tends to work well if the image is intended to be tiled (typically not though) If you look at the image of the fish at the top of this article, you can tell that wrapping was used for the top row of the image since it is so dark. Another very simple solution is to just copy one of the nearest pixels into spots which are missing pixels so you can complete the process. The end result is definitely acceptable.

Time Complexity

It turns out that the simple procedure described above can be improved greatly. The time complexity of the above algorithm is O(rows*cols*kernelwidth*kernelheight). Gaussian blur has a special property called separability, the blur can be applied to each kernel row first in 1 pass, then each kernel column in another and you can achieve the same result. This means that you do not need to traverse the entire kernel matrix for each pixel. This lowers the time complexity to O(rows*cols*kernelheight + rows*cols*kernelwidth). You can read more on the Separability property on the  Gaussian Blur wikipedia page. 稍候翻译,并给出小例子

© 著作权归作者所有

上一篇: 很兴奋的一刻!
下一篇: js正则
ifeixiang
粉丝 16
博文 270
码字总数 99506
作品 0
广州
程序员
私信 提问
高斯模糊的算法

通常,图像处理软件会提供"模糊"(blur)滤镜,使图片产生模糊的效果。 "模糊"的算法有很多种,其中有一种叫做"高斯模糊"(Gaussian Blur)。它将正态分布(又名"高斯分布")用于图像处理。 ...

阮一峰
2012/11/14
0
0
高斯模糊算法的 C++ 实现

  2008 年在一个 PS 讨论群里,有网友不解 Photoshop 的高斯模糊中的半径是什么含义,因此当时我写了这篇文章:   对Photoshop高斯模糊滤镜的算法总结;   在那篇文章中,主要讲解了高...

hoodlum1980
2015/05/25
0
0
(精选)EasyPR--中文开源车牌识别系统 开发详解(3)

在本篇文章中我们对前3个步骤,分别是高斯模糊、灰度化和Sobel算子进行分析。 一、高斯模糊  1.目标   对图像去噪,为边缘检测算法做准备。    2.效果   在我们的车牌定位中的第一步...

芝麻粒儿
03/28
0
0
妹纸们的最爱 - 拓幻科技美颜SDK算法

现在各大手机制造商都在主推美颜效果,各种前后2000万像素,照亮你的美,各种逆光也清晰。其实这些看似神秘的美颜效果,除了依赖于手机像素之外,更重要的是攻城狮们对于美颜算法的构造。除了...

tillusory
2018/01/31
0
0
SIFT四部曲之——高斯滤波

版权声明:本文为博主原创文章,未经博主允许不得转载。博客不用于商业活动,博主对博客的使用,拥有最终解释权 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用...

hit2015spring
2016/10/22
0
0

没有更多内容

加载失败,请刷新页面

加载更多

全面剖析 Knative Eventing 0.6 版本新特性

前言 Knative Eventing 0.6 版本已经于5月15号正式发布。相比于0.5版本,此次发布包含了一些重要特性及更新。针对这些新特性以及更新,我们如何快速、精准的定位主要技术点。本篇文章针对这些...

阿里云官方博客
21分钟前
1
0
在闲鱼,我们如何用Dart做高效后端开发?

背景 像阿里其他技术团队以及业界的做法一样,闲鱼的大多数后端应用都是全部使用java来实现的。java易用、丰富的库、结构容易设计的特性决定了它是进行业务开发的最好语言之一。后端应用中数...

阿里云云栖社区
23分钟前
14
1
初学Docker容器网络不得不看的学习笔记

【技术沙龙002期】数据中台:宜信敏捷数据中台建设实践|宜信技术沙龙 将于5月23日晚8点线上直播,点击报名 一、关于Docker Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0...

宜信技术学院
25分钟前
9
0
Django框架配置

今天我们学习Django框架配置,首先先来看一下如何在Django中创建APP! 1.Django中创建APP 首先我们打开PyCharm,找到之前你创建的Django项目,我的项目是First_Project: (1)先选中你要运行...

彩色泡泡糖
27分钟前
1
0
git代码迁移

一、从阿里云code迁移到gitLab 1、在gitLab上新建一个项目Bicon,添加私钥 2、从原地址克隆一份裸版本库 git clone --bare git://xxxx_Bicon.git 这里的地址是阿里云code的地址 3、以镜像推送...

星爵22
28分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部