当前位置: 首页 > news >正文

高斯模糊的简单算法

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

来自 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. 稍候翻译,并给出小例子

转载于:https://my.oschina.net/ifeixiang/blog/339498

相关文章:

  • 扫盲!直播app源码搭建直播平台为什么要用到ShareSDK
  • php,perl计算crc
  • 用 Redis 轻松实现秒杀系统的构思
  • UITableView/UIScrollView 不能响应TouchBegin 的处理 及窥见 hitTest:withEvent:
  • UDP,TCP之间的区别
  • springboot 系列教程六:springboot mybatis集成
  • windows添加开机启动项
  • 关于Docker文件系统
  • XP和Win 7双系统安装说明和注意事项
  • jQuery之getAll()和cleanData()
  • 利用pig分析cdn访问日志内指定时间段的url访问次数
  • 6本Python好书上新,来撩~
  • cursor:hand与cursor:pointer的区别介绍
  • 【AC自动机】AC自动机
  • Java 生成 exe 文件
  • Google 是如何开发 Web 框架的
  • angular2开源库收集
  • CSS进阶篇--用CSS开启硬件加速来提高网站性能
  • js正则,这点儿就够用了
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • 闭包--闭包作用之保存(一)
  • 从PHP迁移至Golang - 基础篇
  • 简单基于spring的redis配置(单机和集群模式)
  • 如何设计一个微型分布式架构?
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 深入浅出Node.js
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • 用element的upload组件实现多图片上传和压缩
  • 走向全栈之MongoDB的使用
  • 《天龙八部3D》Unity技术方案揭秘
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • 翻译 | The Principles of OOD 面向对象设计原则
  • ​html.parser --- 简单的 HTML 和 XHTML 解析器​
  • ​渐进式Web应用PWA的未来
  • ​学习一下,什么是预包装食品?​
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (附源码)springboot美食分享系统 毕业设计 612231
  • (实战篇)如何缓存数据
  • (转)负载均衡,回话保持,cookie
  • (转)使用VMware vSphere标准交换机设置网络连接
  • **登录+JWT+异常处理+拦截器+ThreadLocal-开发思想与代码实现**
  • .bat批处理(五):遍历指定目录下资源文件并更新
  • .NET 2.0中新增的一些TryGet,TryParse等方法
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .NET Framework 3.5中序列化成JSON数据及JSON数据的反序列化,以及jQuery的调用JSON
  • .NET Framework与.NET Framework SDK有什么不同?
  • .NET MVC第三章、三种传值方式
  • .NET 反射 Reflect
  • .Net6 Api Swagger配置
  • @TableId注解详细介绍 mybaits 实体类主键注解
  • [145] 二叉树的后序遍历 js
  • [Android Pro] android 混淆文件project.properties和proguard-project.txt
  • [BZOJ] 2044: 三维导弹拦截