Foundation of Image Processing-Gaussian low-pass filter template generation C implementation

Source: Internet
Author: User

() Code implementation

Perform Gaussian smoothing on the source image to remove computing noise in the image.
Void BMP: makegauss (double Sigma, double ** pdkernel, int * pnwindowsize ){
// Cyclic control variable
Int I;
// Center of the array
Int ncenter;
// The distance from a point in the array to the center point
Double DDIs;
// Intermediate variable
Double dvalue;
Double dsum;
Dsum = 0;

// Array length. Based on the knowledge of probability theory, select data within [-3 * Sigma, 3 * Sigma ].
// The data will cover the vast majority of the filter coefficients.
* Pnwindowsize = 1 + 2 * Ceil (3 * sigma );

// Center
Ncenter = (* pnwindowsize)/2;
// Allocate memory
* Pdkernel = new double [* pnwindowsize];

// Generate Gaussian data
For (I = 0; I <(* pnwindowsize); I ++)
{
DDIs = (double) (I-ncenter );
Dvalue = exp (-(1/2) * DDIs/(Sigma * sigma)/(SQRT (2 * PI) * sigma );
(* Pdkernel) [I] = dvalue;
Dsum + = dvalue;
}

// Normalization
For (I = 0; I <(* pnwindowsize); I ++)
{
(* Pdkernel) [I]/= dsum;
}
 
}

Void BMP: gaussiansmooth (u8_t * punchimg, int nwidth, int nheight, double Sigma, u8_t * punchsmthdimg ){
// Cyclic variable
Int y, X, I;

// Array length of Gaussian filter
Int nwindowsize;
// The Window Length is 1/2
Int nhalflen;
// One-dimensional Gaussian data filter
Double * pdkernel;

// Gaussian coefficient and point multiplication of image data
Double ddotmul;
// Sum of Gaussian filter coefficients
Double dweightsum;
// Intermediate variable
Double * pdtmp;

// Allocate memory
Pdtmp = new double [nwidth * nheight];

// Generate a one-dimensional Gaussian data filter
Makegauss (Sigma, & pdkernel, & nwindowsize );

// Makegauss return Window Length. Use this variable to calculate the half length of the window.
Nhalflen = nwindowsize/2;

// Filter in the X direction
For (y = 0; y <nheight; y ++)
{
For (x = 0; x <nwidth; X ++)
{
Ddotmul = 0;
Dweightsum = 0;
For (I = (-nhalflen); I <= nhalflen; I ++)
{
// Determine whether the image is inside the image
If (I + x)> = 0 & (I + x) <nwidth)
{
// Filter image data using Gaussian Coefficient
Ddotmul + = (double) punchimg [y * nwidth + (I + x)] * pdkernel [nhalflen + I];
Dweightsum + = pdkernel [nhalflen + I];
}
}
Pdtmp [y * nwidth + x] = ddotmul/dweightsum;
// Punchsmthdimg [y * nwidth + x] = (u8_t) (INT) ddotmul/dweightsum;
} // End for X
} // End for y

// Filter data in the Y direction
For (x = 0; x <nwidth; X ++)
{
For (y = 0; y <nheight; y ++)
{
Ddotmul = 0;
Dweightsum = 0;
For (I = (-nhalflen); I <= nhalflen; I ++)
{
// Determine whether the image is inside the image
If (I + Y)> = 0 & (I + Y) <nheight)
{
// Filter image data using Gaussian Coefficient
Ddotmul + = (double) pdtmp [(Y + I) * nwidth + x] * pdkernel [nhalflen + I];
Dweightsum + = pdkernel [nhalflen + I];
}
}
Punchsmthdimg [y * nwidth + x] = (u8_t) (INT) ddotmul/dweightsum;
} // End for y
} // End for X
 
// Release the memory
Delete [] pdkernel;
Pdkernel = NULL;
Delete [] pdtmp;
Pdtmp = NULL;
}

 

Main (){

U8_t ** new_temp_data;
U8_t * new_temp;
U32_t width, height;
Width = BMP _head-> img_head-> width;
Height = BMP _head-> img_head-> height;
New_temp_data = (u8_t **) malloc (u32_t) width * Height );
Memset (maid, (u8_t) 255, (u32_t) width * Height );
New_temp = (u8_t *) new_temp_data;
Memcpy (new_temp_data, BMP _head-> Buf, (u32_t) width * Height );

Gaussiansmooth (u8_t *) BMP _head-> Buf, width, height, 0.05, new_temp );
BMP _head-> Buf = new_temp_data;

}

Complete http://blog.csdn.net/yang1994/article/details/1492815 Gaussian call

() Generate Theory

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;
}
}
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");
 }
}

Http://blog.sina.com.cn/s/blog_71fa0df50100wodv.html

() Determine Gaussian filter parameters

Http://tsindahui.blog.sohu.com/166075850.html

Opencv implementation, in the init_gaussian_kernel function of cvfilter. cpp:

Sigmax = Sigma> 0? Sigma: (n/2-1) * 0.3 + 0.8;

Gaussian smoothing of color images

() Text description

Http://www.ruanyifeng.com/blog/2012/11/gaussian_blur.html

() Wikipedia and Chinese encyclopedia

Http://en.wikipedia.org/wiki/Convolution

Http://www.zwbk.org/MyLemmaShow.aspx? Lid = 126233

 

() Two-Dimensional Optimization

Http://www.cnblogs.com/easymind223/archive/2012/11/13/2768680.html

Http://blog.csdn.net/lanbing510/article/details/28696833

Foundation of Image Processing-Gaussian low-pass filter template generation C implementation

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.