C#中图像过滤的高速性能

And*_*hin 2 .net c# filtering image-processing

我有位图.我想将中值滤波器应用于我的位图.但我不能使用GetPixel()和SetPixel(),因为速度对我来说是非常重要的因素.我需要非常快速的方法来做到这一点.也许可以用一个Graphics.DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes).

中值滤波后我想应用二值化滤波器(对于每个像素计算亮度:B = 0.299*R + 0.5876*G + 0.114B,如果亮度小于thresholdValue(thresholdValue是[0 ... 255]中我的任务的参数)那么结果图像中我的像素值是1,否则 - 0)二进制滤波器中的速度对我来说也很重要

Rub*_*ias 7

刚刚找到这个链接:在.NET中灰度图像的快速方法(C#)

/// <summary>
/// Grayscales a given image.
/// </summary>
/// <param name="image">
/// The image that is transformed to a grayscale image.
/// </param>
public static void GrayScaleImage(Bitmap image)
{
    if (image == null)
        throw new ArgumentNullException("image");

    // lock the bitmap.
    var data = image.LockBits(
                  new Rectangle(0, 0, image.Width, image.Height), 
                  ImageLockMode.ReadWrite, image.PixelFormat);
    try
    {
        unsafe
        {
            // get a pointer to the data.
            byte* ptr = (byte*)data.Scan0;

            // loop over all the data.
            for (int i = 0; i < data.Height; i++)
            {
                for (int j = 0; j < data.Width; j++)
                {
                    // calculate the gray value.
                    byte y = (byte)(
                        (0.299 * ptr[2]) + 
                        (0.587 * ptr[1]) + 
                        (0.114 * ptr[0]));

                    // set the gray value.
                    ptr[0] = ptr[1] = ptr[2] = y;

                    // increment the pointer.
                    ptr += 3;
                }

                // move on to the next line.
                ptr += data.Stride - data.Width * 3;
            }
        }
    }
    finally
    {
        // unlock the bits when done or when 
        // an exception has been thrown.
        image.UnlockBits(data);
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:查看更多信息:

  1. 使用LockBits方法访问图像数据
  2. GrayScale和ColorMatrix