### C# Tutorial: How To Create Gaussian Blur

Gaussian blur is an image processing operation, that reduces noise in images. It does so by a convolution process, using a matrix that contains values calculated by a Gaussian...

Filter by Category

- C# Tutorial(85)
- C# Image Processing(69)
- Morphological Processes(20)
- Image Processing(16)
- Image Restoration and Reconstruction(16)
- Image Segmentation(13)
- C# Data Structures And Algorithms(11)
- Frequency Domain Filtering(8)
- Color Image Processing(8)
- Image Noise(6)
- Grayscale Morphology(5)
- Thresholding(4)
- Sorting Algorithms(4)
- Mean Filters(4)
- Order-Statistic Filters(4)
- Morphological Reconstruction(3)
- Edge Detection(3)
- Simple Lists(2)
- Linked Lists(2)
- Adaptive Filters(2)
- RGB to HSI Color Model(2)
- Tone and Color Corrections(2)
- Landing Pages(1)
- Point Detection(1)
- Line Detection(1)
- Social Games(1)
- Region Growing Segmentation(1)
- Region Splitting And Merging(1)
- Region Segmentation With K Means Clustering(1)
- Region Segmentation Using Superpixels(1)
- C# Arrays(1)
- C# Basics(1)
- Sorted Lists(1)
- Stacks(1)
- Queues(1)
- Bandreject Filters(1)
- Bandpass filters(1)
- Notch Filters(1)
- Intensity Slicing and Color Coding(1)
- Color Slicing(1)
- Histogram Processing Color Images(1)
- Color Image Smoothing And Sharpening(1)
- Using Color In Image Segmentation(1)
- Digital Image Watermarking(1)

Back to Latest Articles
###
C# Tutorial: How To Create Gaussian Blur

###
C# Tutorial: How To Apply Erosion To An Image

###
C# Tutorial: How To Apply Dilation To An Image

###
C# Tutorial: How To Apply Sobel Operator To An Image

###
C# Tutorial: How To Apply Contrast Stretch To An Image

###
C# Tutorial: How To Convert a Color Image To Grayscale

###
Image optimization – Why and How [blog]

C# Tutorial

Gaussian blur is an image processing operation, that reduces noise in images. It does so by a convolution process, using a matrix that contains values calculated by a Gaussian...

C# Tutorial

Erosion is a morphological process, where pixels at an object boundaries are removed. Erosion and dilation are a pair of basic morphological transformations, which are completely...

C# Tutorial

Dilation is a simple morphology process which changes pixel intensities based on the change of intensities that occur at object boundaries. This process is used on grayscale...

C# Tutorial

What is sobel operator? Well, basically it’s 2 kernels, with which we can process an image in a way, that only edges are visible. It is commonly used for grayscale images,...

C# Tutorial

Contrast stretch or otherwise known as normalization is a process where your image’s intensity is changed in such a way, that dark pixels become darker and light pixels...

C# Tutorial

Grayscale images are basically black and white images. These images are good for programming, since their pixels hold only intensity values or in other words, shades of gray, they...

Landing Pages

Why is image optimization important? There are a few different image formats that you can choose from to display it on your website. But it is not all the same even if it may look...

C# Tutorial: How To Create Gaussian Blur

Gaussian blur is an image processing operation, that reduces noise in images. It does so by a convolution process, using a matrix that contains values calculated by a Gaussian formula. By using a convolutional filter of Gaussian blur, edges in our processed image are preserved better. This is a very crucial if we intend to use edge detection processes, such as Sobel operation.

Where x represents distance from the origin on horizontal axis, y represents distance from the origin on vertical axis and sigma is the standard deviation of Gaussian distribution.

Origin is represented by the center pixel of the kernel we are using to convolve our image. Center pixel recieves the highest Gaussian value, while the neighboring pixels receive the smaller values as the distance from the origin increases.

public static double[,] GaussianBlur(int lenght, double weight) { double[,] kernel = new double[lenght, lenght]; double kernelSum = 0; int foff = (lenght - 1) / 2; double distance = 0; double constant = 1d / (2 * Math.PI * weight * weight); for (int y = -foff; y <= foff; y++) { for (int x = -foff; x <= foff; x++) { distance = ((y * y) + (x * x)) / (2 * weight * weight); kernel[y + foff, x + foff] = constant * Math.Exp(-distance); kernelSum += kernel[y + foff, x + foff]; } } for (int y = 0; y < lenght; y++) { for (int x = 0; x < lenght; x++) { kernel[y, x] = kernel[y, x] * 1d / kernelSum; } } return kernel; }

Lenght and weight variables are input parameters for the function. The entire function outputs a kernel, with values calculated according to Gaussian function.

public static Bitmap Convolve(Bitmap srcImage, double[,] kernel) { int width = srcImage.Width; int height = srcImage.Height; BitmapData srcData = srcImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int bytes = srcData.Stride * srcData.Height; byte[] buffer = new byte[bytes]; byte[] result = new byte[bytes]; Marshal.Copy(srcData.Scan0, buffer, 0, bytes); srcImage.UnlockBits(srcData); int colorChannels = 3; double[] rgb = new double[colorChannels]; int foff = (kernel.GetLength(0) - 1) / 2; int kcenter = 0; int kpixel = 0; for (int y = foff; y < height - foff; y++) { for (int x = foff; x < width - foff; x++) { for (int c = 0; c < colorChannels; c++) { rgb[c] = 0.0; } kcenter = y * srcData.Stride + x * 4; for (int fy = -foff; fy <= foff; fy++) { for (int fx = -foff; fx <= foff; fx++) { kpixel = kcenter + fy * srcData.Stride + fx * 4; for (int c = 0; c < colorChannels; c++) { rgb[c] += (double)(buffer[kpixel + c]) * kernel[fy + foff, fx + foff]; } } } for (int c = 0; c < colorChannels; c++) { if (rgb[c] > 255) { rgb[c] = 255; } else if (rgb[c] < 0) { rgb[c] = 0; } } for (int c = 0; c < colorChannels; c++) { result[kcenter + c] = (byte)rgb[c]; } result[kcenter + 3] = 255; } } Bitmap resultImage = new Bitmap(width, height); BitmapData resultData = resultImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Marshal.Copy(result, 0, resultData.Scan0, bytes); resultImage.UnlockBits(resultData); return resultImage; }

Please leave a comment below, I’d love to see what you have to say on this matter.

## Comments

I see your blog needs some fresh content. Writing manually is time consuming, but there is

solution for this hard task. Just search for; Miftolo’s tools rewriter