OpenCV Gaussian Blur

Source: Internet
Author: User

Gaussian Blur is an image fuzzy filter that calculates the transformation of each pixel in an image with a normal distribution. N-dimensional normal distribution equation is

In a two-dimensional space defined as

where R is the fuzzy radius (r2 = u2 + v2), Σ is the standard deviation of the normal distribution. In a two-dimensional space, the contours of the surface generated by this formula are concentric circles that begin with a normal distribution from the center. A convolution matrix that distributes nonzero pixels is transformed from the original image. The value of each pixel is a weighted average of the surrounding neighboring pixel values. The value of the original pixel has the largest Gaussian distribution value, so there is a maximum weight, and the neighboring pixels are getting farther away from the original pixels, and their weights are getting smaller. In this way, the blurring process retains the edge effect more than other equalization fuzzy filters, see scale space implementation.

Theoretically, the distribution of each point in the image is nonzero, which means that each pixel needs to contain an entire image. In practical applications, when calculating the discrete approximation of a Gaussian function, pixels outside the approximate 3σ distance can be considered as ineffective, and the calculation of these pixels can be ignored. In general, the image processing program only needs the computed matrix to guarantee the relevant pixel influence.

In addition to circular symmetry, Gaussian blur can also be used to calculate two independent one-dimensional spaces on a two-dimensional image, which is called linear . This means that the effect of using a two-dimensional matrix transformation can also be obtained by a Gaussian matrix transformation in the horizontal direction plus a Gaussian matrix transformation in the vertical direction. From a computational point of view, this is a useful feature, because it requires only a second calculation, and the irreducible matrix requires a secondary calculation, where m,N is the dimension of the image that needs to be filtered,m, n is the dimension of the filter.

The effect of multiple continuous Gaussian blur on an image can produce the same effect as a larger Gaussian blur, and the radius of a large Gaussian blur is the square root of the sum of squares of multiple Gaussian blur radii. For example, using a two-time Gaussian blur transform with a radius of 6 and 8 is equivalent to a Gaussian blur effect with a radius of 10. Depending on this relationship, using more than one continuous, smaller Gaussian blur will be less than a single Gaussian larger processing time.

Gaussian blur is often used in cases where the image size is reduced. In the cases of under-sampling, the image is usually treated with low-pass filtering prior to sampling. This ensures that false high-frequency information is not present in the sampled image. Gaussian Blur has good properties, such as no apparent boundary, so that it does not form a shock in the filtered image.

Original address

The type of image data in the actual application might be unsigned char, float, double, so I used a custom template data structure:

Template<typename t>class  imgtype{ public :    *data;     int width;     int height;     int depth;     int step;     int channels;};

I use the Gaussian blur in the x direction y direction respectively to achieve

Template<typename type>voidImgalgorithm<type>::gaussiansmooth (imgtype<type> *src,imgtype<type> *&dst,DoubleSigma) {Sigma=sigma>0? sigma:-Sigma; //Ivigos and matrix size (6*sigma+1) * (6*sigma+1)//Ksize is an odd number    intKsize = ((int) (sigma*3+0.5))*2+1; if(ksize==1) {DST=src; return; }    //compute a Gaussian core G (R) = 1/SQRT (2*pi*sigma) * EXP (-r*r/(2*sigma*sigma))    Double*kernel =New Double[Ksize]; DoubleScale =-0.5/(sigma*Sigma); DoubleCons = sqrt (-scale/PI); Doublesum =0; intKcenter = ksize/2;  for(intI=0; i<ksize;i++)    {        intx=i-Kcenter; Kernel[i]=cons*exp (x*x*Scale ); Sum+=Kernel[i]; }    //normalization to ensure that the Gaussian weights are between [0,1]printf".. Gauss kernel:\n"); printf ("... sigma=%lf, kernelsize=%d\n", sigma,ksize);  for(intI=0; i<ksize;i++) {Kernel[i]/=sum; printf (". .. kernel[%d]=%lf\n", I,kernel[i]); } printf ("Kcenter%d\n", Kcenter); DST=createimage (src->width,src->height,src->depth,src->channels); Imgtype<Type> *tmp=createimage (src->width,src->height,src->depth,src->channels); Type a[3];//Channel//x Direction one Gaussian blur     for(intI=0; i<src->height;i++)    {         for(intj=0; j<src->width;j++) {a[0]=a[1]=a[2]=0;  for(intk=0; k<src->channels;k++)            {                Doublesum=0;  for(intd=-kcenter;d<=kcenter;d++)                {                    if(d+j>=0&& d+j<src->width) {A[k]+ = src->data[i*src->step+ (d+j) *src->channels+k] * kernel[kcenter+d]; Sum+ = kernel[kcenter+d]; }} tmp->data[i*src->step+j*src->channels+k]=a[k]/sum; }        }    }        //Gaussian blur in the y direction     for(intI=0; i<src->height;i++)    {         for(intj=0; j<src->width;j++) {a[0]=a[1]=a[2]=0;  for(intk=0; k<src->channels;k++)            {                Doublesum=0;  for(intd=-kcenter;d<=kcenter;d++)                {                    if(d+i>=0&& d+i<src->height) {A[k]+ = tmp->data[(d+i) *src->step+j*src->channels+k] * kernel[kcenter+d]; Sum+ = kernel[kcenter+d]; }} DST->data[i*src->step+j*src->channels+k]=a[k]/sum;    }}} delete []kernel; Freeimage (TMP);}

1x5 Gaussian fuzzy matrix values:

OpenCV Gaussian Blur

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: 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.