static void Compare(string bmp1, string bmp2, byte threshold = 5) { //get the full path of the images string image1Path = Path.Combine(sampleImagesFolder.FullName, bmp1); string image2Path = Path.Combine(sampleImagesFolder.FullName, bmp2); //compare the two Console.Write("Comparing: " + bmp1 + " and " + bmp2 + ", with a threshold of " + threshold); Bitmap firstBmp = (Bitmap)Image.FromFile(image1Path); Bitmap secondBmp = (Bitmap)Image.FromFile(image2Path); //get the difference as a bitmap firstBmp.GetDifferenceImage(secondBmp,true) .Save(image1Path + "_diff.png"); Console.WriteLine(string.Format("Difference: {0:0.0} %", firstBmp.PercentageDifference(secondBmp, threshold) * 100)); //Console.WriteLine("ENTER see histogram for " + bmp1); //Console.ReadLine(); Console.WriteLine(); Console.WriteLine("Creating histogram for " + bmp1); Histogram hist = new Histogram(firstBmp); // hist.Visualize().Save(image1Path + "_hist.png"); Console.WriteLine(hist.ToString()); Console.WriteLine("ENTER to continue..."); Console.ReadLine(); }
/// <summary> /// Gets the variance between two histograms (http://en.wikipedia.org/wiki/Variance) as a percentage of the maximum possible variance: 256 (for a white image compared to a black image) /// </summary> /// <param name="histogram">the histogram to compare this one to</param> /// <returns>A percentage which tells how different the two histograms are</returns> public float GetVariance(Histogram histogram) { // double diffRed = 0, diffGreen = 0, diffBlue = 0; for (int i = 0; i < 256; i++) { diffRed += Math.Pow(Red[i] - histogram.Red[i], 2); diffGreen += Math.Pow(Green[i] - histogram.Green[i], 2); diffBlue += Math.Pow(Blue[i] - histogram.Blue[i], 2); } diffRed /= 256; diffGreen /= 256; diffBlue /= 256; const double maxDiff = 512; return (float)(diffRed / maxDiff + diffGreen / maxDiff + diffBlue / maxDiff) / 3; }