←Select platform

CompareBitmapCommand Class

Summary

Compares two images for additions, deletions, and changes and generates an image with the differences highlighted.

Syntax
C#
VB
C++
public class CompareBitmapCommand : RasterCommand 
Public Class CompareBitmapCommand 
   Inherits RasterCommand 
public: 
   ref class CompareBitmapCommand : RasterCommand 

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

Example
C#
VB
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:\Users\Public\Documents\LEADTOOLS Images" 
End Class 
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:\Users\Public\Documents\LEADTOOLS Images"; 
} 

Requirements

Target Platforms

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

Leadtools.ImageProcessing.Core Assembly