CogOCRMaxChar ClassCognex VisionPro 9.5
This class contains data associated with characters in OCRMax. This includes the character's character code, the character's image, rectangles that describe where the character appears in the image, and other data.
Inheritance Hierarchy

System Object
  System MarshalByRefObject
    Cognex.VisionPro.Implementation CogObjectBase
      Cognex.VisionPro.Implementation CogSerializableObjectBase
        Cognex.VisionPro.Implementation CogSerializableChangedEventBase
          Cognex.VisionPro.OCRMax CogOCRMaxChar

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

[SerializableAttribute]
public class CogOCRMaxChar : CogSerializableChangedEventBase

The CogOCRMaxChar type exposes the following members.

Constructors

  NameDescription
Public methodCogOCRMaxChar 
Construct a default CogOCRMaxChar object.
Public methodCogOCRMaxChar(CogOCRMaxChar)
Constructs a deep copy of the supplied CogOCRMaxChar object.
Protected methodCogOCRMaxChar(SerializationInfo, StreamingContext)
Serialization constructor for CogOCRMaxChar.
Top
Methods

  NameDescription
Public methodCreateObjRef
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
(Inherited from MarshalByRefObject.)
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize
Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.
(Inherited from Object.)
Protected methodGetAttributes
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Public methodStatic memberGetCharCodeFromString(String)
Returns the UTF 32-bit charcode that represents the letter in the input string.
Public methodStatic memberGetCharCodeFromString(String, String)
Returns the UTF 32-bit charcode that represents the letter in the input string.
Public methodStatic memberGetCharCodesFromString(String)
Returns an array of UTF 32-bit character codes that represent the input string.
Public methodStatic memberGetCharCodesFromString(String, String)
Returns an array of UTF 32-bit character codes that represent the input string.
Protected methodGetClassName
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetComponentName
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetConverter
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetDefaultEvent
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetDefaultProperty
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetEditor
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetEvents 
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetEvents( Attribute )
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetLifetimeService
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
(Inherited from MarshalByRefObject.)
Protected methodGetObjectData
Implements the GetObjectData method of the ISerializable interface.
(Inherited from CogSerializableObjectBase.)
Protected methodGetProperties 
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetProperties( Attribute )
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetPropertyOwner
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Public methodGetString 
Returns a string which represents this CogOCRMaxChar.
Public methodGetString(String)
Returns a string which represents this CogOCRMaxChar.
Public methodStatic memberGetStringFromCharCode
Returns the String representation of the UTF 32-bit charCode.
Public methodStatic memberGetStringFromCharCodes
Returns the String representation of the UTF 32-bit character codes.
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodInitializeLifetimeService
Obtains a lifetime service object to control the lifetime policy for this instance.
(Inherited from MarshalByRefObject.)
Protected methodMemberwiseClone 
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodMemberwiseClone(Boolean)
Creates a shallow copy of the current MarshalByRefObject object.
(Inherited from MarshalByRefObject.)
Protected methodOnChanged(Int64)
This method should be called internally whenever the object's state may have changed.
(Inherited from CogSerializableChangedEventBase.)
Protected methodOnChanged(CogChangedEventArgs)
This method may be called internally whenever a derived object's state may have changed and the derived object expresses this change via a derived CogChangedEventArgs class.
(Inherited from CogSerializableChangedEventBase.)
Public methodResumeAndRaiseChangedEvent
Re-enables raising of the Changed event after SuspendChangedEvent has been called, and raises the Changed event if the ChangedEventSuspended count is reduced to zero and any changes were made while events were suspended. Must be called once for each call to SuspendChangedEvent.
(Inherited from CogSerializableChangedEventBase.)
Public methodSuspendChangedEvent
Temporarily suspends the raising of the Changed event. May be called more than once, and a corresponding call to ResumeAndRaiseChangedEvent must be made for each call to SuspendChangedEvent.
(Inherited from CogSerializableChangedEventBase.)
Public methodToString
Returns a String that represents the current Object.
(Inherited from Object.)
Top
Fields

  NameDescription
Public fieldStatic memberSfCharacterCode
This bit will be set in the EventArgs of a Changed event every time the value returned by CharacterCode may have changed.
Public fieldStatic memberSfFont
This bit will be set in the EventArgs of a Changed event every time the value returned by Font may have changed.
Public fieldStatic memberSfGetString
This bit will be set in the EventArgs of a Changed event every time the value returned by GetString may have changed.
Public fieldStatic memberSfKey
This bit will be set in the EventArgs of a Changed event every time the value returned by Key may have changed.
Protected fieldStatic memberSfNextSf
Public fieldStatic memberSfPolarity
This bit will be set in the EventArgs of a Changed event every time the value returned by Polarity may have changed.
Top
Properties

  NameDescription
Public propertyBinarizedImage
The Binarized image for this character.
Public propertyChangedEventSuspended
If nonzero, indicates that the raising of the Changed event has been suspended. This value is incremented when SuspendChangedEvent is called and decremented when ResumeAndRaiseChangedEvent is called.
(Inherited from CogSerializableChangedEventBase.)
Public propertyCharacterCode
The character code, which is a Unicode UTF-32 code point. This can be set to any Int32 value even though not all Int32 values are valid UTF-32 code points.
Public propertyFont
A reference to the CogOCRMaxFont of which this character is a member.
Public propertyHasChanged (Inherited from CogSerializableChangedEventBase.)
Public propertyImage
A Rectified Image of the character. A Rectified Image is an image of the character where the character is neither rotated nor skewed
Public propertyInvert
The invert value used to binarize the normalized image.
Public propertyKey
The key for this character. The key contains information that uniquely identifies this character from other characters when it the CogOCRMaxChar is a member of a CogOCRMaxFont
Public propertyMetrics
The metrics for this character.
Public propertyNormalizedImage
The Normalized image for this character.
Public propertyPolarity
The Polarity of the character.
Public propertyStateFlags
Returns the complete set of state flags supported on this object. The flags may be indexed by name as shown in the following C# code snippet: if (changedObject.StateFlags["Color"] & eventArgs.StateFlags) { ... }
(Inherited from CogSerializableChangedEventBase.)
Public propertyThreshold
The threshold value used to binarize the normalized image.
Top
Events

  NameDescription
Public eventChanged
This event is raised when one or more parts of the object's state may have changed.
(Inherited from CogSerializableChangedEventBase.)
Top
Remarks

The CogOCRMaxChar object contains an Image of the character as well as the character's Key and Metrics. The character code is a UTF-32 code point which determines which 'letter' the CogOCRMaxChar corresponds to. The Metrics contain two regions (the mark rectangle and the cell rectangle) which define where in the image the character is located. The CogOCRMaxChar class contains additional information as well, but the primary information about a character is contained within the rectified image, its mark rectangle, its cell rectangle, and the 32-bit character code.

Users are not typically expected to create instances of CogOCRMaxChar directly. Instances of CogOCRMaxChar are usually created by calling the GetCharacter() method of a CogOCRMaxPositionResult.

The CogOCRMaxChar.CharacterCode may be set to a special unknown character code (0xF800) to indicate that the character code is 'Unknown'.

The CogOCRMaxChar class also appears as the output of CogOCRMaxSegmenter, the input to CogOCRMaxClassifier, and the members of a CogOCRMaxFont.

Adding a CogOCRMaxChar to a CogOCRMaxFont will cause the CogOCRMaxChar's Key to be automatically replaced by a new CogOCRMaxCharKey to ensure that the Key's Instance number is unique.

Examples

Imports System.Drawing
Imports Cognex.VisionPro
Imports Cognex.VisionPro.OCRMax

Module CogOCRMaxExample

  Sub Main()

    Dim OCRMaxTool As New CogOCRMaxTool()

    ' Load an image containing the text to read.
    Dim bmp As New Bitmap(System.Environment.GetEnvironmentVariable("VPRO_ROOT") & "\images\alphanumbers.bmp")

    Dim image As New CogImage8Grey(bmp)

    ' Create a rectangle that surrounds the text 
    ' "ABCDEFGHIJKLMNOPQRSTUVWXYZ" in the image.
    Dim rect As New CogRectangleAffine()
    rect.SetOriginLengthsRotationSkew(340, 748, 1010, 93, 0, 0)

    ' Set the image and search region of the tool.
    OCRMaxTool.InputImage = image
    OCRMaxTool.Region = rect

    ' Segment the characters in the image.
    OCRMaxTool.Run()

    If OCRMaxTool.RunStatus.Result <> CogToolResultConstants.Accept OrElse OCRMaxTool.LineResult.Count <> 26 Then
      Console.WriteLine("Initial segmentation failed. " + OCRMaxTool.RunStatus.Message)
      Return
    End If

    ' Get an array of Unicode UTF-32 character codes
    ' which represent a string of all the letters in the 
    ' english alphabet.
    Dim alphabetString As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim alphabetCharCodes As Integer() = CogOCRMaxChar.GetCharCodesFromString(alphabetString)

    ' Label each segmented character with the correct
    ' character code and add it to the classifier's font.
    For i As Integer = 0 To OCRMaxTool.LineResult.Count - 1
      Dim c As CogOCRMaxChar = OCRMaxTool.LineResult(i).GetCharacter()
      c.CharacterCode = alphabetCharCodes(i)
      OCRMaxTool.Classifier.Font.Add(c)
    Next

    Try
      ' Train the classifier based on the font.
      OCRMaxTool.Classifier.Train()
    Catch ex As Exception
      Console.WriteLine("Failed to train classifier characters: " & ex.Message)
    End Try

    ' Run the (now trained) tool again.
    OCRMaxTool.Run()

    If OCRMaxTool.RunStatus.Result <> CogToolResultConstants.Accept Then
      Console.WriteLine("OCRMax tool failed: " + OCRMaxTool.RunStatus.Message)
      Return
    End If

    If OCRMaxTool.LineResult.Status <> CogOCRMaxLineResultStatusConstants.Read Then
      Console.WriteLine("OCRMax tool failed to read the line of text.")
      Return
    End If

    ' Print out the read string.
    Console.WriteLine("OCRMax Tool read the string: " + OCRMaxTool.LineResult.ResultString)
  End Sub
End Module
using System;
using System.Drawing;
using Cognex.VisionPro;
using Cognex.VisionPro.OCRMax;

class CogOCRMaxExample
{
  static void Main()
  {
    CogOCRMaxTool OCRMaxTool = new CogOCRMaxTool();

    // Load an image containing the text to read.
    Bitmap bmp = new Bitmap(
      System.Environment.GetEnvironmentVariable("VPRO_ROOT") +
      @"\images\alphanumbers.bmp");

    CogImage8Grey image = new CogImage8Grey(bmp);

    // Create a rectangle that surrounds the text  
    // "ABCDEFGHIJKLMNOPQRSTUVWXYZ" in the image.
    CogRectangleAffine rect = new CogRectangleAffine();
    rect.SetOriginLengthsRotationSkew(340, 748, 1010, 93, 0, 0);

    // Set the image and search region of the tool.
    OCRMaxTool.InputImage = image;
    OCRMaxTool.Region = rect;

    // Segment the characters in the image.
    OCRMaxTool.Run();

    if (OCRMaxTool.RunStatus.Result != CogToolResultConstants.Accept ||
        OCRMaxTool.LineResult.Count != 26)
    {
      Console.WriteLine(
        "Initial segmentation failed. " + OCRMaxTool.RunStatus.Message);
      return;
    }

    // Get an array of Unicode UTF-32 character codes 
    // which represent a string of all the letters in the  
    // english alphabet. 
    string alphabetString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int[] alphabetCharCodes =
      CogOCRMaxChar.GetCharCodesFromString(alphabetString);

    // Label each segmented character with the correct 
    // character code and add it to the classifier's font. 
    for (int i = 0; i < OCRMaxTool.LineResult.Count; i++)
    {
      CogOCRMaxChar c = OCRMaxTool.LineResult[i].GetCharacter();
      c.CharacterCode = alphabetCharCodes[i];
      OCRMaxTool.Classifier.Font.Add(c);
    }

    try
    {
      // Train the classifier based on the font.
      OCRMaxTool.Classifier.Train();
    }
    catch (Exception ex)
    {
      Console.WriteLine(
        "Failed to train classifier characters: " + ex.Message);
    }

    // Run the (now trained) tool again.
    OCRMaxTool.Run();

    if (OCRMaxTool.RunStatus.Result != CogToolResultConstants.Accept)
    {
      Console.WriteLine("OCRMax tool failed: " + OCRMaxTool.RunStatus.Message);
      return;
    }

    if (OCRMaxTool.LineResult.Status != CogOCRMaxLineResultStatusConstants.Read)
    {
      Console.WriteLine("OCRMax tool failed to read the line of text.");
      return;
    }

    // Print out the read string.
    Console.WriteLine("OCRMax Tool read the string: " + OCRMaxTool.LineResult.ResultString);
  }
}
See Also