比较位图的有效方法

时间:2013-10-31 02:07:27

标签: c# .net bitmap comparison compare

我一直试图找到一种方法在两个位图之间进行快速简单的比较,我想出了两种方法:

private static Bitmap Resize(Bitmap b, int nWidth, int nHeight)
{
    Bitmap result = new Bitmap(nWidth, nHeight);
    using (Graphics g = Graphics.FromImage((Image)result))
    {
        g.InterpolationMode = InterpolationMode.NearestNeighbor;
        g.DrawImage(b, 0, 0, nWidth, nHeight);
    }
    return result;
}

public static Decimal CompareImages(Bitmap b1, Bitmap b2, int Approx = 64)
{
    Bitmap i1 = Resize(b1, Approx, Approx);
    Bitmap i2 = Resize(b2, Approx, Approx);
    int diff = 0;

    for (int row = 1; row < Approx; row++)
    {
        for (int col = 1; col < Approx; col++)
        {
            if (i1.GetPixel(col, row) != i2.GetPixel(col, row))
                diff++;
        }
    }

    i1.Dispose();
    i2.Dispose();

    return Math.Round((Decimal)(diff / (Approx ^ 2)), 2);
}

public static Decimal CompareImagesExact(Bitmap b1, Bitmap b2)
{
    int h = b1.Height > b2.Height ? b1.Height : b2.Height;
    int w = b1.Width > b2.Width ? b1.Width : b2.Width;
    int diff = 0;
    for (int row = 1; row < h; row++)
    {
        for (int col = 1; col < w; col++)
        {
            if (b1.GetPixel(col, row) != b2.GetPixel(col, row))
                diff++;
        }
    }

    return Math.Round((Decimal)(diff / ((h * w) ^ 2)), 2);
}

我问的是,这些是比较位图的有效方法吗?使用这些时我可能遇到任何问题吗?还有更有效的方法吗?

0 个答案:

没有答案