Skip to content

Codeuctivity/SkiaSharp.Compare

 
 

Repository files navigation

SkiaSharpCompare

Compares images

.NET build and test Nuget Donate

Inspired by the image compare feature "Visual verification API" of TestApi this code supports comparing images by using a tolerance mask image. That tolerance mask image is a valid image by itself and can be manipulated.

SkiaSharpCompare focus on os agnostic support and therefore depends on SkiaSharp.

Example simple show cases

Compares each RGB value of each pixel to determine the equality

bool isEqual = Compare.ImagesAreEqual("actual.png", "expected.png");
var calcDiff = Compare.CalcDiff("2x2PixelBlack.png", "2x2PixelWhite.png");
Console.WriteLine($"PixelErrorCount: {calcDiff.PixelErrorCount}");
Console.WriteLine($"PixelErrorPercentage: {calcDiff.PixelErrorPercentage}");
Console.WriteLine($"AbsoluteError: {calcDiff.AbsoluteError}");
Console.WriteLine($"MeanError: {calcDiff.MeanError}");
// PixelErrorCount: 4
// PixelErrorPercentage: 100
// AbsoluteError: 3060
// MeanError: 765

Example show case allowing some tolerated diff

Imagine two images you want to compare, and want to accept the found difference as at state of allowed difference.

Reference Image

actual image

Actual Image

actual image

Tolerance mask image

Using CalcDiffMaskImage you can calc a diff mask from actual and reference image

Example - Create difference image

using (var fileStreamDifferenceMask = File.Create("differenceMask.png"))
using (var maskImage = SkiaSharpCompare.CalcDiffMaskImage(pathPic1, pathPic2))
var encodedData = maskImage.Encode(SKEncodedImageFormat.Png, 100);
encodedData.SaveTo(fileStreamDifferenceMask);

differenceMask.png

Example - Compare two images using the created difference image. Add white pixels to differenceMask.png where you want to allow difference.

var maskedDiff = SkiaSharpCompare.CalcDiff(pathPic1, pathPic2, "differenceMask.png");
Assert.That(maskedDiff.AbsoluteError, Is.EqualTo(0));
var comparer = new ImageCompare(ResizeOption.Resize, TransparencyOptions.CompareAlphaChannel, pixelColorShiftTolerance: 5, compareMetadata: true);
var calcDiff = comparer.CalcDiff("pngPartialTransparent4x4Pixel.png", "2x2PixelWhite.png");
// Displaying the differences
Console.WriteLine($"PixelErrorCount: {calcDiff.PixelErrorCount}");
Console.WriteLine($"PixelErrorPercentage: {calcDiff.PixelErrorPercentage}");
Console.WriteLine($"AbsoluteError: {calcDiff.AbsoluteError}");
Console.WriteLine($"MeanError: {calcDiff.MeanError}");
foreach (var metadataDifference in calcDiff.MetadataDifferences)
    Console.WriteLine($"Metadata Difference: {metadataDifference}");

// PixelErrorCount: 16
// PixelErrorPercentage: 100
// AbsoluteError: 14272
// MeanError: 892
// Metadata Difference: [File:File Name, (pngPartialTransparent4x4Pixel.png, 2x2PixelWhite.png)]
// Metadata Difference: [File:File Size, (688 bytes, 556 bytes)]
// Metadata Difference: [PNG-IHDR:Color Type, (True Color with Alpha, True Color)]
// Metadata Difference: [PNG-IHDR:Image Height, (4, 2)]
// Metadata Difference: [PNG-IHDR:Image Width, (4, 2)]
// Metadata Difference: [PNG-tEXt:Textual Data, (Software: Paint.NET 5.1.2, Comment: Created with GIMP)]
// ...

About

Adds compare features on top of SkiaSharp

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages

  • C# 100.0%