在C#中实现高斯模糊,可以使用System.Drawing库。高斯模糊是一种基于高斯函数的滤波器,它可以有效地平滑图像。以下是详细的步骤,包括生成高斯核并应用到图像上的代码示例。
1. 生成高斯核
首先,我们需要编写一个方法来生成高斯核。
public static float[,] CreateGaussianKernel(int length, float weight)
{float[,] kernel = new float[length, length];float sumTotal = 0;int kernelRadius = length / 2;float distance = 0;float calculatedEuler = 1.0f / (2.0f * (float)Math.PI * weight * weight);for (int filterY = -kernelRadius; filterY <= kernelRadius; filterY++){for (int filterX = -kernelRadius; filterX <= kernelRadius; filterX++){distance = ((filterX * filterX) + (filterY * filterY)) / (2 * (weight * weight));kernel[filterY + kernelRadius, filterX + kernelRadius] = calculatedEuler * (float)Math.Exp(-distance);sumTotal += kernel[filterY + kernelRadius, filterX + kernelRadius];}}for (int y = 0; y < length; y++){for (int x = 0; x < length; x++){kernel[y, x] = kernel[y, x] * (1.0f / sumTotal);}}return kernel;
}
2. 应用卷积滤波器
编写一个方法来应用卷积滤波器,将高斯核应用到图像上。
public static Bitmap ApplyConvolutionFilter(Bitmap sourceImage, float[,] kernel)
{int width = sourceImage.Width;int height = sourceImage.Height;BitmapData srcData = sourceImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);Bitmap resultImage = new Bitmap(width, height);BitmapData resultData = resultImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);int bytesPerPixel = 4;int stride = srcData.Stride;IntPtr srcScan0 = srcData.Scan0;IntPtr resultScan0 = resultData.Scan0;int kernelWidth = kernel.GetLength(1);int kernelHeight = kernel.GetLength(0);int kernelOffset = kernelWidth / 2;unsafe{byte* srcPtr = (byte*)srcScan0.ToPointer();byte* resultPtr = (byte*)resultScan0.ToPointer();for (int y = kernelOffset; y < height - kernelOffset; y++){for (int x = kernelOffset; x < width - kernelOffset; x++){float blue = 0.0f;float green = 0.0f;float red = 0.0f;for (int ky = -kernelOffset; ky <= kernelOffset; ky++){for (int kx = -kernelOffset; kx <= kernelOffset; kx++){int pixelPos = ((y + ky) * stride) + ((x + kx) * bytesPerPixel);blue += srcPtr[pixelPos] * kernel[ky + kernelOffset, kx + kernelOffset];green += srcPtr[pixelPos + 1] * kernel[ky + kernelOffset, kx + kernelOffset];red += srcPtr[pixelPos + 2] * kernel[ky + kernelOffset, kx + kernelOffset];}}int resultPos = (y * stride) + (x * bytesPerPixel);resultPtr[resultPos] = (byte)Math.Min(Math.Max(blue, 0), 255);resultPtr[resultPos + 1] = (byte)Math.Min(Math.Max(green, 0), 255);resultPtr[resultPos + 2] = (byte)Math.Min(Math.Max(red, 0), 255);resultPtr[resultPos + 3] = 255; // Alpha channel}}}sourceImage.UnlockBits(srcData);resultImage.UnlockBits(resultData);return resultImage;
}
3. 使用高斯模糊处理图像
最后,编写一个程序来加载图像、应用高斯模糊滤波器,并保存处理后的图像。
using System;using System.Drawing;using System.Drawing.Imaging;
class Program
{static void Main(){// 加载原始图像Bitmap sourceImage = new Bitmap("path_to_your_image.jpg");// 创建高斯核float weight = 1.0f;int kernelSize = 5; // 核大小可以根据需要调整float[,] kernel = CreateGaussianKernel(kernelSize, weight);// 应用高斯模糊滤波器Bitmap resultImage = ApplyConvolutionFilter(sourceImage, kernel);// 保存处理后的图像resultImage.Save("path_to_save_filtered_image.jpg");}public static float[,] CreateGaussianKernel(int length, float weight){float[,] kernel = new float[length, length];float sumTotal = 0;int kernelRadius = length / 2;float distance = 0;float calculatedEuler = 1.0f / (2.0f * (float)Math.PI * weight * weight);for (int filterY = -kernelRadius; filterY <= kernelRadius; filterY++){for (int filterX = -kernelRadius; filterX <= kernelRadius; filterX++){distance = ((filterX * filterX) + (filterY * filterY)) / (2 * (weight * weight));kernel[filterY + kernelRadius, filterX + kernelRadius] = calculatedEuler * (float)Math.Exp(-distance);sumTotal += kernel[filterY + kernelRadius, filterX + kernelRadius];}}for (int y = 0; y < length; y++){for (int x = 0; x < length; x++){kernel[y, x] = kernel[y, x] * (1.0f / sumTotal);}}return kernel;}public static Bitmap ApplyConvolutionFilter(Bitmap sourceImage, float[,] kernel){int width = sourceImage.Width;int height = sourceImage.Height;BitmapData srcData = sourceImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);Bitmap resultImage = new Bitmap(width, height);BitmapData resultData = resultImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);int bytesPerPixel = 4;int stride = srcData.Stride;IntPtr srcScan0 = srcData.Scan0;IntPtr resultScan0 = resultData.Scan0;int kernelWidth = kernel.GetLength(1);int kernelHeight = kernel.GetLength(0);int kernelOffset = kernelWidth / 2;unsafe{byte* srcPtr = (byte*)srcScan0.ToPointer();byte* resultPtr = (byte*)resultScan0.ToPointer();for (int y = kernelOffset; y < height - kernelOffset; y++){for (int x = kernelOffset; x < width - kernelOffset; x++){float blue = 0.0f;float green = 0.0f;float red = 0.0f;for (int ky = -kernelOffset; ky <= kernelOffset; ky++){for (int kx = -kernelOffset; kx <= kernelOffset; kx++){int pixelPos = ((y + ky) * stride) + ((x + kx) * bytesPerPixel);blue += srcPtr[pixelPos] * kernel[ky + kernelOffset, kx + kernelOffset];green += srcPtr[pixelPos + 1] * kernel[ky + kernelOffset, kx + kernelOffset];red += srcPtr[pixelPos + 2] * kernel[ky + kernelOffset, kx + kernelOffset];}}int resultPos = (y * stride) + (x * bytesPerPixel);resultPtr[resultPos] = (byte)Math.Min(Math.Max(blue, 0), 255);resultPtr[resultPos + 1] = (byte)Math.Min(Math.Max(green, 0), 255);resultPtr[resultPos + 2] = (byte)Math.Min(Math.Max(red, 0), 255);resultPtr[resultPos + 3] = 255; // Alpha channel}}}sourceImage.UnlockBits(srcData);resultImage.UnlockBits(resultData);return resultImage;}
}
图像对比
- 原图:
- 处理后:
注意事项
- 确保路径 path_to_your_image.jpg 和 path_to_save_filtered_image.jpg 是正确的。
- 调整高斯核的大小和权重以获得所需的模糊效果。
- System.Drawing。
通过上述步骤,你可以在C#中实现图像的高斯模糊,对图像进行高斯模糊。