protected void CompareImages([CallerMemberName] string testName = "") { string expectedPath = Path.Combine(OutputPath, testName + ".expected.png"); string actualPath = Path.Combine(OutputPath, testName + ".out.png"); using (MagickImage expected = new MagickImage(expectedPath)) using (MagickImage actual = new MagickImage(actualPath)) { double error = expected.Compare(actual, ErrorMetric.RootMeanSquared); if (error > 0.022) { Assert.True(false, actualPath + ": Error = " + error); } } }
protected void CompareImagesNoRenderer([CallerMemberName] string testName = "") { var expectedPath = Path.Combine(OutputPath, testName + ".expected.png"); var actualPath = Path.Combine(OutputPath, testName + ".out.png"); using (var expected = new MagickImage(expectedPath)) using (var actual = new MagickImage(actualPath)) { double immediateError = expected.Compare(actual, ErrorMetric.RootMeanSquared); if (immediateError > 0.022) { Assert.True(false, actualPath + ": Error = " + immediateError); } } }
public void ShouldCreateDifferentImagesEachRun() { TestHelper.ExecuteInsideLock(() => { using (var imageA = new MagickImage(MagickColors.Black, 10, 10)) { using (var imageB = new MagickImage(MagickColors.Black, 10, 10)) { imageA.AddNoise(NoiseType.Random); imageB.AddNoise(NoiseType.Random); Assert.NotEqual(0.0, imageA.Compare(imageB, ErrorMetric.RootMeanSquared)); } } }); }
public void TestAlphaChannel() { using (var image = new MagickImage(TestUtils.LoadMagickImage("alpha_test.png"))) using (var dest = new MagickImage(image)) using (var emission = new MagickImage(MagickColors.Black, image.Width, image.Height)) { Assert.True(image.HasAlpha); dest.HasAlpha = false; dest.Composite(emission, CompositeOperator.Screen); dest.HasAlpha = true; dest.CopyPixels(image, Channels.Alpha); var result = image.Compare(dest, ErrorMetric.MeanErrorPerPixel, Channels.All); Assert.AreEqual(0.0, result); } }
public void CompareFile(String imageFileName) { var currentImage = new MagickImage(PathCombine(CurrentImageFolder, imageFileName)); var baselineImage = new MagickImage(PathCombine(BaselineImageFolder, imageFileName)); try { using (var diff = new MagickImage()) { currentImage.Compare(baselineImage, ErrorMetric.PeakAbsolute, diff); } } catch (Exception ex) { throw; } }
public void ShouldCombineCmykImage() { using (var cmyk = new MagickImage(Files.CMYKJPG)) { using (var collection = new MagickImageCollection()) { collection.AddRange(cmyk.Separate(Channels.CMYK)); Assert.Equal(4, collection.Count); using (var image = collection.Combine(ColorSpace.CMYK)) { Assert.Equal(0.0, cmyk.Compare(image, ErrorMetric.RootMeanSquared)); } } } }
private void Button_Click_3(object sender, RoutedEventArgs e) { try { //var diffImagePath = @"C:\software\ImageCompare\3.png"; using (MagickImage image1 = new MagickImage(fileName1)) using (MagickImage image2 = new MagickImage(fileName2)) using (MagickImage diffImage = new MagickImage()) { image1.Compare(image2, ErrorMetric.Absolute, diffImage); diffImage.Write(Environment.CurrentDirectory + "diff.png"); this.image3.Source = new BitmapImage(new Uri(Environment.CurrentDirectory + "diff.png", UriKind.RelativeOrAbsolute)); } } catch (Exception) { } }
public void ShouldUseTheColorFuzz() { using (var image = new MagickImage(new MagickColor("#f1d3bc"), 1, 1)) { using (var other = new MagickImage(new MagickColor("#24292e"), 1, 1)) { using (var diff = new MagickImage()) { image.ColorFuzz = new Percentage(81); double result = image.Compare(other, ErrorMetric.Absolute, diff); Assert.Equal(0, result); ColorAssert.Equal(new MagickColor("#fd2ff729f28b"), diff, 0, 0); } } } }
public void CreateDiffImage(string path1, string path2, string outputPath) { var directory = outputPath.Replace(Path.GetFileName(outputPath), ""); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } using (var image1 = new MagickImage(path1)) using (var image2 = new MagickImage(path2)) using (var imageOutput = new MagickImage()) { image2.Compare(image1, ErrorMetric.Fuzz, imageOutput); imageOutput.Write(outputPath); } }
private static IEnumerable <PageComparison> Compare(int numPages, string rootPath, string referencePath, string toTestPath, string comparePath) { for (var currentPage = 1; currentPage <= numPages; currentPage++) { var currentComparison = new PageComparison() { PageNumber = currentPage, RootPath = rootPath }; using (var referenceImage = new MagickImage()) using (var testImage = new MagickImage()) using (var compareImage = new MagickImage()) { var referenceImagePath = currentComparison.ReferencePagePath; if (File.Exists(referenceImagePath)) { referenceImage.Read(referenceImagePath); } var testImagePath = currentComparison.TestPagePath; if (File.Exists(testImagePath)) { testImage.Read(testImagePath); } InsertEmptyImageIfNecessary(referenceImage, testImage); InsertEmptyImageIfNecessary(testImage, referenceImage); EnsureImageIsColored(referenceImage); EnsureImageIsColored(testImage); var result = referenceImage.Compare(testImage, ErrorMetric.Absolute, compareImage, Channels.All); compareImage.Write(currentComparison.ComparePagePath); compareImage.Resize(ThumbnailGeometry); compareImage.Write(currentComparison.ComparePageThumbnailPath); currentComparison.ComparisonScore = result; currentComparison.PixelCount = referenceImage.Height * referenceImage.Width; } yield return(currentComparison); } }
public ImageComparisonInfo GetCompareImageDiff() { using (var firstImage = new MagickImage(ImageComparisonInfo.FirstImagePath)) using (var secondImage = new MagickImage(ImageComparisonInfo.SecondImagePath)) using (var diffImage = new MagickImage()) { ImageComparisonInfo.IsImagesDifferent = false; if (firstImage.GetHashCode() == secondImage.GetHashCode()) { return(ImageComparisonInfo); } firstImage.Compare(secondImage, ErrorMetric.Absolute, diffImage, Channels.Index); diffImage.Write(ImageComparisonInfo.DiffImagePath); return(ImageComparisonInfo); } }
private void btnImageCompare_Click(object sender, EventArgs e) { string orgImagePath = lblImage1Path.Text; string dupImagePath = lblImage2Path.Text; bool isFirstImageEmpty = string.IsNullOrWhiteSpace(orgImagePath); bool isSecondImageEmpty = string.IsNullOrWhiteSpace(dupImagePath); if (isFirstImageEmpty || isSecondImageEmpty) { MessageBox.Show("Please select images before compare"); } bool ispHashSelected = cbCompareAlgorithm.Text == "pHash"; bool isBhattacharyyaSelected = cbCompareAlgorithm.Text == "Bhattacharyya"; if (ispHashSelected) { var percentage = ImageHashing.Similarity(orgImagePath, dupImagePath); percentage = Math.Round(percentage, 3); txtPerDiff.Text = percentage.ToString(); } else if (isBhattacharyyaSelected) { Image img1 = Image.FromFile(orgImagePath); Image img2 = Image.FromFile(dupImagePath); var percentage = Bhattacharyya.BhattacharyyaDifference(img1, img2); percentage = percentage * 100; percentage = Math.Round(percentage, 3); txtPerDiff.Text = percentage.ToString(); } else { int imageAlgoIndex = cbCompareAlgorithm.SelectedIndex; ErrorMetric imageAlgo = (ErrorMetric)imageAlgoIndex; MagickImage orgImage = new MagickImage(orgImagePath); MagickImage dupImage = new MagickImage(dupImagePath); var percentage = orgImage.Compare(dupImage, imageAlgo); txtPerDiff.Text = percentage.ToString(); orgImage.Dispose(); dupImage.Dispose(); } }
public double saveResult(MagickImage StandartIM, MagickImage CurrentIM, string outFilePath, string outCurFilePath) { MagickImage resultImage = new MagickImage(); var compareResult = StandartIM.Compare(CurrentIM, ErrorMetric.Absolute, resultImage); if (compareResult != 0) { CreateScreenDirectory(Path.GetDirectoryName(outFilePath)); CreateScreenDirectory(Path.GetDirectoryName(outCurFilePath)); resultImage.Write(outFilePath); CurrentIM.Write(outCurFilePath); Console.Out.WriteLine("Погрешность= " + compareResult + " Изображения не совпадают" + outFilePath); } else { Console.Out.WriteLine("Изображения совпадают"); } return(compareResult); }
protected void AssertOutput(IMagickImage image, string expectedOutput) { if (image == null) { throw new InvalidOperationException(); } var actualOutputFile = GetActualOutputFile(expectedOutput); if (!actualOutputFile.Directory.Exists) { actualOutputFile.Directory.Create(); } image.Write(actualOutputFile); var expectedOutputFile = GetExpectedOutputFile(expectedOutput); /* Compress the image that will be used as the expected output after it has been compared * to the result from Fred his script. */ if (!expectedOutputFile.Exists) { LosslessCompress(actualOutputFile.FullName); } using (var expectedImage = new MagickImage(expectedOutputFile)) { using (var actualImage = new MagickImage(actualOutputFile)) { Assert.AreEqual(expectedImage.Width, actualImage.Width, actualImage.FileName); Assert.AreEqual(expectedImage.Height, actualImage.Height, actualImage.FileName); var distortion = actualImage.Compare(expectedImage, ErrorMetric.RootMeanSquared); if (distortion != 0) { LosslessCompress(actualOutputFile.FullName); } Assert.AreEqual(0.0, distortion, actualImage.FileName); } } }
private static void CompareImages(MagickImage expectedImage, MagickImage imageToCompare, string deltaImagePath) { var fileName = imageToCompare.FileName.Replace(CurrentPath, ""); using (var delta = new MagickImage()) { var result = expectedImage.Compare(imageToCompare, ErrorMetric.Fuzz, delta); if (result > ThreashHold) { //delta.Write(deltaImagePath); Console.WriteLine($"Threshhold: {ThreashHold} compare result: {result} Does not match {fileName}."); } else { Console.WriteLine($"Threshhold: {ThreashHold} compare result: {result} Matched {fileName}"); } } }
public void ShouldReturnZeroWhenTheImagesAreEqual() { var settings = new CompareSettings { Metric = ErrorMetric.RootMeanSquared, }; using (var image = new MagickImage(Files.Builtin.Logo)) { using (var other = new MagickImage(Files.Builtin.Logo)) { using (var diff = new MagickImage()) { double result = image.Compare(other, settings, diff); Assert.Equal(0, result); } } } }
protected void CompareImages([CallerMemberName] string testName = "") { string expectedPath = Path.Combine(this.OutputPath, testName + ".expected.png"); string actualPath = Path.Combine(this.OutputPath, testName + ".out.png"); MagickImage expected = new MagickImage(expectedPath); MagickImage actual = new MagickImage(actualPath); MagickErrorInfo error = expected.Compare(actual); if (error.NormalizedMaximumError > 0.1) { if (error.NormalizedMaximumError > 0.15) { Assert.Fail("NormalizedMaximumError = " + error.NormalizedMaximumError); } else { Assert.Inconclusive("Close but no cigar. NormalizedMaximumError = " + error.NormalizedMaximumError); } } }
public void Test_RandomSeed() { using (IMagickImage first = new MagickImage("plasma:red", 10, 10)) { using (IMagickImage second = new MagickImage("plasma:red", 10, 10)) { Assert.AreNotEqual(0.0, first.Compare(second, ErrorMetric.RootMeanSquared)); } } MagickNET.SetRandomSeed(1337); using (IMagickImage first = new MagickImage("plasma:red", 10, 10)) { using (IMagickImage second = new MagickImage("plasma:red", 10, 10)) { Assert.AreEqual(0.0, first.Compare(second, ErrorMetric.RootMeanSquared)); } } }
public void ShouldReturnErrorInfoWhenTheImagesAreNotEqual() { using (var image = new MagickImage(Files.Builtin.Logo)) { using (var other = new MagickImage(Files.Builtin.Logo)) { other.Rotate(180); var errorInfo = image.Compare(other); #if Q8 Assert.InRange(errorInfo.MeanErrorPerPixel, 44.55, 44.56); #else Assert.InRange(errorInfo.MeanErrorPerPixel, 11450.85, 11450.86); #endif Assert.Equal(1, errorInfo.NormalizedMaximumError); Assert.InRange(errorInfo.NormalizedMeanError, 0.13, 0.14); } } }
/// <summary> /// Compares 2 images and returns true if they are the same /// </summary> /// <returns><c>true</c>, if image was verifyed, <c>false</c> otherwise.</returns> /// <param name="expectedFileName">Expected file name.</param> /// <param name="resultFileName">Result file name.</param> private bool VerifyImage(string expectedFileName, string resultFileName) { var dir = TestContext.CurrentContext.TestDirectory; MagickImage expectedImage; using (var expectedOutput = new FileStream($"{dir}../../../../Resources/{expectedFileName}", FileMode.Open)) { expectedImage = new MagickImage(expectedOutput); } MagickImage resultingImage; using (var resultAsStream = new FileStream($"{TestContext.CurrentContext.WorkDirectory}/{resultFileName}", FileMode.Open)) { resultingImage = new MagickImage(resultAsStream); } return(Math.Abs(resultingImage.Compare(expectedImage, ErrorMetric.Absolute)) < 0.1); }
public void ShouldUseTheSpecifiedMode() { using (var quantumImage = new MagickImage(Files.PictureJPG)) { quantumImage.SetProfile(ColorProfile.USWebCoatedSWOP); using (var highResImage = new MagickImage(Files.PictureJPG)) { highResImage.SetProfile(ColorProfile.USWebCoatedSWOP, ColorTransformMode.HighRes); var difference = quantumImage.Compare(highResImage, ErrorMetric.RootMeanSquared); #if Q16HDRI Assert.Equal(0.0, difference); #else Assert.NotEqual(0.0, difference); #endif } } }
public void ShouldBlurTheImage() { using (var gaussian = new MagickImage(Files.Builtin.Wizard)) { gaussian.GaussianBlur(5.5, 10.2); using (var blur = new MagickImage(Files.Builtin.Wizard)) { blur.Blur(5.5, 10.2); double distortion = blur.Compare(gaussian, ErrorMetric.RootMeanSquared); #if Q8 Assert.InRange(distortion, 0.00066, 0.00067); #elif Q16 Assert.InRange(distortion, 0.0000033, 0.0000034); #else Assert.InRange(distortion, 0.0000011, 0.0000012); #endif } } }
private static bool CompareImages(MagickImage expectedImage, MagickImage imageToCompare, string deltaImagePath) { double ThreashHold = 0.01; using (var delta = new MagickImage()) { var result = expectedImage.Compare(imageToCompare, ErrorMetric.Fuzz, delta); if (result > ThreashHold) { delta.Write(deltaImagePath); AllureLifecycle.Instance.AddAttachment("smeid-diff", "image/png", deltaImagePath); Console.WriteLine($"Threshhold: {ThreashHold} compare result: {result} Does not match {expectedImage.FileName}."); return(false); } else { Console.WriteLine($"Threshhold: {ThreashHold} compare result: {result} Matched {expectedImage.FileName}"); return(true); } } }
private static CompareResult CheckOperator(MagickImage sourceIcon, string operatorName) { string filepath = Path.Combine(cache, operatorName + ".shrink.png"); CompareResult result = new CompareResult() { operatorName = operatorName, fileName = filepath, weight = 0, minimum = 0 }; if (!File.Exists(filepath)) { return(result); } using (var operatorIcon = new MagickImage(filepath)) result.weight = sourceIcon.Compare(operatorIcon, new ErrorMetric()); return(result); }
public void ShouldReturnErrorInfoWhenTheImagesAreNotEqual() { using (IMagickImage image = new MagickImage(Files.Builtin.Logo)) { using (IMagickImage other = new MagickImage(Files.Builtin.Logo)) { other.Rotate(180); MagickErrorInfo errorInfo = image.Compare(other); #if Q8 Assert.AreEqual(44.55, errorInfo.MeanErrorPerPixel, 0.01); #elif Q16 || Q16HDRI Assert.AreEqual(11450.85, errorInfo.MeanErrorPerPixel, 0.01); #else #error Not implemented! #endif Assert.AreEqual(1, errorInfo.NormalizedMaximumError); Assert.AreEqual(0.13, errorInfo.NormalizedMeanError, 0.01); } } }
public async Task TestImgDiffing() { FileEntry imFileToDiff_original = await GetImageFile("imFileToDiff_original.jpg"); FileEntry imFileToDiff_modified = imFileToDiff_original.Parent.GetChild("imFileToDiff_sharpened.jpg"); var t = Log.MethodEntered("Load modify save compare save diff"); if (!imFileToDiff_modified.IsNotNullAndExists()) { GenerateModifiedVariant(imFileToDiff_original, imFileToDiff_modified); } using (MagickImage newImg = new MagickImage()) { newImg.LoadFromFileEntry(imFileToDiff_modified); var diff = newImg.Compare(imFileToDiff_original, ErrorMetric.MeanSquared, 0.000001); Assert.NotNull(diff); } Log.MethodDone(t); Log.d("Saved to " + imFileToDiff_modified.GetFullFileSystemPath()); }
public static double CompareV2(this MagickImage self, MagickImage oldImg, ErrorMetric errorMetric, out MagickImage imgWithDifferences) { AssertV2.AreEqual(oldImg.Width, self.Width, "RegressionImage.Width"); AssertV2.AreEqual(oldImg.Height, self.Height, "RegressionImage.Height"); // Check if both images have the same size: if (oldImg.Width != self.Width || oldImg.Height != self.Height) { if (self.GetAspectRatio() == oldImg.GetAspectRatio()) { // If aspect ratio matches, resize both images to same size: var minWidth = Math.Min(oldImg.Width, self.Width); var minHeight = Math.Min(oldImg.Height, self.Height); oldImg.Resize(minWidth, minHeight); self.Resize(minWidth, minHeight); } } //return oldImg.Compare(self, out imgWithDifferences); imgWithDifferences = new MagickImage(); return(oldImg.Compare(self, new CompareSettings() { Metric = errorMetric }, imgWithDifferences)); }
public async Task ShouldReadImageFromSeekablePartialStream() { using (var image = new MagickImage()) { using (var fileStream = File.OpenRead(Files.ImageMagickJPG)) { await image.ReadAsync(fileStream); fileStream.Position = 0; using (var partialStream = new PartialStream(fileStream, true)) { using (var testImage = new MagickImage()) { await testImage.ReadAsync(partialStream); Assert.Equal(image.Width, testImage.Width); Assert.Equal(image.Height, testImage.Height); Assert.Equal(image.Format, testImage.Format); Assert.Equal(0.0, image.Compare(testImage, ErrorMetric.RootMeanSquared)); } } } } }
public void ShouldPingImageFromNonSeekablePartialStream() { using (IMagickImage image = new MagickImage()) { using (FileStream fs = File.OpenRead(Files.ImageMagickJPG)) { image.Ping(fs); fs.Position = 0; using (PartialStream partialStream = new PartialStream(fs, false)) { using (IMagickImage testImage = new MagickImage()) { testImage.Ping(partialStream); Assert.AreEqual(image.Width, testImage.Width); Assert.AreEqual(image.Height, testImage.Height); Assert.AreEqual(image.Format, testImage.Format); Assert.AreEqual(0.0, image.Compare(testImage, ErrorMetric.RootMeanSquared)); } } } } }
public void saveResult(string FilePath1, string FilePath2, string outFilePath) { MagickImageCollection images = new MagickImageCollection(); // Add the first image MagickImage first = new MagickImage(FilePath1); // images.Add(first); // Add the second image MagickImage second = new MagickImage(FilePath2); // images.Add(second); /* * first.Composite(second, 0, 0, CompositeOperator.ModulusAdd); * first.Write(outFilePath + "compos.png"); * */ MagickImage third = new MagickImage(); first.Compare(second, ErrorMetric.Absolute, third); third.Write(outFilePath + "third.bmp"); }