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. Maven+Spring

    Maven+Spring 关于Maven Maven是一个用于项目构建的工具,通过它便捷的管理项目的生命周期.即项目的jar包依赖,开发,测试,发布打包. 做过.NET的人应该会联想到Nuget,是的 ...

  2. OpenCV2学习笔记(十四):基于OpenCV卡通图片处理

    得知OpenCV有一段时间.除了研究的各种算法的内容.除了从备用,据导游书籍和资料,尝试结合链接的图像处理算法和日常生活,第一桌面上(随着摄像头)完成了一系列的视频流处理功能.开发平台Qt5.3.2+ ...

  3. Oracle拉进sqlserver表声明的建设

    我们将Oracle数据被同步到sqlserver时间,早餐在sqlserver表中的端构造. 我们是不同步的复杂领域,只考虑以下四种数据类型. Oracle到SQLServer做的映射: int -& ...

  4. height/innerHeight/outerHeight

    <script> $(document).ready(function(){ alert("height:"+$("#div").height()) ...

  5. iOS 生成随机颜色(UIColor)

    #import <UIKit/UIKit.h> @interface UIColor (RandomColor) +(UIColor *) randomColor; @end #impor ...

  6. Swift的74标准功能

    Swift中共同拥有74个内建函数,可是在Swift官方文档("The Swift Programming Language")中仅仅记录了7中.剩下的67个都没有记录. 本文将列 ...

  7. Pro Aspnet MVC 4读书笔记(5) - Essential Tools for MVC

    Listing 6-1. The Product Model Class using System; using System.Collections.Generic; using System.Li ...

  8. ABP之动态WebAPI

    ABP之动态WebAPI ABP的动态WebApi实现了直接对服务层的调用(其实病没有跨过ApiController,只是将ApiController公共化,对于这一点的处理类似于MVC,对服务端的 ...

  9. AngularJS html5Mode与ASP.NET MVC路由共存

    前言 很久之前便听说AngularJS,非常酷,最近也比较火,我也在持续关注这个技术,只是没有认真投入学习.前不久公司找我们部门做一个OA系统(想省下几万大洋的费用),第一时间便想到AngularJS ...

  10. 面向对象三大特征之继承(extends)——Java笔记(六)

    继承:            从一般到特殊的关系,是一种拓展关系,子类对象是父类的一种,也可称为”is a“的关系 泛化:         把子类里的共性抽取到父类里的来的过程 特化:         ...