Percentage difference between images: Difference between revisions

Content added Content deleted
(→‎{{header|Perl}}: added an alternative solution using the Imager library)
(→‎{{header|C#}}: GetPixel is an expensive call, optimized to minimize calls (Code is now 2.8x faster by BenchmarkDotNet test). Also optimized to remove repeated floating point rounding errors. .Net's Math.Abs is optimized, repeated calls can be left.)
Line 337: Line 337:
for (int x = 0; x < img1.Width; x++)
for (int x = 0; x < img1.Width; x++)
{
{
diff += (float)Math.Abs(img1.GetPixel(x, y).R - img2.GetPixel(x, y).R) / 255;
Color pixel1 = img1.GetPixel(x, y);
diff += (float)Math.Abs(img1.GetPixel(x, y).G - img2.GetPixel(x, y).G) / 255;
Color pixel2 = img2.GetPixel(x, y);

diff += (float)Math.Abs(img1.GetPixel(x, y).B - img2.GetPixel(x, y).B) / 255;
diff += Math.Abs(pixel1.R - pixel2.R);
diff += Math.Abs(pixel1.G - pixel2.G);
diff += Math.Abs(pixel1.B - pixel2.B);
}
}
}
}


Console.WriteLine("diff: {0} %", 100 * diff / (img1.Width * img1.Height * 3));
Console.WriteLine("diff: {0} %", 100 * (diff / 255) / (img1.Width * img1.Height * 3));
}
}
}</lang>
}</lang>