Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Default usage:
var secondImage = new Bitmap("path/to/second/image);

var comparer = new BitmapComparer();
var diff = comparer.Compare(firstImage, secondImage);
var diff = comparer.Compare(firstImage, secondImage); //Returns a result with the differences + the Bitmap
var generate = comparer.Generate(firstImage, secondImage);// Generates the bitmap image and returns a Bitmap

When initialized without options, the following values are used:

Expand All @@ -36,19 +37,21 @@ When initialized without options, the following values are used:
- BoundingBoxPadding: 2
- BoundingBoxColor: Red
- BoundingBoxMode: Single
- BoundingBoxThickness: 1


The compare object can be configured to use different settings for the different stages of processing.

var options = new CompareOptions
{
AnalyzerType = AnalyzerTypes.CIE76,
AnalyzerType = AnalyzerTypes.CIE76,
JustNoticableDifference = 2.3,
DetectionPadding = 2,
Labeler = LabelerTypes.ConnectedComponentLabeling,
BoundingBoxColor = Color.Red,
BoundingBoxPadding = 2,
BoundingBoxMode = BoundingBoxModes.Multiple
BoundingBoxMode = BoundingBoxModes.Multiple,
BoundingBoxThickness = 1
};
var comparer = new BitmapComparer(options);

Expand Down Expand Up @@ -76,6 +79,10 @@ The color of the bounding box to be drawn when highlighting detected differences
#### Bounding Box Padding
The number of pixels of padding to include around the detected difference when drawing a bounding box.

#### Bounding Box Thickness
The thickness of the rectangle showing the difference in pixel. Default = 1


#### Bounding Box Mode
Specifies how to build the bounding boxes when highlighting the detected differences.

Expand Down
Binary file added src/.vs/ImageDiff/v16/TestStore/0/009.testlog
Binary file not shown.
Binary file added src/.vs/ImageDiff/v16/TestStore/0/testlog.manifest
Binary file not shown.
20 changes: 16 additions & 4 deletions src/ImageDiff/BitmapComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class BitmapComparer : IImageComparer<Bitmap>
private int DetectionPadding { get; set; }
private int BoundingBoxPadding { get; set; }
private Color BoundingBoxColor { get; set; }
private int BoundingBoxThickness { get; set; }
private BoundingBoxModes BoundingBoxMode { get; set; }
private AnalyzerTypes AnalyzerType { get; set; }

Expand Down Expand Up @@ -47,21 +48,31 @@ private void Initialize(CompareOptions options)
BoundingBoxPadding = options.BoundingBoxPadding;
BoundingBoxMode = options.BoundingBoxMode;
AnalyzerType = options.AnalyzerType;
BoundingBoxThickness = options.BoundingBoxThickness;
}

public Bitmap Compare(Bitmap firstImage, Bitmap secondImage)


public Bitmap Generate(Bitmap firstImage, Bitmap secondImage)
{
Result result = Compare(firstImage, secondImage);
return result.Image;
}

public Result Compare(Bitmap firstImage, Bitmap secondImage)
{
if (firstImage == null) throw new ArgumentNullException("firstImage");
if (secondImage == null) throw new ArgumentNullException("secondImage");
if (firstImage.Width != secondImage.Width || firstImage.Height != secondImage.Height) throw new ArgumentException("Bitmaps must be the same size.");

var differenceMap = BitmapAnalyzer.Analyze(firstImage, secondImage);
var differenceLabels = Labeler.Label(differenceMap);
var boundingBoxes = BoundingBoxIdentifier.CreateBoundingBoxes(differenceLabels);
var differenceBitmap = CreateImageWithBoundingBoxes(secondImage, boundingBoxes);
return differenceBitmap;
return Result.Create(differenceBitmap, boundingBoxes);
}


public bool Equals(Bitmap firstImage, Bitmap secondImage)
{
if (firstImage == null && secondImage == null) return true;
Expand Down Expand Up @@ -95,7 +106,8 @@ private Bitmap CreateImageWithBoundingBoxes(Bitmap secondImage, IEnumerable<Rect

using (var g = Graphics.FromImage(differenceBitmap))
{
var pen = new Pen(BoundingBoxColor);
var pen = new Pen(BoundingBoxColor,BoundingBoxThickness) ;

foreach (var boundingRectangle in boundingRectangles)
{
g.DrawRectangle(pen, boundingRectangle);
Expand Down
2 changes: 2 additions & 0 deletions src/ImageDiff/CompareOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class CompareOptions
public double JustNoticeableDifference { get; set; }
public int DetectionPadding { get; set; }
public int BoundingBoxPadding { get; set; }
public int BoundingBoxThickness { get; set; }
public Color BoundingBoxColor { get; set; }
public BoundingBoxModes BoundingBoxMode { get; set; }

Expand All @@ -21,6 +22,7 @@ public CompareOptions()
BoundingBoxColor = Color.Red;
BoundingBoxMode = BoundingBoxModes.Single;
AnalyzerType = AnalyzerTypes.ExactMatch;
BoundingBoxThickness = 1;
}
}
}
3 changes: 2 additions & 1 deletion src/ImageDiff/IImageComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace ImageDiff
{
public interface IImageComparer<T> where T : Image
{
T Compare(T firstImage, T secondImage);
T Generate(T firstImage, T secondImage);
Result Compare(T firstImage, T secondImage);
bool Equals(T firstImage, T secondImage);
}
}
1 change: 1 addition & 0 deletions src/ImageDiff/ImageDiff.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="BoundingBoxes\MultipleBoundingBoxIdentifier.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="BoundingBoxes\SingleBoundingBoxIdentifer.cs" />
<Compile Include="ResultBitmap.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
55 changes: 55 additions & 0 deletions src/ImageDiff/ResultBitmap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageDiff
{
public class Result
{

public static Result Create(Bitmap image, IEnumerable<Rectangle> boundingBoxes)
{
return new Result()
{
Image = image,
BoundingBoxes = boundingBoxes
};
}

public System.Drawing.Bitmap Image { get; set; }

public IEnumerable<Rectangle> BoundingBoxes{ get; set; }

public bool IsSimilar
{
get
{
return BoundingBoxes.Count() == 0;
}
}

/// <summary>
/// For more options, use Image.Save
/// </summary>
/// <param name="filename"></param>
public void Save(string filename)
{
Image.Save(filename);
}

public void Save(string filename, System.Drawing.Imaging.ImageFormat format)
{
Image.Save(filename, format);
}





}

}

Loading