Gaussian smoothing Gaussian fuzzy Gaussian filter (Gaussian smoothing, Gaussian blur, Gaussian filter) C ++ implementation

Source: Internet
Author: User

Now that the smooth algorithm was developed, I had no idea how to name this article, so I had to list some keywords to facilitate searching.

We have mentioned the Mean Filter before, that is, the color of a pixel is determined by the average value of the nine squares centered on it. on this basis, the weighted average filter is developed. Here, Gaussian smoothing or filter is such an weighted average filter. how are these weights distributed? Let's take a look at several typical template examples:

We tried to use these filters to operate on our original graph and got a set of results:

Source image:

3x3 Gaussian:

5x5 Gauss:

 

From the perspective of the effect alone, both templates play a smooth role, but the degree is different in depth. Why does it play a smooth role theoretically? Obviously, the color of a pixel is determined not only by itself, but also by the weighting of the surrounding pixels, which objectively reduces the difference with the surrounding pixels. at the same time, these weights meet the rule that the closer the weight is, the larger the weight. theoretically, the distribution of these weights satisfies the so-called Gaussian distribution:


This is the one-dimensional formula.


This is a two-dimensional formula.

X and Y indicate the distance from the current vertex to the corresponding vertex, and the Specific templates are calculated by some special cases in the formula here. it should be noted that there are not only such special cases. from Wikipedia, you can easily find those complex templates, such:

Sample Gaussian Matrix

This is a sample matrix, produced by sampling the Gaussian filter
Kernel (with σ = 0.84089642) at the midpoints of each pixel and then
Normalising. Note that the center element (at [4, 4]) has the largest
Value, decreasing into rically as distance from the center increases.

0.00000067 0.00002292 0.00019117 0.00038771 0.00019117 0.00002292 0.00000067
0.00002292 0.00078633 0.00655965 0.01330373 0.00655965 0.00078633 0.00002292
0.00019117 0.00655965 0.05472157 0.11098164 0.05472157 0.00655965 0.00019117
0.00038771 0.01330373 0.11098164 0.22508352 0.11098164 0.01330373 0.00038771
0.00019117 0.00655965 0.05472157 0.11098164 0.05472157 0.00655965 0.00019117
0.00002292 0.00078633 0.00655965 0.01330373 0.00655965 0.00078633 0.00002292
0.00000067 0.00002292 0.00019117 0.00038771 0.00019117 0.00002292 0.00000067

If you see it, it's big. :) but it doesn't matter. For general applications, the previous example can already complete the task. For the code, let's give a 5x5 example:

/** <Br/> ** method to remove noise from the specified upted image by Gaussian filter value <br/> * @ Param converted upted input grayscale binary array with specified upted info <br/> * @ Param smooth output data for smooth result, the memory need to be allocated outside of the function <br/> * @ Param width of the input grayscale image <br/> * @ Param height of the input grayscale image <br /> */<br/> void gaussianfilter2 (unsigned char * successfully upted, unsigned char * smooth, int width, int height) <br/>{< br/> int templates [25] = {1, 4, 7, 4, 1, <br/> 4, 16, 26, 16, 4, <br/> 7, 26, 41, 26, 7, <br/> 4, 16, 26, 16, 4, <br/> 1, 4, 7, 4, 1 }; </P> <p> memcpy (smooth, upted, width * height * sizeof (unsigned char); <br/> for (Int J = 2; j <peight-2; j ++) <br/>{< br/> for (INT I = 2; I <width-2; I ++) <br/>{< br/> int sum = 0; <br/> int Index = 0; <br/> for (int m = J-2; m <j + 3; m ++) <br/> {<br/> for (INT n = I-2; n <I + 3; n + +) <br/> {<br/> sum + = Specified upted [M * width + N] * templates [index ++]; <br/>}< br/> sum/= 273; <br/> If (sum> 255) <br/> sum = 255; <br/> smooth [J * width + I] = sum; <br/>}< br/>}

Additionally, it is obvious that, similar to the mean filter, this filter does not eliminate verification noise.

 

 

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.