Compare Image Data for Similarities - C# .NET 6

This tutorial shows how to use various image processing commands to compare two images to determine how similar they are in a C# .NET 6 application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use various image processing commands to compare images in a C# .NET 6 Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on this tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both).

If using NuGet references, this tutorial requires the following NuGet package:

If using local DLL references, the following DLLs are needed.

The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net:

For a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the Image Comparison Code

With the project created, the references added, and the license set, coding can begin.

This tutorial will compare two images by calculating the proportion of black pixels after using the CombineCommand with the OperationXor flag, the average intensity of all pixels using the StatisticsInformationCommand, and the average hue and saturation values using the ColorSeparateCommand.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs:

C#
using System; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.ImageProcessing.Core; 
using Leadtools.ImageProcessing.Effects; 

Inside the Main method add two new string variables and set them equal to the two separate image file paths, as shown below. For the purposes of this tutorial you can download the test images here. For example, compare 1stRed.png with 2ndRed.png.

C#
static void Main(string[] args) 
{ 
   string image1Filename = @"FILEPATH TO FIRST IMAGE"; 
   string image2Filename = @"FILEPATH TO SECOND IMAGE"; 
 
   InitLEAD(); 
 
   // RasterImages are IDisposable so they must be in using blocks if not calling .Dispose() later 
   using RasterImage image1 = LoadImage(image1Filename); 
   using RasterImage image2 = LoadImage(image2Filename); 
 
   Console.WriteLine($"Image 1: {image1Filename}"); 
   Console.WriteLine($"Image 2: {image2Filename}\n"); 
 
   CompareXOR(image1, image2); 
   CompareIntensities(image1, image2); 
   CompareHSV(image1, image2); 
} 

Add a new method to the Program class named LoadImage(string _filePath). Call the method twice inside using statements in the Main method, below the call to the InitLEAD method, as shown above. Add the code below to the LoadImage method to load the given files as RasterImage objects.

C#
static RasterImage LoadImage(string filename)  
{  
   using RasterCodecs codecs = new RasterCodecs(); 
   return codecs.Load(filename);  
}  

Add four new methods to the Program class named CompareXOR(RasterImage image1, RasterImage image2), CompareIntensities(RasterImage image1, RasterImage image2), and CompareHSV(RasterImage image1, RasterImage image2). Call all four of these methods below the calls to the LoadImage method, inside the using statements, as shown above.

Add the below code for the CompareXOR method. In this method we will use the CombineCommand. This places one image onto another at a certain location and within a certain rectangle. The way the pixels of the underlying image are affected by the overlaying image depends on the flags we use. In this case we will use a LeadRect that is the same size as the bottom image to use the whole image. We will use the OperationXor flag which will perform a bitwise XOR operation onto each set of top and bottom pixels from each image. This should create black pixels where the two pixels in the same location in either images are the same, and white everywhere else. To calculate how much is the same between the two images, we will create a region out of only the black pixels and compare it to the sum of all pixels.

C#
private static void CompareXOR(RasterImage image1, RasterImage image2) 
{ 
   using RasterImage image2Copy = image2.Clone(); 
    
   // Combine the images with XOR 
   new CombineCommand() 
   { 
      DestinationRectangle = new LeadRect(0, 0, image1.Width, image1.Height), 
      Flags = CombineCommandFlags.OperationXor, 
      SourceImage = image1, 
      SourcePoint = new LeadPoint(0, 0) 
   }.Run(image2Copy); 
 
   // Calculate the percentage of black pixels (where XOR was identical) 
   image2Copy.AddColorToRegion(RasterColor.Black, RasterRegionCombineMode.Set); 
   double ratio = (double)image2Copy.CalculateRegionArea() / (image2Copy.Width * image2Copy.Height); 
   Console.WriteLine($"Individual pixels: {100.0 * ratio:0.00}% match"); 
} 

Add the below code for the CompareIntensities method to create an instance of the StatisticsInformationCommand, which allows you to only include pixels of a certain range or color in the statistics population. We will use all pixels, from values 0-255 from the master channel. We will gather the images' mean values and compare the two, rescaling it to 0-1.00 instead of 0-255. This will give us the difference in average intensity or brightness.

We can also use this command to derive intensity of the difference of these images by passing it through an XOR CombineCommand like before. This will be calculated from the ratio of identical pixel values to differing pixel values.

C#
private static void CompareIntensities(RasterImage image1, RasterImage image2) 
{ 
   using RasterImage image1Gray = image1.Clone(); 
   using RasterImage image2Gray = image1.Clone(); 
    
   // Convert both images to grayscale 
   GrayscaleCommand grayscale = new GrayscaleCommand() 
   { 
      BitsPerPixel = 8 
   }; 
   grayscale.Run(image1Gray); 
   grayscale.Run(image2Gray); 
 
   // Compare the average overall intensities 
   StatisticsInformationCommand statistics = new StatisticsInformationCommand() 
   { 
      Channel = RasterColorChannel.Master, 
      End = 255, 
      Start = 0 
   }; 
   statistics.Run(image1Gray); 
   double leftIntensity = statistics.Mean; 
   statistics.Run(image2Gray); 
   double rightIntensity = statistics.Mean; 
   double similarity = 1.0 - (Math.Abs(leftIntensity - rightIntensity) / 255.0); 
   Console.WriteLine($"Overall image intensities: {100.0 * similarity:0.00}% match"); 
 
   // Check the intensity of the difference 
   new CombineCommand() 
   { 
      DestinationRectangle = new LeadRect(0, 0, image1Gray.Width, image1Gray.Height), 
      Flags = CombineCommandFlags.OperationXor, 
      SourceImage = image1Gray, 
      SourcePoint = new LeadPoint(0, 0) 
   }.Run(image2Gray); 
 
   statistics.Run(image2Gray); 
   similarity = 1.0 - (statistics.Mean / 255.0); 
   Console.WriteLine($"Image difference intensity: {100.0 * similarity:0.00}% match"); 
} 

Add the below code for the CompareHSV method to separate the images into Hue, Saturation, and Value channels instead of Red, Green, and Blue. This creates a DestinationImage object with three pages, one for each channel. We can then use the StatisticsInformationCommand again to find the average Hue and Saturation for both images by running it on each page of the separated image. The average Value is essentially the image's brightness which is not as useful. We can then compare the difference in average Hue and Saturation, again scaling it to 0-100 from 0-255.

C#
private static void CompareHSV(RasterImage image1, RasterImage image2) 
{ 
   ColorSeparateCommand colorSeparate = new ColorSeparateCommand() 
   { 
      Type = ColorSeparateCommandType.Hsv 
   }; 
 
   StatisticsInformationCommand statistics = new StatisticsInformationCommand() 
   { 
      Channel = RasterColorChannel.Master, 
      End = 255, 
      Start = 0 
   }; 
 
   // Separate the first image, and calculate the average values 
   colorSeparate.Run(image1); 
   double image1Hue, image1Saturation; 
   using RasterImage image1Separated = colorSeparate.DestinationImage; 
       
   image1Separated.Page = 1; 
   statistics.Run(image1Separated); 
   image1Hue = statistics.Mean; 
   image1Separated.Page = 2; 
   statistics.Run(image1Separated); 
   image1Saturation = statistics.Mean; 
 
   // Separate the second image, and calculate the average values 
   colorSeparate.Run(image2); 
   double image2Hue, image2Saturation; 
   using RasterImage image2Separated = colorSeparate.DestinationImage; 
    
   image2Separated.Page = 1; 
   statistics.Run(image2Separated); 
   image2Hue = statistics.Mean; 
   image2Separated.Page = 2; 
   statistics.Run(image2Separated); 
   image2Saturation = statistics.Mean; 
 
   // Calculate the hue difference 
   // Note: 0 is next to 255, so need to compensate for this 
   double hueDiff = Math.Abs(image1Hue - image2Hue); 
   if (hueDiff > 127) 
      hueDiff = Math.Abs(hueDiff - 255); 
 
   // Log the hue similarity 
   double similarity = 1.0 - (hueDiff / 255.0); 
   Console.WriteLine($"Average hue values: {100.0 * similarity:0.00}% match"); 
 
   // Log the saturation similarity 
   similarity = 1.0 - (Math.Abs(image1Saturation - image2Saturation) / 255.0); 
   Console.WriteLine($"Average saturation values: {100.0 * similarity:0.00}% match"); 
} 

Handling Streams

To use MemoryStream with this tutorial, replace the existing code in the Main() method with the following:

C#
static void Main(string[] args) 
{ 
   string image1Filename = @"FILEPATH TO FIRST IMAGE"; 
   string image2Filename = @"FILEPATH TO SECOND IMAGE"; 
   try 
   { 
      InitLEAD(); 
      Console.WriteLine(); 
      // Use Memory Stream 
      using (RasterCodecs rasterCodecs = new RasterCodecs()) 
      { 
            // RasterImages are IDisposable so they must be in using blocks if not calling .Dispose() later 
            byte[] bytes1 = File.ReadAllBytes(image1Filename); 
            byte[] bytes2 = File.ReadAllBytes(image2Filename); 
            MemoryStream memoryStream1 = new MemoryStream(bytes1); 
            MemoryStream memoryStream2 = new MemoryStream(bytes2); 
            using (RasterImage image1 = rasterCodecs.Load(memoryStream1)) 
            using (RasterImage image2 = rasterCodecs.Load(memoryStream2)) 
            { 
 
               Console.WriteLine($"Image 1: {image1}"); 
               Console.WriteLine($"Image 2: {image2}"); 
               Console.WriteLine(); 
 
               CompareXOR(image1, image2); 
               CompareIntensities(image1, image2); 
               CompareHSV(image1, image2); 
            } 
      } 
   } 
   catch (Exception ex) 
   { 
      Console.WriteLine(ex.Message); 
      Console.WriteLine(ex.StackTrace); 
   } 
   Console.WriteLine(); 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(true); 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application performs multiple calculations on the two images outputting the similarity statistics to the console.

Wrap-Up

This tutorial showed how to use various Image Processing commands including the CombineCommand, GrayscaleCommand, StatisticsInformationCommand, ColorSeparateCommand, and SizeCommand, to obtain similarity statistics of two images.

See Also

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

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