Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更改比较两幅图像的方法以加快速度?_C#_Winforms - Fatal编程技术网

C# 如何更改比较两幅图像的方法以加快速度?

C# 如何更改比较两幅图像的方法以加快速度?,c#,winforms,C#,Winforms,这是我今天使用的方法: public static Bitmap CloudsOnly(Bitmap bitmapwithclouds, Bitmap bitmapwithoutclouds) { Color color; Color backgroundColor = Color.Black; CreateErrorsArray(bitmapwithclouds, bitmapwithoutclouds)

这是我今天使用的方法:

public static Bitmap CloudsOnly(Bitmap bitmapwithclouds, Bitmap bitmapwithoutclouds)
        {
            Color color;
            Color backgroundColor = Color.Black;

            CreateErrorsArray(bitmapwithclouds, bitmapwithoutclouds);

            int tolerance = tolerancenum * tolerancenumeric + tolerancenumeric * tolerancenumeric + tolerancenumeric * tolerancenumeric;
            Bitmap newbitmap = new Bitmap(512, 512);
            for (int x = 0; x < bitmapwithclouds.Width; x++)
            {
                for (int y = 0; y < bitmapwithclouds.Height; y++)
                {
                    int error = errorsArray[x, y];

                    if (error < tolerance)
                    {
                        color = backgroundColor;
                    }
                    else
                    {
                        color = bitmapwithclouds.GetPixel(x, y);
                    }
                    newbitmap.SetPixel(x, y, color);
                }
            }
            newbitmap.Save(@"d:\test\newbitmap.jpg");
            return newbitmap;
        }
publicstaticbitmap-CloudsOnly(位图位图位图带云,位图位图位图不带云)
{
颜色;
颜色背景颜色=颜色。黑色;
CreateErrorsArray(bitmapwithclouds,bitmapwithoutclouds);
整数公差=公差数值*公差数值+公差数值*公差数值+公差数值*公差数值+公差数值*公差数值;
位图newbitmap=新位图(512);
对于(int x=0;x
但是GetPixel很慢,我在google上搜索了一些使用锁位的方法,例如:

private bool CompareBitmaps(Image left, Image right)
        {
            if (object.Equals(left, right))
                return true;
            if (left == null || right == null)
                return false;
            if (!left.Size.Equals(right.Size) || !left.PixelFormat.Equals(right.PixelFormat))
                return false;

            Bitmap leftBitmap = left as Bitmap;
            Bitmap rightBitmap = right as Bitmap;
            if (leftBitmap == null || rightBitmap == null)
                return true;

            #region Optimized code for performance

            int bytes = left.Width * left.Height * (Image.GetPixelFormatSize(left.PixelFormat) / 8);

            bool result = true;
            byte[] b1bytes = new byte[bytes];
            byte[] b2bytes = new byte[bytes];

            BitmapData bmd1 = leftBitmap.LockBits(new Rectangle(0, 0, leftBitmap.Width - 1, leftBitmap.Height - 1), ImageLockMode.ReadOnly, leftBitmap.PixelFormat);
            BitmapData bmd2 = rightBitmap.LockBits(new Rectangle(0, 0, rightBitmap.Width - 1, rightBitmap.Height - 1), ImageLockMode.ReadOnly, rightBitmap.PixelFormat);

            Marshal.Copy(bmd1.Scan0, b1bytes, 0, bytes);
            Marshal.Copy(bmd2.Scan0, b2bytes, 0, bytes);

            for (int n = 0; n <= bytes - 1; n++)
            {
                if (b1bytes[n] != b2bytes[n])
                {
                    result = false;
                    break;
                }
            }

            leftBitmap.UnlockBits(bmd1);
            rightBitmap.UnlockBits(bmd2);

            #endregion

            return result;
        }
private bool comparebitmap(左图、右图)
{
if(object.Equals(左、右))
返回true;
如果(左==null | |右==null)
返回false;
如果(!left.Size.Equals(right.Size)| |!left.PixelFormat.Equals(right.PixelFormat))
返回false;
位图leftBitmap=左为位图;
位图rightBitmap=右侧为位图;
if(左位图==null | |右位图==null)
返回true;
#针对性能的区域优化代码
int bytes=left.Width*left.Height*(Image.GetPixelFormatSize(left.PixelFormat)/8);
布尔结果=真;
字节[]b1bytes=新字节[字节];
字节[]b2bytes=新字节[字节];
BitmapData bmd1=leftBitmap.LockBits(新矩形(0,0,leftBitmap.Width-1,leftBitmap.Height-1),ImageLockMode.ReadOnly,leftBitmap.PixelFormat);
BitmapData bmd2=rightBitmap.LockBits(新矩形(0,0,rightBitmap.Width-1,rightBitmap.Height-1),ImageLockMode.ReadOnly,rightBitmap.PixelFormat);
封送处理副本(bmd1.Scan0,b1bytes,0,bytes);
Marshal.Copy(bmd2.Scan0,b2bytes,0,bytes);

因为(int n=0;nGetPixel的速度非常慢,如果你有多个像素,并且你不想看着代码在图片中爬行度过童年,就不要使用它

您必须使用不安全代码(非托管)才能快速访问图片。请参阅本文以获取帮助:


性能比较:

我可以推荐一种简单的优化方法。 您可以使用哈希函数,而不是手动比较结果数组中的每个字节。这意味着下一个代码:

 for (int n = 0; n <= bytes - 1; n++)
            {
                if (b1bytes[n] != b2bytes[n])
                {
                    result = false;
                    break;
                }
            }

内置哈希函数经过了很好的优化。

使用System.Drawing.Imaging.ImageAttributes类和System.Drawing.Imaging.ColorMap类是否可以更快地完成此操作?只是在黑暗中拍摄。如果您解释了您的方法应该做什么,那就太酷了。
锁位的使用应该是您想要的寻找,如果它不起作用,那是因为你的代码中有错误,
锁位
是我们在.NET中处理图像的常用方法,它比GetPixel和SetPixel快得多,它们只能在你只需要获取/设置像素一次或多次时使用。我会避免使用
封送.复制
和instead编写一些直接访问位图数据的不安全代码。Lasse你能根据我的代码向我展示一个代码吗?我必须同意。对于这样的大型迭代函数,指针的速度非常快。
var sha1 = new SHA1CryptoServiceProvider();
return sha1.ComputeHash(b1bytes) == sha1.ComputeHash(b2bytes);