|
| 1 | +using Aquality.Selenium.Core.Configurations; |
| 2 | +using System; |
| 3 | +using System.Drawing; |
| 4 | + |
| 5 | +namespace Aquality.Selenium.Core.Visualization |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Compares images with defined threshold. |
| 9 | + /// Uses resizing and gray-scaling to simplify comparison. |
| 10 | + /// Uses parameters from <see cref="IVisualizationConfiguration"/>. |
| 11 | + /// Special thanks to https://www.codeproject.com/Articles/374386/Simple-image-comparison-in-NET |
| 12 | + /// </summary> |
| 13 | + public class ImageComparator : IImageComparator |
| 14 | + { |
| 15 | + private const int ThresholdDivisor = 255; |
| 16 | + private readonly IVisualizationConfiguration visualizationConfiguration; |
| 17 | + |
| 18 | + public ImageComparator(IVisualizationConfiguration visualizationConfiguration) |
| 19 | + { |
| 20 | + this.visualizationConfiguration = visualizationConfiguration; |
| 21 | + } |
| 22 | + |
| 23 | + private float DefaultThreshold => visualizationConfiguration.DefaultThreshold; |
| 24 | + private int ComparisonHeight => visualizationConfiguration.ComparisonHeight; |
| 25 | + private int ComparisonWidth => visualizationConfiguration.ComparisonWidth; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Gets the difference between two images as a percentage |
| 29 | + /// </summary> |
| 30 | + /// <param name="thisOne">The first image</param> |
| 31 | + /// <param name="theOtherOne">The image to compare with</param> |
| 32 | + /// <param name="threshold">How big a difference will be ignored as a percentage - value between 0 and 1. |
| 33 | + /// If the value is null, the default value is got from <see cref="IVisualizationConfiguration"/>.</param> |
| 34 | + /// <returns>The difference between the two images as a percentage - value between 0 and 1.</returns> |
| 35 | + /// <remarks>See https://web.archive.org/web/20130208001434/http://tech.pro:80/tutorial/660/csharp-tutorial-convert-a-color-image-to-grayscale for more details.</remarks> |
| 36 | + public virtual float PercentageDifference(Image thisOne, Image theOtherOne, float? threshold = null) |
| 37 | + { |
| 38 | + var thresholdValue = threshold ?? DefaultThreshold; |
| 39 | + if (thresholdValue < 0 || thresholdValue > 1) |
| 40 | + { |
| 41 | + throw new ArgumentOutOfRangeException(nameof(threshold), thresholdValue, "Threshold should be between 0 and 1"); |
| 42 | + } |
| 43 | + |
| 44 | + var byteThreshold = Convert.ToByte(thresholdValue * ThresholdDivisor); |
| 45 | + return PercentageDifference(thisOne, theOtherOne, byteThreshold); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Gets the difference between two images as a percentage |
| 50 | + /// </summary> |
| 51 | + /// <param name="thisOne">The first image</param> |
| 52 | + /// <param name="theOtherOne">The image to compare with</param> |
| 53 | + /// <param name="threshold">How big a difference (out of 255) will be ignored - the default is 3.</param> |
| 54 | + /// <returns>The difference between the two images as a percentage</returns> |
| 55 | + /// <remarks>See https://web.archive.org/web/20130208001434/http://tech.pro:80/tutorial/660/csharp-tutorial-convert-a-color-image-to-grayscale for more details</remarks> |
| 56 | + protected virtual float PercentageDifference(Image thisOne, Image theOtherOne, byte threshold = 3) |
| 57 | + { |
| 58 | + var differences = GetDifferences(thisOne, theOtherOne); |
| 59 | + |
| 60 | + int diffPixels = 0; |
| 61 | + |
| 62 | + foreach (byte b in differences) |
| 63 | + { |
| 64 | + if (b > threshold) { diffPixels++; } |
| 65 | + } |
| 66 | + |
| 67 | + return diffPixels / ((float)ComparisonWidth * ComparisonHeight); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Finds the differences between two images and returns them in a double-array |
| 72 | + /// </summary> |
| 73 | + /// <param name="thisOne">The first image</param> |
| 74 | + /// <param name="theOtherOne">The image to compare with</param> |
| 75 | + /// <returns>the differences between the two images as a double-array</returns> |
| 76 | + protected virtual byte[,] GetDifferences(Image thisOne, Image theOtherOne) |
| 77 | + { |
| 78 | + var firstGray = GetResizedGrayScaleValues(thisOne); |
| 79 | + var secondGray = GetResizedGrayScaleValues(theOtherOne); |
| 80 | + |
| 81 | + var differences = new byte[ComparisonWidth, ComparisonHeight]; |
| 82 | + for (int y = 0; y < ComparisonHeight; y++) |
| 83 | + { |
| 84 | + for (int x = 0; x < ComparisonWidth; x++) |
| 85 | + { |
| 86 | + differences[x, y] = (byte)Math.Abs(firstGray[x, y] - secondGray[x, y]); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return differences; |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Gets the lightness of the image in sections (by default 256 sections, 16x16) |
| 95 | + /// </summary> |
| 96 | + /// <param name="img">The image to get the lightness for</param> |
| 97 | + /// <returns>A double-array (16x16 by default) containing the lightness of the sections(256 by default)</returns> |
| 98 | + protected virtual byte[,] GetResizedGrayScaleValues(Image img) |
| 99 | + { |
| 100 | + using (var thisOne = (Bitmap)img.Resize(ComparisonWidth, ComparisonHeight).GetGrayScaleVersion()) |
| 101 | + { |
| 102 | + byte[,] grayScale = new byte[thisOne.Width, thisOne.Height]; |
| 103 | + |
| 104 | + for (int y = 0; y < thisOne.Height; y++) |
| 105 | + { |
| 106 | + for (int x = 0; x < thisOne.Width; x++) |
| 107 | + { |
| 108 | + grayScale[x, y] = (byte)Math.Abs(thisOne.GetPixel(x, y).R); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return grayScale; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments