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