Класс ImageComparisonCommand
В этом разделе
Создает изображение в оттенках серого, представляющее собой результат сравнения двух изображений.
Объектная модель
Синтаксис
Пример
Вот C#/VB.NET код, который демонстрирует, как сравнить два изображения и выделить результат красным цветом.
''' <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 = Vintasoft.Imaging.VintasoftImageGdiExtensions.OpenGraphics(image1)
Vintasoft.Imaging.VintasoftImageGdiExtensions.Draw(differences, differencesGraphics, New System.Drawing.Rectangle(0, 0, differences.Width, differences.Height))
Vintasoft.Imaging.VintasoftImageGdiExtensions.CloseGraphics(image1)
' 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 = Vintasoft.Imaging.VintasoftImageGdiExtensions.OpenGraphics(image1);
Vintasoft.Imaging.VintasoftImageGdiExtensions.Draw(differences,
differencesGraphics,
new System.Drawing.Rectangle(0, 0, differences.Width, differences.Height));
Vintasoft.Imaging.VintasoftImageGdiExtensions.CloseGraphics(image1);
// 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;
}
Иерархия наследования
System.Object
 Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase
   Vintasoft.Imaging.ImageProcessing.ImageComparisonCommand
Требования
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
Смотрите также