Introduction to Gaussian filter Gaussian filter

Source: Internet
Author: User


Introduction to Gaussian filter


I try to introduce these playful things as low as possible--only the normal distribution function is needed as a basis to start playing the Gaussian filter of the image. Don ' t Panic!





In the usual image, there are very many pixel points, and very many cases are very much a large chunk of pixels recording a scene area. So this is the fact that the continuity of color changes in real life is simulated in a digital discrete environment (note. The discrete environment of a computer is a real simulation.)


What is Gaussian filtering going to pinch? A word of things. is to use the Gaussian function to take weights, in a region to carry out the weighted average!


simple things are never complicated . What is the definition of the project master? Is the person who simplifies the complex problem. The easier the better. There are no very difficult questions in the world, just the people who say it is not straightforward enough.


The formula is right here. What do you do? The following program implementation to "realize" and understand the Gaussian filter. No, I don't know.

X y represents the distance from the center point

The simplest single-channel black-and-white image processing function (short enough) is given here first. Can explain the algorithm can, the following will give the color image implementation function, in order to make the program algorithm easy to understand. I try not to invoke the MATLAB API, using the C language style to write the demo program.


The following programs have not been optimized for whatever

(after worrying about optimization, the program is easy to compare with the original algorithm ...) So the program will be slower when the user makes the Kernel form larger (kernel_size > 11)

%***********************************************************% Code writer:eof% Code FILE:GAUSSIAN_FILTER_FOR_DA rk_image.m% code date:2014.10.25% e-mail: [email protected]%% code description:%% Here's My imp Lementation of Gaussian filter which% is only work for single channel image.%% If you find something wrong with my C Ode, 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);    Percent 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 is 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)./(                    (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                                       Percent Sum all weighted neighboring pixel by Gaussian distribution.                       If Kernel_row > 0 && kernel_col > 0 && ... Kernel_row <= image_height && kernel_col <= image_width sum_val                       UE = Sum_value + ...                              Kernel (Kernel_row-kernel_row_start + 1,...                       Kernel_col-kernel_col_start + 1) * ...                    Image (Kernel_row,kernel_col);                    End End Output (row,col) = Sum_value/sum_of_kernel; End End End



Test:

Number of references: Kernel_size = one, epsilon = 2.5

The image on the left is the input. The right side is the resulting filter result.

Let's say you carefully analyze the Gaoshuan kernel_size get. heavy words will find. The fact that kernel_size is too large is of no practical significance. The effects are small, generally 7 and 11 are very close to the results. The real effect of the blur effect is epsilon, where the value of epsilon is 2.5, assuming you take a smaller value. The degree of blurring of the image is weak (ie. Clear)

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2lubxlozwfyda==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">


Now that's over. Gaussian filtering of single-channel images. The Gaussian filter of the color picture becomes the logical, very easy.

Here I have integrated a single-channel and three-channel image Gaussian filter, written as a function (hehe. I Love Robust)



Why three-channel for so long pinch .... It feels like a long run ... I'm going to try to optimize it later.

The following is the output effect with kernel_win_size = 3, epsilon = 1.5

The left side is the original, and the right is the filtered result.




%***********************************************************% Code writer:eof% Code FILE:GAUSSIAN_FILTER_FOR_IM age.m% code date:2014.10.25% e-mail: [email protected]%% code description:%% here's my implemen Tation of Gaussian filter.% Parameter @Image is the inputed Image, @Kernel_size% describe the size of the filter ke Rnel and @epsilon is the% of parameter in Normal-distribution which you be familiar with.%% If you find something Wro Ng with my Code, 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);        Percent 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 is 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).            /(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_e                            ND-percent 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 percent% never forget to divide ' s                    Um_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


Doubts:

You may have encountered the following "Gaussian filter kernel" before


In fact, this is the distribution data that someone else has already calculated, and then it is used directly.

And we're using a highly customizable form size of kernel_size.

The essence is the same

Don ' t panic:)



Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Introduction to Gaussian filter Gaussian filter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.