设置白色和黑色像素取决于亮度

时间:2014-03-07 18:27:41

标签: c# bitmap lockbits

我正试图从这些或类似的图片中找到针:

我的解决方案是拍摄照片的平均亮度,并设置黑白像素取决于它。结果是这样的:

我不需要看到数字和那里的人员..只有针,但它在那里不是大问题。但我使用set,获取像素函数,它真的很慢我关于LockBits metod的东西,我用它来获得亮度,但我不知道如何使用它正确的setpixel。任何人都可以帮我解决我的代码吗?.. thnx

BitmapData imageData = imgPristroj.LockBits(new Rectangle(0, 0, imgPristroj.Width, imgPristroj.Height), ImageLockMode.ReadOnly, imgPristroj.PixelFormat);
        float brightness = 0;
        float average;
        unsafe
        {
            try
            {
                UnmanagedImage Unimg = new UnmanagedImage(imageData);
                int pixelSize = (Unimg.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
                byte* p = (byte*)Unimg.ImageData.ToPointer();

                for (int y = 0; y < Unimg.Height; y++)
                {
                    for (int x = 0; x < Unimg.Width; x++, p += pixelSize)
                    {
                        brightness += Unimg.GetPixel(x, y).GetBrightness();
                    }
                }
                average = brightness / (Unimg.Width * Unimg.Height);
            }
            finally
            {
                imgPristroj.UnlockBits(imageData); //Unlock
            }
        }


        img19 = (Bitmap)imgPristroj.Clone();

        for (int y = 0; y < img19.Height; y++)
        {
            for (int x = 0; x < img19.Width; x++)
            {

                if (img19.GetPixel(x, y).GetBrightness() > average)
                {
                    img19.SetPixel(x, y, Color.White);
                }
                else
                {
                    img19.SetPixel(x, y, Color.Black);

                }

            }
        }

Edit2:这是我的整个代码..

        float brightness = 0;
        float average = 0;

        PixelUtil pixelUtil2 = new PixelUtil((Bitmap)imgPristroj.Clone());
        pixelUtil2.LockBits();

        for (int y = 0; y < imgPristroj.Height; y++)
        {
            // for each pixel
            for (int x = 0; x < imgPristroj.Width; x++)
            {
                brightness += pixelUtil2.GetPixel(x, y).GetBrightness();
            }
        }
        average = brightness / (imgPristroj.Width * imgPristroj.Height);

        pixelUtil2.UnlockBits();

        img19 = (Bitmap)imgPristroj.Clone();
        Crop cfilter1 = new Crop(new Rectangle(0, (int)(pix * 1.6), img19.Width, (int)(pix * 3)));
        img19 = cfilter1.Apply(img19);

        PixelUtil pixelUtil = new PixelUtil(img19);
        pixelUtil.LockBits();


        for (int y = 0; y < img19.Height; y++)
        {
            for (int x = 0; x < img19.Width; x++)
            {

                if (pixelUtil.GetPixel(x, y).GetBrightness() >= average)
                {
                    pixelUtil.SetPixel(x, y, Color.White);
                }
                else
                {
                    pixelUtil.SetPixel(x, y, Color.Black);
                }

            }
        }

        pixelUtil.UnlockBits();

        string filepath = Environment.CurrentDirectory;
        string fileName = System.IO.Path.Combine(filepath, @"img" + ".bmp");
        img19.Save(fileName);

 我用彩色像素得到这个位图,你能告诉我为什么吗?

当我使用红色...而不是黑色...(pixelUtil.SetPixel(x,y,Color.Red);)我有这个有趣的照片。 (不同大小可以..)

2 个答案:

答案 0 :(得分:1)

你可以尝试使用编组:它非常快。 (你只需要复制粘贴它)

public class PixelUtil
{
    Bitmap source = null;
    IntPtr Iptr = IntPtr.Zero;
    BitmapData bitmapData = null;

    public byte[] Pixels { get; set; }
    public int Depth { get; private set; }
    public int Width { get; private set; }
    public int Height { get; private set; }

    /// <summary>
    /// Pixel marshaling class, use this to get and set pixels rapidly.
    /// </summary>
    /// <param name="source">The Bitmap to work with</param>
    public PixelUtil(Bitmap source)
    {
        this.source = source;
    }

    /// <summary>
    /// Lock bitmap data
    /// </summary>
    public void LockBits()
    {
        try
        {
            // Get width and height of bitmap
            Width = source.Width;
            Height = source.Height;

            // get total locked pixels count
            int PixelCount = Width * Height;

            // Create rectangle to lock
            var rect = new Rectangle(0, 0, Width, Height);

            // get source bitmap pixel format size
            Depth = System.Drawing.Image.GetPixelFormatSize(source.PixelFormat);

            // Check if bpp (Bits Per Pixel) is 8, 24, or 32
            if (Depth != 8 && Depth != 24 && Depth != 32)
            {
                throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
            }

            // Lock bitmap and return bitmap data
            bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
                                         source.PixelFormat);

            // create byte array to copy pixel values
            int step = Depth / 8;
            Pixels = new byte[PixelCount * step];
            Iptr = bitmapData.Scan0;

            // Copy data from pointer to array
            Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <summary>
    /// Unlock bitmap data
    /// </summary>
    public void UnlockBits()
    {
        try
        {
            // Copy data from byte array to pointer
            Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);

            // Unlock bitmap data
            source.UnlockBits(bitmapData);
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <summary>
    /// Get the color of the specified pixel
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <returns></returns>
    public Color GetPixel(int x, int y)
    {
        Color clr = Color.Empty;

        // Get color components count
        int cCount = Depth / 8;

        // Get start index of the specified pixel
        int i = ((y * Width) + x) * cCount;

        if (i > Pixels.Length - cCount)
            throw new IndexOutOfRangeException();

        if (Depth == 32) //For 32 bpp get Red, Green, Blue and Alpha
        {
            byte b = Pixels[i];
            byte g = Pixels[i + 1];
            byte r = Pixels[i + 2];
            byte a = Pixels[i + 3]; // a
            clr = Color.FromArgb(a, r, g, b);
        }
        if (Depth == 24) //For 24 bpp get Red, Green and Blue
        {
            byte b = Pixels[i];
            byte g = Pixels[i + 1];
            byte r = Pixels[i + 2];
            clr = Color.FromArgb(r, g, b);
        }
        if (Depth == 8) //For 8 bpp get color value (Red, Green and Blue values are the same)
        {
            byte c = Pixels[i];
            clr = Color.FromArgb(c, c, c);
        }
        return clr;
    }

    /// <summary>
    /// Set the color of the specified pixel
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="color"></param>
    public void SetPixel(int x, int y, Color color)
    {
        //Get color components count
        int cCount = Depth / 8;

        //Get start index of the specified pixel
        int i = ((y * Width) + x) * cCount;

        if (Depth == 32) //For 32 bpp set Red, Green, Blue and Alpha
        {
            Pixels[i] = color.B;
            Pixels[i + 1] = color.G;
            Pixels[i + 2] = color.R;
            Pixels[i + 3] = color.A;
        }
        if (Depth == 24) //For 24 bpp set Red, Green and Blue
        {
            Pixels[i] = color.B;
            Pixels[i + 1] = color.G;
            Pixels[i + 2] = color.R;
        }
        if (Depth == 8) //For 8 bpp set color value (Red, Green and Blue values are the same)
        {
            Pixels[i] = color.B;
        }
    }
}

您可以这样打电话:

public void Example(Bitmap image)
{
  PixelUtil pixelUtil = new PixelUtil(image);
  pixelUtil.LockBits();

  Color firstPixel = pixelUtil.GetPixel(0, 0);

  pixelUtil.SetPixel(0, 0, Color.White);

  //Don't forget to unlock!
  pixelUtil.UnlockBits();
}

编辑:

不确定如何保存图像,但它对我来说很好用:

    public Form1()
    {
        InitializeComponent();

        Bitmap image1 = new Bitmap(@"C:\Users\Nicke Manarin\Desktop\YEeJO.png");

        BlackWhite(image1);
    }

    public void BlackWhite(Bitmap image)
    {
        PixelUtil pixelUtil = new PixelUtil(image);
        pixelUtil.LockBits();

        for (int y = 0; y < image.Height; y++)
        {
            for (int x = 0; x < image.Width; x++)
            {
                if (pixelUtil.GetPixel(x, y).GetBrightness() > 0.5)
                {
                    pixelUtil.SetPixel(x, y, Color.White);
                }
                else
                {
                    pixelUtil.SetPixel(x, y, Color.Black);
                }
            }
        }

        pixelUtil.UnlockBits();

        pictureBox1.Image = image;
        image.Save(@"C:\Users\Nicke Manarin\Desktop\YEeJO2.png");
    }

enter image description here

答案 1 :(得分:0)

我使用此代码,现在它的工作正常......再次发送!!!!

  public class PointBitmap
        {
            Bitmap source = null;
            IntPtr Iptr = IntPtr.Zero;
            BitmapData bitmapData = null;

            public int Depth { get; private set; }
            public int Width { get; private set; }
            public int Height { get; private set; }

            public PointBitmap(Bitmap source)
            {
                this.source = source;
            }

            public void LockBits()
            {
                try
                {
                    // Get width and height of bitmap
                    Width = source.Width;
                    Height = source.Height;

                    // get total locked pixels count
                    int PixelCount = Width * Height;

                    // Create rectangle to lock
                    Rectangle rect = new Rectangle(0, 0, Width, Height);

                    // get source bitmap pixel format size
                    Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);

                    // Check if bpp (Bits Per Pixel) is 8, 24, or 32
                    if (Depth != 8 && Depth != 24 && Depth != 32)
                    {
                        throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
                    }

                    // Lock bitmap and return bitmap data
                    bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
                                                 source.PixelFormat);

                    unsafe
                    {
                        Iptr = bitmapData.Scan0;

                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public void UnlockBits()
            {
                try
                {
                    source.UnlockBits(bitmapData);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public Color GetPixel(int x, int y)
            {
                unsafe
                {
                    byte* ptr = (byte*)Iptr;
                    ptr = ptr + bitmapData.Stride * y;
                    ptr += Depth * x / 8;
                    Color c = Color.Empty;
                    if (Depth == 32)
                    {
                        int a = ptr[3];
                        int r = ptr[2];
                        int g = ptr[1];
                        int b = ptr[0];
                        c = Color.FromArgb(a, r, g, b);
                    }
                    else if (Depth == 24)
                    {
                        int r = ptr[2];
                        int g = ptr[1];
                        int b = ptr[0];
                        c = Color.FromArgb(r, g, b);
                    }
                    else if (Depth == 8)
                    {
                        int r = ptr[0];
                        c = Color.FromArgb(r, r, r);
                    }
                    return c;
                }
            }

            public void SetPixel(int x, int y, Color c)
            {
                unsafe
                {
                    byte* ptr = (byte*)Iptr;
                    ptr = ptr + bitmapData.Stride * y;
                    ptr += Depth * x / 8;
                    if (Depth == 32)
                    {
                        ptr[3] = c.A;
                        ptr[2] = c.R;
                        ptr[1] = c.G;
                        ptr[0] = c.B;
                    }
                    else if (Depth == 24)
                    {
                        ptr[2] = c.R;
                        ptr[1] = c.G;
                        ptr[0] = c.B;
                    }
                    else if (Depth == 8)
                    {
                        ptr[2] = c.R;
                        ptr[1] = c.G;
                        ptr[0] = c.B;
                    }
                }
            }
        }

我认为这里唯一不同的是不安全修饰符