无法将屏幕截图与图像进行比较

本文关键字:比较 图像 屏幕截图 | 更新日期: 2023-09-27 17:56:32

我正在Windows Forms中处理我的项目,我有一个问题。在这个项目中,我必须将屏幕截图与文件中的图像进行比较。我有很好的 在互联网上进行比较的方法 找到它似乎正在工作。例如:我从网站上剪切了Facebook徽标并将其保存在桌面上。当我将此徽标与自身进行比较时(我正在制作徽标的屏幕截图,然后将此屏幕截图与徽标进行比较)此方法工作正常(它说屏幕截图包含徽标),但是当我在其网站上制作屏幕截图并将其与徽标进行比较时,此方法说屏幕截图不包含徽标。

我正在使用这种比较方法:

public static Rectangle searchBitmap(Bitmap smallBmp, Bitmap bigBmp, double tolerance)
{
    BitmapData smallData =
        smallBmp.LockBits(new Rectangle(0, 0, smallBmp.Width, smallBmp.Height),
            System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    BitmapData bigData =
    bigBmp.LockBits(new Rectangle(0, 0, bigBmp.Width, bigBmp.Height),
            System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    int smallStride = smallData.Stride;
    int bigStride = bigData.Stride;
    int bigWidth = bigBmp.Width;
    int bigHeight = bigBmp.Height - smallBmp.Height + 1;
    int smallWidth = smallBmp.Width * 3;
    int smallHeight = smallBmp.Height;
    Rectangle location = Rectangle.Empty;
    int margin = Convert.ToInt32(255.0 * tolerance);
    unsafe
    {
        byte* pSmall = (byte*)(void*)smallData.Scan0;
        byte* pBig = (byte*)(void*)bigData.Scan0;
        int smallOffset = smallStride - smallBmp.Width * 3;
        int bigOffset = bigStride - bigBmp.Width * 3;
        bool matchFound = true;
        for (int y = 0; y < bigHeight; y++)
        {
            for (int x = 0; x < bigWidth; x++)
            {
                byte* pBigBackup = pBig;
                byte* pSmallBackup = pSmall;
                //Look for the small picture.
                for (int i = 0; i < smallHeight; i++)
                {
                    int j = 0;
                    matchFound = true;
                    for (j = 0; j < smallWidth; j++)
                    {
                        //With tolerance: pSmall value should be between margins.
                        int inf = pBig[0] - margin;
                        int sup = pBig[0] + margin;
                        if (sup < pSmall[0] || inf > pSmall[0])
                        {
                            matchFound = false;
                            break;
                        }
                        pBig++;
                        pSmall++;
                    }
                    if (!matchFound) break;
                    //We restore the pointers.
                    pSmall = pSmallBackup;
                    pBig = pBigBackup;
                    //Next rows of the small and big pictures.
                    pSmall += smallStride * (1 + i);
                    pBig += bigStride * (1 + i);
                }
                //If match found, we return.
                if (matchFound)
                {
                    location.X = x;
                    location.Y = y;
                    location.Width = smallBmp.Width;
                    location.Height = smallBmp.Height;
                    break;
                }
                //If no match found, we restore the pointers and continue.
                else
                {
                    pBig = pBigBackup;
                    pSmall = pSmallBackup;
                    pBig += 3;
                }
            }
            if (matchFound) break;
            pBig += bigOffset;
        }
    }
    bigBmp.UnlockBits(bigData);
    smallBmp.UnlockBits(smallData);
    return location;
}

此方法返回一个矩形"位置"。如果(location.Width == 0 || location.height == 0),则表示屏幕截图不包含图像。怎么了?

无法将屏幕截图与图像进行比较

如果屏幕截图在边框周围包含任何空白区域,则很可能与图像不匹配(除非屏幕截图是整个屏幕)。