开发者

unsafe image noise removal in c# (error : Bitmap region is already locked)

开发者 https://www.devze.com 2023-03-04 21:31 出处:网络
public unsafe BitmapMedianFilter(Bitmap Img) { int Size =2; List<byte> R = new List<byte>();
public unsafe Bitmap  MedianFilter(Bitmap Img)
     {
        int Size =2;

        List<byte> R = new List<byte>();
        List<byte> G = new List<byte>();
        List<byte> B = new List<byte>();

        int ApetureMin = -(Size / 2);
        int ApetureMax = (Size / 2);

        BitmapData imageData = Img.LockBits(new Rectangle(0, 0, Img.Width, Img.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);

        byte* start = (byte*)imageData.Scan0.ToPointer ();
        for (int x = 0; x < imageData.Width; x++)
        {
            for (int y = 0; y < imageData.Height; y++)
            {
                for (int x1 = ApetureMin; x1 < ApetureMax; x1++)
                {
                    int v开发者_开发百科alx = x + x1;
                    if (valx >= 0 && valx < imageData.Width)
                    {
                        for (int y1 = ApetureMin; y1 < ApetureMax; y1++)
                        {
                            int valy = y + y1;
                            if (valy >= 0 && valy < imageData.Height)
                            {
                                Color tempColor = Img.GetPixel(valx, valy);// error come from here
                                R.Add(tempColor.R);
                                G.Add(tempColor.G);
                                B.Add(tempColor.B);
                            }
                        }
                    }
                }
            }
        }

        R.Sort();
        G.Sort();
        B.Sort();

        Img.UnlockBits(imageData);
        return Img;
     }

I tried to do this. but i got an error call "Bitmap region is already locked" can anyone help how to solve this. (error position is highlighted)


GetPixel is the slooow way to access the image and doesn't work (as you noticed) anymore if someone else starts messing with the image buffer directly. Why would you want to do that?

Check Using the LockBits method to access image data for some good insight into fast image manipulation.

In this case, use something like this instead:

int pixelSize = 4 /* Check below or the site I linked to and make sure this is correct */
byte* color =(byte *)imageData .Scan0+(y*imageData .Stride) + x * pixelSize;

Note that this gives you the first byte for that pixel. Depending on the color format you are looking at (ARGB? RGB? ..) you need to access the following bytes as well. Seems to suite your usecase anyway, since you just care about byte values, not the Color value.


So, after having some spare minutes, this is what I'd came up with (please take your time to understand and check it, I just made sure it compiles):

    public void SomeStuff(Bitmap image)
    {
        var imageWidth = image.Width;
        var imageHeight = image.Height;
        var imageData = image.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);

        var imageByteCount = imageData.Stride*imageData.Height;
        var imageBuffer = new byte[imageByteCount];
        Marshal.Copy(imageData.Scan0, imageBuffer, 0, imageByteCount);

        for (int x = 0; x < imageWidth; x++)
        {
            for (int y = 0; y < imageHeight; y++)
            {
                var pixelColor = GetPixel(imageBuffer, imageData.Stride, x, y);

                // Do your stuff
            }
        }
    }

    private static Color GetPixel(byte[] imageBuffer, int imageStride, int x, int y)
    {

        int pixelBase = y*imageStride + x*3;
        byte blue = imageBuffer[pixelBase];
        byte green = imageBuffer[pixelBase + 1];
        byte red = imageBuffer[pixelBase + 2];

        return Color.FromArgb(red, green, blue);
    }

This

  • Relies on the PixelFormat you used in your sample (regarding both the pixelsize/bytes per pixel and the order of the values). If you change the PixelFormat this will break.
  • Doesn't need the unsafe keyword. I doubt that it makes a lot of difference, but you are free to use the pointer based access instead, the method would be the same.
0

精彩评论

暂无评论...
验证码 换一张
取 消