Generate a Gaussian template (C ++)

Source: Internet
Author: User

 

Figure 1. Normal distribution equation in n-dimensional space (the formula is the same as that in figure 2)

 

Gaussian Blur is an image fuzzy filter that uses normal distribution to calculate the transformation of each pixel in an image.
The normal distribution equation of n-dimensional space is

(Figure 2)
In two-dimensional space, it is defined

(Figure 3)
Here, R is The Blur radius (R2 = u2 + V2), and σ is the standard deviation of normal distribution.
In two-dimensional space, the contour lines of the surface generated by this formula are concentric circles with normal distribution starting from the center. The convolution matrix composed of pixels with a non-zero distribution is transformed with the original image.

 

/*

E's X power function
For example
Exp (1) = 1 power of e = E = 2. 718281828...
Exp (0) = 0 power of e = 1
Exp (2) = E square = 3890561...
E is a constant, equal to 2. 718281828...
*/
 

A 5x5 Gaussian convolution kernel with a standard deviation of 1.4:

2 4 5 4 2

4 9 12 9 4

5 12 15 12 5

4 9 12 9 4

2 4 5 4 2

Last multiplied by the ratio of 1/115

// Code

// Code 1 (self-implemented)

Void makegauss ()
{
Double Sigma = 1.4; // σ is the standard deviation of normal distribution. Here it is 1.4.
Double dresult [5] [5]; // used to store results
Double dresult1 [5] [5]; // used to store results
 
// Center of the array
Int ncenterx = 2, ncentery = 2; // start with 1 at the center point.

Int nsize = 5;

// The distance from a point in the array to the center point
Double DDIs;

Double Pi = 3.1415926535;
// Intermediate variable
Double dvalue;
Double dsum;
Dsum = 0;

Int I, J;
 
For (I = 0; I <nsize; ++ I)
{
For (j = 0; j <nsize; ++ J)
{
DDIs = (I-ncenterx) * (I-ncenterx) + (J-ncentery) * (J-ncentery );
Dvalue = exp (-DDIs/(2 * Sigma * sigma ))/
(2 * pI * Sigma * sigma );
Dresult [I] [J] = dvalue;
Dsum + = dvalue;
}
}

// Normalization
For (I = 0; I <nsize; ++ I)
{
For (j = 0; j <nsize; ++ J)
{
Dresult1 [I] [J] = dresult [I] [J]/dsum;
}
}

STD: cout <dsum <STD: Endl;
For (I = 0; I <nsize; ++ I)
{
For (j = 0; j <nsize; ++ J)
{
// Dresult1 is the result of Gauss, but dresult * 1.95*100 is the result of the article.

// A convolution kernel of 5x5 Gaussian with a standard deviation of 1.4
// Do not know why.
STD: cout <(INT) (dresult [I] [J] * 1.95*100) <"";
}
STD: cout <STD: Endl;
}
}

// Code 2 (found online)

Void Gauss ()
{
Int h_size;
Float siz, sigma;
Int I, J;
Printf ("Please input size of Gaussian Core/N ");
Scanf ("% d", & h_size );
Printf ("Please input Sigma:/N ");
Scanf ("% F", & sigma );
Siz = (h_size-1)/2;
Float ** A, ** B;
A = new float * [h_size];
For (INT I = 0; I B = new float * [h_size];
For (I = 0; I

For (I = 0; I {
For (j = 0; j {
A [I] [J] =-siz + J;
Printf ("% 4.2f", a [I] [J]);
}
Printf ("/N ");
}
Printf ("/N ");
For (I = 0; I {
For (j = 0; j {
B [I] [J] = A [J] [I];
Printf ("% 4.2f", B [I] [J]);
}
Printf ("/N ");
}
Printf ("/N ");
Float h_sum = 0;
For (I = 0; I {
For (j = 0; j {
A [I] [J] = A [I] [J] * A [I] [J];
B [I] [J] = B [I] [J] * B [I] [J];
A [I] [J] =-(A [I] [J] + B [I] [J])/(2 * Sigma * sigma );
A [I] [J] = exp (A [I] [J]);
If (A [I] [J] <0.0001) A [I] [J] = 0;
H_sum = h_sum + A [I] [J];
}
}
 
For (I = 0; I {
For (j = 0; j {
A [I] [J] = A [I] [J]/h_sum;
}
}
For (I = 0; I {
For (j = 0; j {
Printf ("% 4.4f", a [I] [J]);
}
Printf ("/N ");
}
}

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.