Compares two images for additions, deletions, and changes and generates an image with the differences highlighted.
public class CompareBitmapCommand : RasterCommand @interface LTCompareBitmapCommand : LTRasterCommand public class CompareBitmapCommandextends RasterCommand
public:ref class CompareBitmapCommand : RasterCommand
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 imageusing (RasterImage referenceImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "ocr1.tif")))// Use the same image for the "modified" imageusing (RasterImage modifiedImage = referenceImage.Clone()){// Remove the last paragraph of the reference imagereferenceImage.AddRectangleToRegion(null, new LeadRect(290, 2470, 1930, 360), RasterRegionCombineMode.Set);new FillCommand(RasterColor.White).Run(referenceImage);referenceImage.MakeRegionEmpty();// Remove the title from the modified imagemodifiedImage.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 rotationLeadMatrix 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 optionsCompareBitmapCommand command = new CompareBitmapCommand(){Alignment = alignment,ReferenceImage = referenceImage,// following properties can be left to their defaults or set to desired values:ModifiedBackground = RasterColor.White,ModifiedForeground = RasterColor.Black,OutputAddition = RasterColor.FromKnownColor(RasterKnownColor.Lime),OutputBackground = RasterColor.FromKnownColor(RasterKnownColor.White),OutputChange = RasterColor.FromKnownColor(RasterKnownColor.Yellow),OutputDeletion = RasterColor.FromKnownColor(RasterKnownColor.Red),OutputExternal = new RasterColor(0x80, 0x80, 0xff),OutputMatch = new RasterColor(0x40, 0x40, 0x40),ReferenceBackground = RasterColor.White,ReferenceForeground = RasterColor.Black,Threshold = 0};// Compare the imagescommand.Run(modifiedImage);// Save the resultsusing (RasterImage outputImage = command.OutputImage)codecs.Save(outputImage, Path.Combine(LEAD_VARS.ImagesDir, "CompareBitmap_Output.png"), RasterImageFormat.Png, 0);// Save the two input images, for referencecodecs.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:\LEADTOOLS23\Resources\Images";}
import static org.junit.Assert.assertTrue;import java.io.File;import java.io.IOException;import org.junit.*;import org.junit.runner.JUnitCore;import org.junit.runner.Result;import org.junit.runner.notification.Failure;import leadtools.*;import leadtools.codecs.*;import leadtools.imageprocessing.FillCommand;import leadtools.imageprocessing.RotateCommand;import leadtools.imageprocessing.RotateCommandFlags;import leadtools.imageprocessing.core.CompareBitmapCommand;public void compareBitmapCommandExample() {final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";RasterCodecs codecs = new RasterCodecs();// Load the original imageRasterImage referenceImage = codecs.load(combine(LEAD_VARS_IMAGES_DIR, "ocr1.tif"));// Use the same image for the "modified" imageRasterImage modifiedImage = referenceImage.clone();// Remove the last paragraph of the reference imagereferenceImage.addRectangleToRegion(null, new LeadRect(290, 2470, 1930, 360), RasterRegionCombineMode.SET);new FillCommand(RasterColor.WHITE).run(referenceImage);referenceImage.makeRegionEmpty();// Remove the title from the modified imagemodifiedImage.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.getValue(), RasterColor.BLACK).run(modifiedImage);// Update the transformation to align/reverse the above rotationLeadMatrix alignment = LeadMatrix.getIdentity();alignment.translate(-1 * modifiedImage.getWidth() * 0.5, -1 * modifiedImage.getHeight() * 0.5);alignment.rotate(20.0);alignment.translate(referenceImage.getWidth() * 0.5, referenceImage.getHeight() * 0.5);// Setup the comparison optionsCompareBitmapCommand command = new CompareBitmapCommand();command.setAlignment(alignment);command.setReferenceImage(referenceImage);// Compare the imagescommand.run(modifiedImage);// Save the resultsRasterImage outputImage = command.getOutputImage();codecs.save(outputImage, combine(LEAD_VARS_IMAGES_DIR, "CompareBitmap_Output.png"), RasterImageFormat.PNG, 0);// Save the two input images, for referencecodecs.save(referenceImage, combine(LEAD_VARS_IMAGES_DIR, "CompareBitmap_Reference.png"), RasterImageFormat.PNG,0);codecs.save(modifiedImage, combine(LEAD_VARS_IMAGES_DIR, "CompareBitmap_Modified.png"), RasterImageFormat.PNG, 0);referenceImage.dispose();codecs.dispose();assertTrue(new File(combine(LEAD_VARS_IMAGES_DIR, "CompareBitmap_Reference.png")).exists());System.out.println("Command run, image saved to " + combine(LEAD_VARS_IMAGES_DIR, "CompareBitmap_Reference.png"));}
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
