Introduction to gaussian filter

我尝试尽可能低门槛的介绍这些好玩的东东~这里只须要正态分布函数作为基础就可以開始玩图像的高斯滤波了. Don't panic !

在通常的图像中,像素点都非常多,非常多情况都是非常多一大块像素点记录某一个场景区域。那么这就数字离散环境下模拟出了实际生活中颜色变化连续性的事实(注意。计算机的离散环境都是对真实的模拟.)

高斯滤波是怎么回事捏?一句话的事情。就是利用高斯函数取权值,在一个区域内进行加权平均!

简单的事情永远别搞复杂了。project师的定义是什么。就是把复杂问题简单化的人. 越简单越好. 世界上本没有非常困难的问题,仅仅是阐述的人说的不够直白而已.

公式就在这里。怎么做?以下通过程序实现来“体会”并领悟高斯滤波. 不信我扯不清楚

x y 代表距离中心点的距离

这里先给出最简单的单通道黑白图像处理的函数(足够短小。可以说明算法就可以,后面会给出彩色图像的实现函数,为了把程序算法便于理解。我尽量不调用matlab的API,使用C语言的风格编写demo程序).

以下的程序没有经过不论什么优化

(操心优化之后程序便于和原来算法对比理解...所以当用户把kernel窗体调的比較大的时候(Kernel_size > 11的时候),程序会比較慢)

%***********************************************************
% code writer : EOF
% code file : gaussian_filter_for_dark_Image.m
% code date : 2014.10.25
% e-mail : jasonleaster@gmail.com
%
% Code Description:
%
% Here is my implementation of gaussian filter which
% is only work for single channel image.
%
% If you find something wrong with my code ,please touch
% me by e-mail.
%************************************************************** function Output = gaussian_filter_for_dark_Image(Image,Kernel_size,epsilon) if size(Image,3) ~= 1
fprintf('Hey guys, please input a single channel image.\n');
end Location_X = zeros(Kernel_size,Kernel_size);
Location_Y = zeros(Kernel_size,Kernel_size); %% Initialization for original Location.
for row = 1 : Kernel_size
for col = 1 : Kernel_size
Location_X(row ,col) = (col -1) - floor(Kernel_size/2);
Location_Y(row ,col) = (row -1) - floor(Kernel_size/2);
end
end Kernel = zeros(Kernel_size,Kernel_size); for row = 1 : Kernel_size
for col = 1 : Kernel_size % Oh , Attention. Here we are gonna to compute the Kernel of
% our filter.
Kernel(row,col) = (1/(2*pi*(epsilon.^2))) * ...
exp( - (Location_X(row,col).^2 + Location_Y(row,col).^2)./(2* (epsilon.^2) )); end
end sum_of_Kernel = sum(Kernel(:)); Image_Height = size(Image,1);
Image_Width = size(Image,2); Output = zeros(Image_Height,Image_Width); for row = 1: Image_Height
for col = 1: Image_Width sum_value = 0; % Set the patch start location and end location.
Kernel_row_start = row - floor(Kernel_size/2);
Kernel_col_start = col - floor(Kernel_size/2);
Kernel_row_end = row + floor(Kernel_size/2);
Kernel_col_end = col + floor(Kernel_size/2); for Kernel_row = Kernel_row_start : Kernel_row_end
for Kernel_col = Kernel_col_start : Kernel_col_end %% Sum all weighted neighboring pixel by gaussian distribution. if Kernel_row > 0 && Kernel_col > 0 && ...
Kernel_row <= Image_Height && Kernel_col <= Image_Width sum_value = sum_value + ...
Kernel(Kernel_row - Kernel_row_start + 1,...
Kernel_col - Kernel_col_start + 1) *...
Image(Kernel_row,Kernel_col);
end
end
end Output(row,col) = sum_value/sum_of_Kernel; end
end end

測试:

參数:Kernel_size = 11, epsilon = 2.5

左图为输入图像。右边是得到的滤波结果.

假设你细致分析过Kernel_size得到的高斯权重的话就会发现。事实上Kernel_size过大是没有什么实际意义的.影响不大,一般7 和11得到的结果都非常接近。真正影响模糊效果的是epsilon, 这里epsilon取值为2.5,假设你取值小一点。图像的模糊程度就会弱(即。清晰)

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2lubXloZWFydA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

既然完毕了单通道图像的高斯滤波。彩色图片的高斯滤波就变得顺理成章了,非常easy.

这里我整合了单通道和三通道的图片高斯滤波,写成一个函数(嘿嘿。I love robust)

为嘛三通道的就这么久捏....感觉跑好久...以后还是要优化一下

以下是用Kernel_win_size = 3, epsilon = 1.5的输出效果

左边是原图,右边是滤波后的结果.

%***********************************************************
% code writer : EOF
% code file : gaussian_filter_for_Image.m
% code date : 2014.10.25
% e-mail : jasonleaster@gmail.com
%
% Code Description:
%
% Here is my implementation of gaussian filter .
%
% Parameter @Image is the inputed image, @Kernel_size
% describe the size of filter kernel and @epsilon is the
% parameter in normal-distribution which you are familiar with.
%
% If you find something wrong with my code ,please touch
% me by e-mail.
%************************************************************** function Output = gaussian_filter_for_Image(Image,Kernel_size,epsilon) if size(Image,3) ~= 1 && size(Image,3) ~= 3
fprintf('Hey guys, please input a single or three channel image.\n');
end Location_X = zeros(Kernel_size,Kernel_size);
Location_Y = zeros(Kernel_size,Kernel_size); %% Initialization for original Location.
for row = 1 : Kernel_size
for col = 1 : Kernel_size
Location_X(row ,col) = (col -1) - floor(Kernel_size/2);
Location_Y(row ,col) = (row -1) - floor(Kernel_size/2);
end
end Kernel = zeros(Kernel_size,Kernel_size); for row = 1 : Kernel_size
for col = 1 : Kernel_size % Oh , Attention. Here we are gonna to compute the Kernel of
% our filter.
Kernel(row,col) = (1/((2*pi*(epsilon.^2)))) * ...
exp( - (Location_X(row,col).^2 + Location_Y(row,col).^2)./(2* (epsilon.^2) )); end
end sum_of_Kernel = sum(Kernel(:)); Image_Height = size(Image,1);
Image_Width = size(Image,2); if size(Image,3) == 1
Output = zeros(Image_Height,Image_Width);
else
Output = zeros(Image_Height,Image_Width,3);
end for row = 1: Image_Height
for col = 1: Image_Width % Set the patch start location and end location.
Kernel_row_start = row - floor(Kernel_size/2);
Kernel_col_start = col - floor(Kernel_size/2);
Kernel_row_end = row + floor(Kernel_size/2);
Kernel_col_end = col + floor(Kernel_size/2); for channel = 1: size(Image,3) sum_value = 0;
for Kernel_row = Kernel_row_start : Kernel_row_end
for Kernel_col = Kernel_col_start : Kernel_col_end
%% Sum all weighted neighboring pixel by gaussian distribution. if Kernel_row > 0 && Kernel_col > 0 && ...
Kernel_row <= Image_Height && Kernel_col <= Image_Width sum_value = sum_value + ...
Kernel(Kernel_row - Kernel_row_start + 1,...
Kernel_col - Kernel_col_start + 1) *...
Image(Kernel_row,Kernel_col,channel);
end end
end %%
% Never forget to divide 'sum_of_kernel', otherwise your outputed image
% would looks like more dark than original image.
Output(row,col,channel) = sum_value/sum_of_Kernel;
end
end
end end

解惑:

之前你可能遇到过以下这种“高斯滤波核”

事实上,这就是别人已经算好的分布数据,然后直接用了。

而我们这里採用的是高度可定制的窗体大小Kernel_size。

本质是一样的

Don't panic :)

版权声明:本文博客原创文章。博客,未经同意,不得转载。

Introduction to gaussian filter 高斯滤波器的更多相关文章

  1. vs2015+opencv3.3.1 实现 c++ 彩色高斯滤波器(Gaussian Smoothing, Gaussian Blur, Gaussian Filter)

    //高斯滤波器 https://github.com/scutlzk#include <opencv2\highgui\highgui.hpp> #include <iostream ...

  2. 二维高斯滤波器(gauss filter)的实现

    我们以一个二维矩阵表示二元高斯滤波器,显然此二维矩阵的具体形式仅于其形状(shape)有关: def gauss_filter(kernel_shape): 为实现二维高斯滤波器,需要首先定义二元高斯 ...

  3. Introduction to Gaussian Processes

    Introduction to Gaussian Processes Gaussian processes (GP) are a cornerstone of modern machine learn ...

  4. C++实现高斯滤波器

    在matlab中,我们经常用到高斯滤波器,生成滤波器一般都是这样的函数psf =   fspecial('gauss', GaussSize, sigma),但是在vs2010中用到的高斯滤波器不能自 ...

  5. kalman filter卡尔曼滤波器- 数学推导和原理理解-----网上讲的比较好的kalman filter和整理、将预测值和观测值融和

    = 参考/转自: 1 ---https://blog.csdn.net/u010720661/article/details/63253509 2----http://www.bzarg.com/p/ ...

  6. Introduction of Servlet Filter(介绍javaweb组件之一过滤器filter)

    javaweb的三大组件都需要交给web服务器运行,都需要在web.xml文件中配置. ①Servlet:javax.servlet.Servlet通过http协议接受客户端的请求,并作出响应的Jav ...

  7. vs2015+opencv3.3.1 实现 c++ 灰度高斯滤波器

    #include <opencv2\highgui\highgui.hpp> #include <iostream> #include<vector> using ...

  8. Introduction of Servlet Filter(了解Servlet之Filter)

    API文档中介绍了public Interface Filter(公共接口过滤器) Servlet API文档中是这样介绍的: ‘A filter is an object that performs ...

  9. Caffe : Layer Catalogue(2)

    TanH / Hyperbolic Tangent 类型(type):TanH CPU 实现: ./src/caffe/layers/tanh_layer.cpp CUDA.GPU实现: ./src/ ...

随机推荐

  1. Docker创建MySQL集装箱

    原文链接:Docker创建MySQL集装箱 这样做的目的是创建一个MySQL的image,出来的容器里自己主动启动MySQL服务接受外部连接 步骤: 1. 首先创建一个文件夹并在文件夹下创建一个Doc ...

  2. uva 10825 - Anagram and Multiplication(暴力)

    题目链接:uva 10825 - Anagram and Multiplication 题目大意:给出m和n,要求找一个m位的n进制数,要求说该数乘以2~m中的随意一个数的结果是原先数各个位上数值的一 ...

  3. BZOJ 2947 Poi2000 促销 set

    标题效果:特定n天,首先插入一些每天.然后去掉最高值和最低值,要付出最大的值-至少值价格.乞讨n总天数支付的价格 堆/段树/平衡树光秃秃的标题 #include <set> #includ ...

  4. myeclipse egit 安装失败 org.eclipse.e4.ui.css.swt.theme 0.0.0

    [前言] 首先确保您可能会被安装在阅读本文之前,myeclipse egit, 见文章:http://blog.csdn.net/uikoo9/article/details/17247405 事实上 ...

  5. android内置存储器memory和第三方外部存储disk管理

    缓存管理这里 http://blog.csdn.net/intbird/article/details/38338713 图片处理在这里 http://blog.csdn.net/intbird/ar ...

  6. USACO maze1 BFS

    不写了很长的时间bfs该,很长一段时间的中间失误,当延期一次延伸成功的新节点的节点应该被标记为参观.否则,在某些情况下无限期延长队列. 输入一个小坑爹处理称号,能够进来当字符串被读取.然后用周围的墙上 ...

  7. Dynamic proxy

    import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflec ...

  8. NSIS:延时启动软件的几个方法及探索

    原文NSIS:延时启动软件的几个方法及探索 有时候,我们想要某软件开机启动,但又不要拖慢开机速度,那么,延时启动技术就显得比较重要了.轻狂在这方面曾经研究过,也实现了自己想要的功能,看看我是怎么做的吧 ...

  9. Android 学习历程摘要(一)

    初学Android,可能有些地方可能理解不正确,假设有朋友看到的话麻烦指正我一下,万分感谢. 1. 善用API DEMO,刚開始学习的人的需求基本在里面都能够满足,Eclipse导入API Demop ...

  10. HDU ACM 1068 最大独立集

    意甲冠军:n同学.有些学生将有宿命的男性和女性成为恋人.收集注定要成为爱好者求学生的最大数目不存在. 分析:独立设置,顶点设定图的一个子集,在休闲2不连续: 二分图:最大独立集 = 顶点 - 匹配的最 ...