←Select platform

CompareBitmapCommand Class


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 CompareBitmapCommand 
    extends RasterCommand 
   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:

Threshold 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.

  1. An image is defined to be the 'reference image'. In this example, we start with the ocr1.tif image from the sample directory.

  2. 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

    CompareBitmap Reference Image
  3. 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

    CompareBitmap Modified Image
  4. 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

    CompareBitmap 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); 
      // Remove the title from the modified image 
      modifiedImage.AddRectangleToRegion(null, new LeadRect(290, 300, 810, 110), RasterRegionCombineMode.Set); 
      new FillCommand(RasterColor.White).Run(modifiedImage); 
      // 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.Translate(referenceImage.Width * 0.5, referenceImage.Height * 0.5); 
      // Setup the comparison options 
      CompareBitmapCommand 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 images 
      // 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:\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 image 
   RasterImage referenceImage = codecs.load(combine(LEAD_VARS_IMAGES_DIR, "ocr1.tif")); 
   // Use the same image for the "modified" image 
   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); 
   // Remove the title from the modified image 
   modifiedImage.addRectangleToRegion(null, new LeadRect(290, 300, 810, 110), RasterRegionCombineMode.SET); 
   new FillCommand(RasterColor.WHITE).run(modifiedImage); 
   // 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 rotation 
   LeadMatrix alignment = LeadMatrix.getIdentity(); 
   alignment.translate(-1 * modifiedImage.getWidth() * 0.5, -1 * modifiedImage.getHeight() * 0.5); 
   alignment.translate(referenceImage.getWidth() * 0.5, referenceImage.getHeight() * 0.5); 
   // Setup the comparison options 
   CompareBitmapCommand command = new CompareBitmapCommand(); 
   // Compare the images 
   // Save the results 
   RasterImage outputImage = command.getOutputImage(); 
   codecs.save(outputImage, combine(LEAD_VARS_IMAGES_DIR, "CompareBitmap_Output.png"), RasterImageFormat.PNG, 0); 
   // Save the two input images, for reference 
   codecs.save(referenceImage, combine(LEAD_VARS_IMAGES_DIR, "CompareBitmap_Reference.png"), RasterImageFormat.PNG, 
   codecs.save(modifiedImage, combine(LEAD_VARS_IMAGES_DIR, "CompareBitmap_Modified.png"), RasterImageFormat.PNG, 0); 
   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")); 

Target Platforms

Help Version 23.0.2024.3.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.ImageProcessing.Core Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.