VintaSoft Imaging .NET SDK 15.1: Documentation for .NET developer
In This Topic
Forms processing: Identification of document image
In This Topic
Sometimes it is necessary to identify a document type without recognition of document data. For example, it may be necessary to separate the images with invoices and tax returns. The SDK allows to do that job using TemplateMatchingCommand class.


The TemplateMatchingCommand class allows to compare the document image with one or more images of document templates and determine the template that matches that document. During the identification SDK calculates a transformation matrix that determines the translation, rotation and scale of document image relative to the image of document template. Also during the identification SDK calculates the confidence to the results of identification of document image.


The TemplateMatchingCommand class would identify the image of document even if image is rotated or scaled comparing to the image of document template. The class would NOT identify the document image, if image is dimensionally distorted or warped comparing to the document template.

Here is C#/VB.NET code that demonstrates how to determine a type of document (invoice or tax return) on image.
/// <summary>
/// Identifies the type of document.
/// </summary>
/// <param name="invoiceTemplateFilename">The invoice template filename.</param>
/// <param name="taxReturnTemplateFilename">The tax return template filename.</param>
/// <param name="documentFilename">The document filename.</param>
public static void IdentifyTypeOfDocument(
    string invoiceTemplateFilename,
    string taxReturnTemplateFilename,
    string documentFilename)
{
    // create new template matching command
    Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand templateMatchingCommand = 
        new Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand();
    // open invoice template
    Vintasoft.Imaging.VintasoftImage invoiceTemplateImage = 
        new Vintasoft.Imaging.VintasoftImage(invoiceTemplateFilename);
    templateMatchingCommand.TemplateImages.Add(invoiceTemplateImage);
    // open tax return template
    Vintasoft.Imaging.VintasoftImage taxReturnTemplateImage = 
        new Vintasoft.Imaging.VintasoftImage(taxReturnTemplateFilename);
    templateMatchingCommand.TemplateImages.Add(taxReturnTemplateImage);
    // open test image
    using (Vintasoft.Imaging.VintasoftImage testImage = 
        new Vintasoft.Imaging.VintasoftImage(documentFilename))
    {
        // execute template matching
        templateMatchingCommand.ExecuteInPlace(testImage);
    }
    // if image is recognized
    if (templateMatchingCommand.Result.ImageCompareResult.IsReliable)
    {
        // write matching template type
        if (templateMatchingCommand.Result.TemplateImage == invoiceTemplateImage)
            System.Console.WriteLine("Document is an invoice.");
        else
            System.Console.WriteLine("Document is a tax return.");
    }
    else
    {
        System.Console.WriteLine("Document type is not recognized.");
    }
    // dispose template images
    templateMatchingCommand.TemplateImages.ClearAndDisposeItems();
}
''' <summary>
''' Identifies the type of document.
''' </summary>
''' <param name="invoiceTemplateFilename">The invoice template filename.</param>
''' <param name="taxReturnTemplateFilename">The tax return template filename.</param>
''' <param name="documentFilename">The document filename.</param>
Public Shared Sub IdentifyTypeOfDocument(invoiceTemplateFilename As String, taxReturnTemplateFilename As String, documentFilename As String)
    ' create new template matching command
    Dim templateMatchingCommand As New Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand()
    ' open invoice template
    Dim invoiceTemplateImage As New Vintasoft.Imaging.VintasoftImage(invoiceTemplateFilename)
    templateMatchingCommand.TemplateImages.Add(invoiceTemplateImage)
    ' open tax return template
    Dim taxReturnTemplateImage As New Vintasoft.Imaging.VintasoftImage(taxReturnTemplateFilename)
    templateMatchingCommand.TemplateImages.Add(taxReturnTemplateImage)
    ' open test image
    Using testImage As New Vintasoft.Imaging.VintasoftImage(documentFilename)
        ' execute template matching
        templateMatchingCommand.ExecuteInPlace(testImage)
    End Using
    ' if image is recognized
    If templateMatchingCommand.Result.ImageCompareResult.IsReliable Then
        ' write matching template type
        If templateMatchingCommand.Result.TemplateImage Is invoiceTemplateImage Then
            System.Console.WriteLine("Document is an invoice.")
        Else
            System.Console.WriteLine("Document is a tax return.")
        End If
    Else
        System.Console.WriteLine("Document type is not recognized.")
    End If
    ' dispose template images
    templateMatchingCommand.TemplateImages.ClearAndDisposeItems()
End Sub


Create an imprint of form template and completed form, identify image of completed form by comparing imprints of form template and completed form

The process of image comparison includes the creation of imprints of compared images and actually the comparison of these image imprints. The SDK implements 2 algorithms of image imprint creation:
Also the SDK allows to specify a user-defined algorithm for creation of image imprints.

Here is C#/VB.NET code that demonstrates how to identify a document image using algorithm that creates an image imprint based on lines.
/// <summary>
/// Identifies the image using key lines.
/// </summary>
/// <param name="templateImages">The template images.</param>
/// <param name="testImage">The test image.</param>
/// <returns>Result of template matching.</returns>
public static Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingResult IdentifyUsingLines(
    Vintasoft.Imaging.VintasoftImage[] templateImages, 
    Vintasoft.Imaging.VintasoftImage testImage)
{
    // create a recognizer
    Vintasoft.Imaging.FormsProcessing.TemplateMatching.KeyLineRecognizerCommand lineRecognizerCommand = 
        new Vintasoft.Imaging.FormsProcessing.TemplateMatching.KeyLineRecognizerCommand();
    // set units of measure
    lineRecognizerCommand.UnitOfMeasure = Vintasoft.Imaging.UnitOfMeasure.Pixels;
    // set max length of line
    lineRecognizerCommand.MaxLength = 2000;
    // create a template matching command
    Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand templateMatchingCommand = 
        new Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand();
    // set imprint generator based on recognizer
    templateMatchingCommand.ImageImprintGenerator =
        new Vintasoft.Imaging.FormsProcessing.TemplateMatching.ImageImprintGeneratorCommand(lineRecognizerCommand);
    // add template images
    templateMatchingCommand.TemplateImages.AddRange(templateImages);
    // execute template matching
    templateMatchingCommand.ExecuteInPlace(testImage);
    // return result
    return templateMatchingCommand.Result;
}
''' <summary>
''' Identifies the image using key lines.
''' </summary>
''' <param name="templateImages">The template images.</param>
''' <param name="testImage">The test image.</param>
''' <returns>Result of template matching.</returns>
Public Shared Function IdentifyUsingLines(templateImages As Vintasoft.Imaging.VintasoftImage(), testImage As Vintasoft.Imaging.VintasoftImage) As Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingResult
    ' create a recognizer
    Dim lineRecognizerCommand As New Vintasoft.Imaging.FormsProcessing.TemplateMatching.KeyLineRecognizerCommand()
    ' set units of measure
    lineRecognizerCommand.UnitOfMeasure = Vintasoft.Imaging.UnitOfMeasure.Pixels
    ' set max length of line
    lineRecognizerCommand.MaxLength = 2000
    ' create a template matching command
    Dim templateMatchingCommand As New Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand()
    ' set imprint generator based on recognizer
    templateMatchingCommand.ImageImprintGenerator = New Vintasoft.Imaging.FormsProcessing.TemplateMatching.ImageImprintGeneratorCommand(lineRecognizerCommand)
    ' add template images
    templateMatchingCommand.TemplateImages.AddRange(templateImages)
    ' execute template matching
    templateMatchingCommand.ExecuteInPlace(testImage)
    ' return result
    Return templateMatchingCommand.Result
End Function

Here is C#/VB.NET code that demonstrates how to identify a document image using algorithm that creates an image imprint based on L search patterns.
/// <summary>
/// Identifies the image using key marks.
/// </summary>
/// <param name="templateImages">The template images.</param>
/// <param name="testImage">The test image.</param>
/// <returns>Result of template matching.</returns>
public static Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingResult IdentifyUsingMarks(
    Vintasoft.Imaging.VintasoftImage[] templateImages, 
    Vintasoft.Imaging.VintasoftImage testImage)
{
    // create a recognizer
    Vintasoft.Imaging.FormsProcessing.TemplateMatching.KeyMarkRecognizerCommand markRecognizerCommand = 
        new Vintasoft.Imaging.FormsProcessing.TemplateMatching.KeyMarkRecognizerCommand();
    // set units of measure
    markRecognizerCommand.UnitOfMeasure = Vintasoft.Imaging.UnitOfMeasure.Pixels;
    // set max size of mark
    markRecognizerCommand.MaxMarkSize = 300;
    // set min size of mark
    markRecognizerCommand.MinMarkSize = 50;
    // create a template matching command
    Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand templateMatchingCommand = 
        new Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand();
    // set imprint generator based on recognizer
    templateMatchingCommand.ImageImprintGenerator =
        new Vintasoft.Imaging.FormsProcessing.TemplateMatching.ImageImprintGeneratorCommand(markRecognizerCommand);
    // add template images
    templateMatchingCommand.TemplateImages.AddRange(templateImages);
    // execute template matching
    templateMatchingCommand.ExecuteInPlace(testImage);
    // return result
    return templateMatchingCommand.Result;
}
''' <summary>
''' Identifies the image using key marks.
''' </summary>
''' <param name="templateImages">The template images.</param>
''' <param name="testImage">The test image.</param>
''' <returns>Result of template matching.</returns>
Public Shared Function IdentifyUsingMarks(templateImages As Vintasoft.Imaging.VintasoftImage(), testImage As Vintasoft.Imaging.VintasoftImage) As Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingResult
    ' create a recognizer
    Dim markRecognizerCommand As New Vintasoft.Imaging.FormsProcessing.TemplateMatching.KeyMarkRecognizerCommand()
    ' set units of measure
    markRecognizerCommand.UnitOfMeasure = Vintasoft.Imaging.UnitOfMeasure.Pixels
    ' set max size of mark
    markRecognizerCommand.MaxMarkSize = 300
    ' set min size of mark
    markRecognizerCommand.MinMarkSize = 50
    ' create a template matching command
    Dim templateMatchingCommand As New Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand()
    ' set imprint generator based on recognizer
    templateMatchingCommand.ImageImprintGenerator = New Vintasoft.Imaging.FormsProcessing.TemplateMatching.ImageImprintGeneratorCommand(markRecognizerCommand)
    ' add template images
    templateMatchingCommand.TemplateImages.AddRange(templateImages)
    ' execute template matching
    templateMatchingCommand.ExecuteInPlace(testImage)
    ' return result
    Return templateMatchingCommand.Result
End Function


Align image of completed form to match its form template

When image of completed form has been identified there may be necessary to align completed form image to its form template. This can be done using the TemplateAligningCommand class.

Here is C#/VB.NET code that demonstrates how to identify an image of completed form and align the completed form image to its form template.
/// <summary>
/// Identifies and aligns the image.
/// </summary>
/// <param name="templateImages">The template images.</param>
/// <param name="testImage">The test image.</param>
public static void IdentifyAndAlign(
    Vintasoft.Imaging.VintasoftImage[] templateImages, 
    Vintasoft.Imaging.VintasoftImage testImage)
{
    // create template matching command
    Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand templateMatchingCommand = 
        new Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand();
    // add template images
    templateMatchingCommand.TemplateImages.AddRange(templateImages);
    // execute template matching
    templateMatchingCommand.ExecuteInPlace(testImage);
    // create template aligning command
    Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateAligningCommand templateAligningCommand = 
        new Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateAligningCommand();
    // set matching result
    templateAligningCommand.CompareResult = templateMatchingCommand.Result.ImageCompareResult;
    // execute template aligning
    templateAligningCommand.ExecuteInPlace(testImage);
}
''' <summary>
''' Identifies and aligns the image.
''' </summary>
''' <param name="templateImages">The template images.</param>
''' <param name="testImage">The test image.</param>
Public Shared Sub IdentifyAndAlign(templateImages As Vintasoft.Imaging.VintasoftImage(), testImage As Vintasoft.Imaging.VintasoftImage)
    ' create template matching command
    Dim templateMatchingCommand As New Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateMatchingCommand()
    ' add template images
    templateMatchingCommand.TemplateImages.AddRange(templateImages)
    ' execute template matching
    templateMatchingCommand.ExecuteInPlace(testImage)
    ' create template aligning command
    Dim templateAligningCommand As New Vintasoft.Imaging.FormsProcessing.TemplateMatching.TemplateAligningCommand()
    ' set matching result
    templateAligningCommand.CompareResult = templateMatchingCommand.Result.ImageCompareResult
    ' execute template aligning
    templateAligningCommand.ExecuteInPlace(testImage)
End Sub