Compares two images for additions, deletions, and changes and generates an image with the differences highlighted.
public class CompareBitmapCommand : RasterCommand
Public Class CompareBitmapCommand
Inherits RasterCommand
public:
ref class CompareBitmapCommand : RasterCommand
CompareBitmapCommand
compares the RGB values of the pixels of a reference image with those of the modified image.
Each pixel in the Output image is assigned a color-coded index value, resulting in a "map" showing additions, deletions, changes, and no changes.
Typically, this function is used on black and white images, but can be used on colored images as well.
If alignment is necessary, use the Alignment property to set up a LeadMatrix object to perform the transformation.
The Threshold defines the range in color values allowed for the same color. The distance is calculated in Euclidean RGB color space using the following equation:
The OutputImage
will be 3 bits-per-pixel. The first six entries of its palette are populated as follows:
Index | RasterColor | Default |
---|---|---|
0 | OutputExternal | rgb(128, 128, 255) |
1 | OutputBackground | rgb(255, 255, 255) |
2 | OutputMatch | rgb(64, 64, 64) |
3 | OutputAddition | rgb(0, 255, 0) |
4 | OutputDeletion | rgb(255, 0, 0) |
5 | OutputChange | rgb(255, 255, 0) |
Default values for the RGB colors have already been set. For details, see the individual properties.
The following walk-through of the example code, below, illustrates how the command works.
An image is defined to be the 'reference image'. In this example, we start with the ocr1.tif
image from the sample directory.
For demonstration purposes, the reference image is cloned (Line 12 in the C# example code). The 'reference image' is processed to remove the last paragraph at the bottom of the image (Line 14 in C# example code).
Reference Image
From the clone image, we remove the title (Line 19 in C# example code) and rotate it clockwise around the center (Line 25 in C# example code). Call this edited clone the 'modified image'.
Modified Image
CompareBitmapCommand is run. The command assigns each output image pixel a color, based on the result of the comparison between the pixel on the 'reference image' and its corresponding pixel on the 'modified image'.
Output Image
In this example, the title ("LEAD Technologies") appears in the 'reference image' but not on the 'modified image'. The title has been removed, and so takes the OutputDeletion color. Also, the last heading and paragraph in the 'modified image' do not appear in the 'reference image'. They have been added to the 'modified image', and so take the OutputAddition color. The sections that are not in either image are shown in the OutputExternal color.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Core;
public void CompareBitmapCommandExample()
{
using (RasterCodecs codecs = new RasterCodecs())
// Load the original image
using (RasterImage referenceImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "ocr1.tif")))
// Use the same image for the "modified" image
using (RasterImage modifiedImage = referenceImage.Clone())
{
// Remove the last paragraph of the reference image
referenceImage.AddRectangleToRegion(null, new LeadRect(290, 2470, 1930, 360), RasterRegionCombineMode.Set);
new FillCommand(RasterColor.White).Run(referenceImage);
referenceImage.MakeRegionEmpty();
// Remove the title from the modified image
modifiedImage.AddRectangleToRegion(null, new LeadRect(290, 300, 810, 110), RasterRegionCombineMode.Set);
new FillCommand(RasterColor.White).Run(modifiedImage);
modifiedImage.MakeRegionEmpty();
// Rotate the modified image for demonstration (angle measured in hundredths of a degree)
new RotateCommand(340 * 100, RotateCommandFlags.Resize, RasterColor.Black).Run(modifiedImage);
// Update the transformation to align/reverse the above rotation
LeadMatrix alignment = LeadMatrix.Identity;
alignment.Translate(-modifiedImage.Width * 0.5, -modifiedImage.Height * 0.5);
alignment.Rotate(20.0);
alignment.Translate(referenceImage.Width * 0.5, referenceImage.Height * 0.5);
// Setup the comparison options
CompareBitmapCommand command = new CompareBitmapCommand()
{
Alignment = alignment,
ReferenceImage = referenceImage
};
// Compare the images
command.Run(modifiedImage);
// Save the results
using (RasterImage outputImage = command.OutputImage)
codecs.Save(outputImage, Path.Combine(LEAD_VARS.ImagesDir, "CompareBitmap_Output.png"), RasterImageFormat.Png, 0);
// Save the two input images, for reference
codecs.Save(referenceImage, Path.Combine(LEAD_VARS.ImagesDir, "CompareBitmap_Reference.png"), RasterImageFormat.Png, 0);
codecs.Save(modifiedImage, Path.Combine(LEAD_VARS.ImagesDir, "CompareBitmap_Modified.png"), RasterImageFormat.Png, 0);
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS21\Resources\Images";
}
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Core
Public Sub CompareBitmapCommandExample()
Using codecs As RasterCodecs = New RasterCodecs()
' Load the original image
Using referenceImage As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "ocr1.tif"))
' Use the same image for the "modified" image
Using modifiedImage As RasterImage = referenceImage.Clone()
' Remove the last paragraph of the reference image
referenceImage.AddRectangleToRegion(Nothing, New LeadRect(290, 2470, 1930, 360), RasterRegionCombineMode.Set)
Call New FillCommand(RasterColor.White).Run(referenceImage)
referenceImage.MakeRegionEmpty()
' Remove the title from the modified image
modifiedImage.AddRectangleToRegion(Nothing, New LeadRect(290, 300, 810, 110), RasterRegionCombineMode.Set)
Call New FillCommand(RasterColor.White).Run(modifiedImage)
modifiedImage.MakeRegionEmpty()
' Rotate the modified image for demonstration (angle measured in hundredths of a degree)
Call New RotateCommand(340 * 100, RotateCommandFlags.Resize, RasterColor.Black).Run(modifiedImage)
' Update the transformation to align/reverse the above rotation
Dim alignment As LeadMatrix = LeadMatrix.Identity
alignment.Translate(-modifiedImage.Width * 0.5, -modifiedImage.Height * 0.5)
alignment.Rotate(20.0)
alignment.Translate(referenceImage.Width * 0.5, referenceImage.Height * 0.5)
' Setup the comparison options
Dim command As CompareBitmapCommand = New CompareBitmapCommand() With {
.Alignment = alignment,
.ReferenceImage = referenceImage
}
' Compare the images
command.Run(modifiedImage)
' Save the results
Using outputImage As RasterImage = command.OutputImage
codecs.Save(outputImage, Path.Combine(LEAD_VARS.ImagesDir, "CompareBitmap_Output.png"), RasterImageFormat.Png, 0)
End Using
' Save the two input images, for reference
codecs.Save(referenceImage, Path.Combine(LEAD_VARS.ImagesDir, "CompareBitmap_Reference.png"), RasterImageFormat.Png, 0)
codecs.Save(modifiedImage, Path.Combine(LEAD_VARS.ImagesDir, "CompareBitmap_Modified.png"), RasterImageFormat.Png, 0)
End Using
End Using
End Using
End Sub
Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\LEADTOOLS21\Resources\Images"
End Class
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document