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 an example that demonstrates how to determine a type of document (invoice or tax return) on image.
' The project, which uses this code, must have references to the following assemblies: ' - Vintasoft.Imaging ' - Vintasoft.Imaging.FormsProcessing ''' <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
// The project, which uses this code, must have references to the following assemblies: // - Vintasoft.Imaging // - Vintasoft.Imaging.FormsProcessing /// <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(); }
' The project, which uses this code, must have references to the following assemblies: ' - Vintasoft.Imaging ' - Vintasoft.Imaging.FormsProcessing ''' <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
// The project, which uses this code, must have references to the following assemblies: // - Vintasoft.Imaging // - Vintasoft.Imaging.FormsProcessing /// <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; }
' The project, which uses this code, must have references to the following assemblies: ' - Vintasoft.Imaging ' - Vintasoft.Imaging.FormsProcessing ''' <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
// The project, which uses this code, must have references to the following assemblies: // - Vintasoft.Imaging // - Vintasoft.Imaging.FormsProcessing /// <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; }
' The project, which uses this code, must have references to the following assemblies: ' - Vintasoft.Imaging ' - Vintasoft.Imaging.FormsProcessing ''' <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
// The project, which uses this code, must have references to the following assemblies: // - Vintasoft.Imaging // - Vintasoft.Imaging.FormsProcessing /// <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); }