C#中不安全的图像噪声去除(错误:位图区域已被锁定) [英] unsafe image noise removal in c# (error : Bitmap region is already locked)

查看:594
本文介绍了C#中不安全的图像噪声去除(错误:位图区域已被锁定)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 valx = 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)
                        {
                            // error come from here
                            Color tempColor = Img.GetPixel(valx, valy);

                            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.不安全方法的全部要点是绕过缓慢的托管API.您应该使用* scan ++(如果您正在从所选的矩形中读取整行,看起来像是这样).

至于错误,我猜Get/SetPixel在内部锁定了图像(可能是它们如此缓慢的部分原因),并且由于您自己(正确)锁定了图像,因此失败了.但是您的问题是,您将安全,缓慢的方式与不安全,快速的方式混为一谈.
If you''re going to use the unsafe approach, don''t use GetPixel. The whole point of the unsafe approach is to bypass the slow managed API. You should be using *scan++ (if you are reading whole rows from the rectangle you selected, which you seem to be).

As for the error, I guess Get/SetPixel lock the image internally (possibly a part of why they''re so slow), and since you are (correctly) locking it yourself, that fails. But your problem is that you have a horrible mashing together of the safe, slow way and the unsafe, fast way.


BobJanova是正确的.

GetPixelSetPixel在内部调用LockBitsUnlockBits.这就是为什么这些功能如此缓慢并且不应该用于图像处理的原因.
单击此链接以了解如何使用LockBits和访问内部数据:
http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx [ ^ ]
BobJanova is right.

GetPixel and SetPixel call LockBits and UnlockBits internally. This is why these functions are so slow and shouldn''t be used for image processing.

Follow this link to learn how to use LockBits and access internal data:
http://msdn.microsoft.com/en-us/library/5ey6h79d.aspx[^]


请勿使用GetPixel/SetPixel,除非您只需要一个或两个(这很少见)或准备降低性能.使用LockBits时,它足以访问单个像素.

—SA
Never use GetPixel/SetPixel unless you need just one or two (which is pretty rare) or ready to kill your performance. When you use LockBits, its more than enough to access individual pixels.

—SA


这篇关于C#中不安全的图像噪声去除(错误:位图区域已被锁定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆