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. role &#39;PLUSTRACE&#39; does not exist

    I have created a new user named watson and granted the related priviledges as following: SQL> cre ...

  2. oracle在SQLPLUS 和PLSQL建 job 的区别

    oracle在SQLPLUS 和PLSQL建 job 的区别 //建立job variable test_job_really number; begin dbms_job.submit(:test_ ...

  3. atcoder它A Mountaineer

    Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB Problem Dave is a mountaineer. He is ...

  4. Net 项目构建基于Jenkins + Github + Mono 的持续集成环境

    Net 项目构建基于Jenkins + Github + Mono 的持续集成环境 阅读目录 1 安装 2 配置 3 测试 在Redhat enterprise 6.5 的服务器上,为在gutub 上 ...

  5. android 内存泄漏分析技巧

    java虚拟机执行一般都有一个内存界限,超过这个界限,就会报outofmemory.这个时候一般都是存在内存泄漏.解决内存泄漏问题,窃以为分为两个步骤:分析应用程序是否真的有内存泄漏,找到内存泄漏的地 ...

  6. Git协作流程(转)

    Git 作为一个源码管理系统,不可避免涉及到多人协作. 协作必须有一个规范的流程,让大家有效地合作,使得项目井井有条地发展下去."协作流程"在英语里,叫做"workflo ...

  7. Timer Swing

    一个Swing的例子,按钮控件上中文出现乱码: 试了网上的设置Font,或将汉字使用new  String(str.getBytes(),"GBK")对展示的汉字进行编码.都无效. ...

  8. net 试图加载格式不正确的程序。(Exception from HRESULT: 0x8007000B)

    原文:net 试图加载格式不正确的程序.(Exception from HRESULT: 0x8007000B) Server Error in '/' Application. 试图加载格式不正确的 ...

  9. Chromium Graphics Update in 2014(滑动)

    原创文章,转载请注明为链接原始来源对于http://blog.csdn.net/hongbomin/article/details/40897433. 摘要:Chromium图形栈在2014年有多项改 ...

  10. HDU 5052 Yaoge’s maximum profit 光秃秃的树链拆分 2014 ACM/ICPC Asia Regional Shanghai Online

    意甲冠军: 特定n小点的树权. 以下n每一行给出了正确的一点点来表达一个销售点每只鸡价格的格 以下n-1行给出了树的侧 以下Q操作 Q行 u, v, val 从u走v,程中能够买一个鸡腿,然后到后面卖 ...