2

2 つのビットマップをすばやく簡単に比較する方法を探していて、次の 2 つの方法を思いつきました。

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);
}

私が求めているのは、これらがビットマップを比較する有効な方法であるかどうかです。これらを使用する際に直面する可能性のある問題はありますか? そして、これを行うためのより効率的な方法はありますか?

4

0 に答える 0