CogOCRMaxTool AutoCorrect Method Cognex VisionPro

For use with OCRMAXTool Tuning

AutoCorrect() makes educated guesses at a "correct" tune record by trying different OCRMax tool parameters and looking for parameters that produce a result string which matches the expected result string contained in the input record.

A "correct" record contains an image, a region, and the correct string of character codes in the image. A "correct" record also contains the mark and cell rects which identify the location of each character in the image.

Namespace: Cognex.VisionPro.OCRMax
Assembly: Cognex.VisionPro.OCRMax (in Cognex.VisionPro.OCRMax.dll) Version: 65.1.0.0
Syntax

public CogOCRMaxTuneRecordCollection AutoCorrect(
	CogOCRMaxTuneRecord inputRecord
)

Parameters

inputRecord
Type: Cognex.VisionPro.OCRMax CogOCRMaxTuneRecord
A tune record which contains the image, region, and correct character codes.

Return Value

Type: CogOCRMaxTuneRecordCollection
Returns a set of corrected tune records which have the mark and cell rects filled in to match the input character codes.
Exceptions

ExceptionCondition
ArgumentNullException
  • Thrown if inputRecord is null.
InvalidOperationException
  • Thrown if TuneParams is null.
  • Thrown if TuneData is null.
  • Thrown if Segmenter is null.
  • Thrown if ClassifierRunParams is null.
  • Thrown if Classifier.TrainParams is null.
CogOCRMaxInvalidTuneRecordException
  • Thrown if the inputRecord has an unallocated image
  • Thrown if inputRecord contains no character codes
  • Thrown if inputRecord character codes contains an unknown character marker
  • Thrown if inputRecord has a mark rect which has an angle or skew greater than 45 degrees relative to the region
Remarks

AutoCorrect() is primarily used during tuning to aid the user in creating "correct" tune records that can be then passed to AutoTune(). In many cases calling AutoCorrect() will be easier than manually constructing the corrected record.

AutoCorrect may use data from prior corrected tune records that have been passed to AutoTune() as well as any current tool parameters as hints when trying to find a correct record.

Examples

' Create a new CogOCRMaxTool to demonstrate OCRMax tuning.
Dim myOCRMaxTool As New CogOCRMaxTool()

' Load an image which contains the text "Date:10/01/2009"
Dim ifPath As String = System.Environment.GetEnvironmentVariable("VPRO_ROOT")  & _
                       "\images\OCRMax_dotmatrix_multiline.idb"

Dim imageFileTool As New CogImageFileTool()
imageFileTool.[Operator].Open(ifPath, CogImageFileModeConstants.Read)
imageFileTool.Run()
Dim image As CogImage8Grey = TryCast(imageFileTool.OutputImage, CogImage8Grey)
myOCRMaxTool.InputImage = image

' Set up the OCRMaxTool's Region so that it contains the text
' we wish to read.
Dim region As New CogRectangleAffine()
region.SetCenterLengthsRotationSkew(236.822, 65.6591, 433.645, 43.7727, 0, 0)
myOCRMaxTool.Region = region

' Run the OCRMaxTool on the image
myOCRMaxTool.Run()

' Note that the OCRMaxTool was unable to correctly read the string.

' Here we extract a tune record from the OCRMaxTool's line result.
Dim extractedRecord As CogOCRMaxTuneRecord = _
  myOCRMaxTool.LineResult.CreateTuneRecordFromResult(myOCRMaxTool.InputImage, _
                                                     myOCRMaxTool.Region)

' A collection of UTF-32 character codes is created which represents
' the string contained in the image.
Dim characterCodes As New CogOCRMaxCharKeyCollection("Date:10/01/2009", "?")

' Here we update the extracted tune records with the 
' character codes we expect the tool to read.
extractedRecord.CharacterCodes = characterCodes

' Now use the OCRMaxTool's Auto Correct feature to locate the 
' enclosing cell and mark rectangle of each character.
Dim autoCorrectResult As CogOCRMaxTuneRecordCollection = _
  myOCRMaxTool.AutoCorrect(extractedRecord)

' Here we assume the first autoCorrectResult is correct.
' In practice we would want to display the autoCorrectResults 
' and get confirmation before proceeding to the next step.
Dim correctedRecord As CogOCRMaxTuneRecord = autoCorrectResult(0)

' To display the record call:
' CogGraphicCollection myGraphics = correctRecord.CreateTuneRecordGraphics("?");
' myCogDisplay.Image = correctRecord.Image;
' myCogDisplay.StaticGraphics.AddList(myGraphics, "tuneRecordGraphics");

' Note that if none of the autoCorrectResult records are actually correct, we
' must choose one of the records and manually modify it so that it is correct.
' The process to manually modify a record is more complex and is beyond the 
' scope of this sample.

' Note that if you do manually correct an autoCorrectResult record before
' passing it to AutoTune you must also set the SegmenterCorrected and
' ClassifierCorrected properties of the record to indicate that it was
' manually corrected.

' Once we have a correct record, the OCRMaxTool can be tuned.
' Tuning the OCRMaxTool updates the Segmenter and Classifier.Font
' properties of the OCRMaxTool such that the new settings can 
' successfully read the tune record image (and future images
' which are similar).
myOCRMaxTool.AutoTune(correctedRecord)

' All tuned records are stored in the OCRMaxTool's TuneData property.
' Copies of the tuned records can be accessed like this:      
'   CogOCRMaxTuneRecordCollection tunedRecords = 
'     myOCRMaxTool.TuneData.GetTuneRecords();

' Tune Records can be removed from the OCRMaxTool's TuneData like this:     
'   myOCRMaxTool.TuneData.RemoveTuneRecord(0); 

' After the OCRMaxTool has been tuned it should be able to
' succesfully read the image.
myOCRMaxTool.Run()

' Check to see that the tool now reads the record correctly
Assert.IsTrue(myOCRMaxTool.LineResult.ResultString = "Date:10/01/2009")

' At this point the user would continue running the OCRMaxTool
' on new images until an image which could not be read is encountered.

' When this happens the steps, starting with extracting the tune record
' from the failing result, are repeated.
// Create a new CogOCRMaxTool to demonstrate OCRMax tuning.
CogOCRMaxTool myOCRMaxTool = new CogOCRMaxTool();

// Load an image which contains the text "Date:10/01/2009" 
string ifPath = System.Environment.GetEnvironmentVariable("VPRO_ROOT") +
@"\images\OCRMax_dotmatrix_multiline.idb";

CogImageFileTool imageFileTool = new CogImageFileTool();
imageFileTool.Operator.Open(ifPath, CogImageFileModeConstants.Read);
imageFileTool.Run();
CogImage8Grey image = imageFileTool.OutputImage as CogImage8Grey;
myOCRMaxTool.InputImage = image;

// Set up the OCRMaxTool's Region so that it contains the text 
// we wish to read.
CogRectangleAffine region = new CogRectangleAffine();
region.SetCenterLengthsRotationSkew(236.822, 65.6591, 433.645, 43.7727, 0, 0);
myOCRMaxTool.Region = region;

// Run the OCRMaxTool on the image
myOCRMaxTool.Run();

// Note that the OCRMaxTool was unable to correctly read the string. 

// Here we extract a tune record from the OCRMaxTool's line result.
CogOCRMaxTuneRecord extractedRecord =
myOCRMaxTool.LineResult.CreateTuneRecordFromResult(myOCRMaxTool.InputImage,
myOCRMaxTool.Region);

// A collection of UTF-32 character codes is created which represents 
// the string contained in the image.
CogOCRMaxCharKeyCollection characterCodes =
new CogOCRMaxCharKeyCollection(@"Date:10/01/2009", "?");

// Here we update the extracted tune records with the 
// character codes we expect the tool to read.
extractedRecord.CharacterCodes = characterCodes;

// Now use the OCRMaxTool's Auto Correct feature to locate the 
// enclosing cell and mark rectangle of each character.
CogOCRMaxTuneRecordCollection autoCorrectResult =
myOCRMaxTool.AutoCorrect(extractedRecord);

// Here we assume the first autoCorrectResult is correct. 
// In practice we would want to display the autoCorrectResults 
// and get confirmation before proceeding to the next step.
CogOCRMaxTuneRecord correctedRecord = autoCorrectResult[0];

// To display the record call: 
// CogGraphicCollection myGraphics = correctRecord.CreateTuneRecordGraphics("?"); 
// myCogDisplay.Image = correctRecord.Image; 
// myCogDisplay.StaticGraphics.AddList(myGraphics, "tuneRecordGraphics"); 

// Note that if none of the autoCorrectResult records are actually correct, we 
// must choose one of the records and manually modify it so that it is correct. 
// The process to manually modify a record is more complex and is beyond the 
// scope of this sample. 

// Note that if you do manually correct an autoCorrectResult record before 
// passing it to AutoTune you must also set the SegmenterCorrected and 
// ClassifierCorrected properties of the record to indicate that it was 
// manually corrected. 

// Once we have a correct record, the OCRMaxTool can be tuned. 
// Tuning the OCRMaxTool updates the Segmenter and Classifier.Font 
// properties of the OCRMaxTool such that the new settings can 
// successfully read the tune record image (and future images 
// which are similar).
myOCRMaxTool.AutoTune(correctedRecord);

// All tuned records are stored in the OCRMaxTool's TuneData property. 
// Copies of the tuned records can be accessed like this: 
//   CogOCRMaxTuneRecordCollection tunedRecords = 
//     myOCRMaxTool.TuneData.GetTuneRecords(); 

// Tune Records can be removed from the OCRMaxTool's TuneData like this: 
//   myOCRMaxTool.TuneData.RemoveTuneRecord(0); 

// After the OCRMaxTool has been tuned it should be able to 
// succesfully read the image.
myOCRMaxTool.Run();

// Check to see that the tool now reads the record correctly
Assert.IsTrue(myOCRMaxTool.LineResult.ResultString == "Date:10/01/2009");

// At this point the user would continue running the OCRMaxTool 
// on new images until an image which could not be read is encountered. 

// When this happens the steps, starting with extracting the tune record 
// from the failing result, are repeated.
See Also