我一直在尝试找到一种方法来快速轻松地比较两个位图,我想出了两种方法:
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);
}
我要问的是这些是否是比较位图的有效方法?使用这些时我会遇到什么问题吗?有没有更有效的方法来做到这一点?