VintaSoft Imaging .NET SDK v8.7
Vintasoft.Imaging.ImageProcessing Namespace / ImageComparisonCommand Class
Members Example



In This Topic
    ImageComparisonCommand Class
    In This Topic
    Creates a grayscale image, which represents the result of comparing two images.
    Object Model
    ImageComparisonCommand ClassVintasoftImage ClassProcessingCommandResults ClassProcessingCommandResult Class
    Syntax
    'Declaration
     
    Public Class ImageComparisonCommand 
       Inherits ProcessingCommandBase
    public class ImageComparisonCommand : ProcessingCommandBase 
    public __gc class ImageComparisonCommand : public ProcessingCommandBase 
    public ref class ImageComparisonCommand : public ProcessingCommandBase 
    Example
    This example shows how to compare two images and highlight the result with red color.
    ''' <summary>
    ''' Compares two images and highlights the comparison result with red color.
    ''' </summary>
    ''' <param name="image1Filename">The filename of first image.</param>
    ''' <param name="image2Filename">The filename of second image.</param>
    ''' <param name="resultFilename">The filename of result image.</param>
    Public Sub CompareImages(image1Filename As String, image2Filename As String, resultFilename As String)
        ' open the first image
        Using image1 As New Vintasoft.Imaging.VintasoftImage(image1Filename)
            ' open the second image
            Using image2 As New Vintasoft.Imaging.VintasoftImage(image2Filename)
                ' create the image comparison command
                Dim imageComparison As New Vintasoft.Imaging.ImageProcessing.ImageComparisonCommand(image2)
                
                ' get the grayscale result of comparing two images
                Using differences As Vintasoft.Imaging.VintasoftImage = imageComparison.Execute(image1)
                    ' creates the colors for the differences palette with the red color as main color
                    Dim colors As Integer() = CreateDifferencesPaletteColors(System.Drawing.Color.Red)
                    differences.Palette.SetColors(colors)
                
                    ' if pixel format of image1 is not Bgr24 or Bgr32 or Bgra32
                    If image1.PixelFormat <> Vintasoft.Imaging.PixelFormat.Bgr24 AndAlso image1.PixelFormat <> Vintasoft.Imaging.PixelFormat.Bgr32 AndAlso image1.PixelFormat <> Vintasoft.Imaging.PixelFormat.Bgra32 Then
                        ' change pixel format of image1 to Bgr24
                        Dim changeFormat As New Vintasoft.Imaging.ImageProcessing.ChangePixelFormatCommand()
                        changeFormat.PixelFormat = Vintasoft.Imaging.PixelFormat.Bgr24
                        changeFormat.ExecuteInPlace(image1)
                    End If
                
                    ' if image1 and the comparison result image have different sizes
                    If image1.Width <> differences.Width OrElse image1.Height <> differences.Height Then
                        ' crop image1 to the comparison result image size
                        Dim crop As New Vintasoft.Imaging.ImageProcessing.CropCommand()
                        crop.RegionOfInterest = New Vintasoft.Imaging.RegionOfInterest(0, 0, differences.Width, differences.Height)
                        crop.ExecuteInPlace(image1)
                    End If
                
                    ' draw the comparison result on the top of image1
                    Dim differencesGraphics As System.Drawing.Graphics = image1.OpenGraphics()
                    differences.Draw(differencesGraphics, New System.Drawing.Rectangle(0, 0, differences.Width, differences.Height))
                    image1.CloseGraphics()
                
                    ' save the processed image to the new file
                    image1.Save(resultFilename)
                End Using
            End Using
        End Using
    End Sub
                
    ''' <summary>
    ''' Creates the colors for the differences palette.
    ''' </summary>
    ''' <param name="color">Main color for the differences palette.</param>
    ''' <returns>The colors for the differences palette.</returns>
    Private Function CreateDifferencesPaletteColors(color As System.Drawing.Color) As Integer()
        Dim colors As Integer() = New Integer(255) {}
        For i As Integer = 0 To 255
            Dim alpha As Byte = CByte(i)
            colors(i) = (alpha << 24) Or (color.R << 16) Or (color.G << 8) Or color.B
        Next
        Return colors
    End Function
    /// <summary>
    /// Compares two images and highlights the comparison result with red color.
    /// </summary>
    /// <param name="image1Filename">The filename of first image.</param>
    /// <param name="image2Filename">The filename of second image.</param>
    /// <param name="resultFilename">The filename of result image.</param>
    public void CompareImages(string image1Filename, string image2Filename, string resultFilename)
    {
        // open the first image
        using (Vintasoft.Imaging.VintasoftImage image1 = new Vintasoft.Imaging.VintasoftImage(image1Filename))
        {
            // open the second image
            using (Vintasoft.Imaging.VintasoftImage image2 = new Vintasoft.Imaging.VintasoftImage(image2Filename))
            {
                // create the image comparison command
                Vintasoft.Imaging.ImageProcessing.ImageComparisonCommand imageComparison =
                    new Vintasoft.Imaging.ImageProcessing.ImageComparisonCommand(image2);
                
                // get the grayscale result of comparing two images
                using (Vintasoft.Imaging.VintasoftImage differences = imageComparison.Execute(image1))
                {
                    // creates the colors for the differences palette with the red color as main color
                    int[] colors = CreateDifferencesPaletteColors(System.Drawing.Color.Red);
                    differences.Palette.SetColors(colors);
                
                    // if pixel format of image1 is not Bgr24 or Bgr32 or Bgra32
                    if (image1.PixelFormat != Vintasoft.Imaging.PixelFormat.Bgr24 && 
                        image1.PixelFormat != Vintasoft.Imaging.PixelFormat.Bgr32 && 
                        image1.PixelFormat != Vintasoft.Imaging.PixelFormat.Bgra32)
                    {
                        // change pixel format of image1 to Bgr24
                        Vintasoft.Imaging.ImageProcessing.ChangePixelFormatCommand changeFormat = 
                            new Vintasoft.Imaging.ImageProcessing.ChangePixelFormatCommand();
                        changeFormat.PixelFormat = Vintasoft.Imaging.PixelFormat.Bgr24;
                        changeFormat.ExecuteInPlace(image1);
                    }
                
                    // if image1 and the comparison result image have different sizes
                    if (image1.Width != differences.Width || image1.Height != differences.Height)
                    {
                        // crop image1 to the comparison result image size
                        Vintasoft.Imaging.ImageProcessing.CropCommand crop = 
                            new Vintasoft.Imaging.ImageProcessing.CropCommand();
                        crop.RegionOfInterest = 
                            new Vintasoft.Imaging.RegionOfInterest(0, 0, differences.Width, differences.Height);
                        crop.ExecuteInPlace(image1);
                    }
                
                    // draw the comparison result on the top of image1
                    System.Drawing.Graphics differencesGraphics = image1.OpenGraphics();
                    differences.Draw(
                        differencesGraphics, 
                        new System.Drawing.Rectangle(0, 0, differences.Width, differences.Height)); 
                    image1.CloseGraphics();
                
                    // save the processed image to the new file
                    image1.Save(resultFilename);
                }
            }
        }
    }
                
    /// <summary>
    /// Creates the colors for the differences palette.
    /// </summary>
    /// <param name="color">Main color for the differences palette.</param>
    /// <returns>The colors for the differences palette.</returns>
    private int[] CreateDifferencesPaletteColors(System.Drawing.Color color)
    {
        int[] colors = new int[256];
        for (int i = 0; i < 256; i++)
        {
            byte alpha = (byte)i;
            colors[i] = (alpha << 24) | (color.R << 16) | (color.G << 8) | color.B;
        }
        return colors;
    }
    Inheritance Hierarchy

    System.Object
       Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase
          Vintasoft.Imaging.ImageProcessing.ImageComparisonCommand

    Requirements

    Target Platforms: Windows 10, Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also