In This Topic
Programming / OCR / Choosing the appropriate OCR context

Choosing the appropriate OCR context

In This Topic

OCR context is very easy to set and use, yet very important in the OCR process.
Often on your forms, you do not want to recognize all the text in the document, but only small parts of it, whether it’s a column in a table, a name on a forum, or additional notes area. You have to inform the OCR engine of the layout type of the data you want to process using OCR.

Copy Code
'We assume that GdPicture has been correctly installed and unlocked.

'We assume a GdViewer object called GdViewer1 has been created and painted on the form.

Dim oGdPictureImaging As New GdPictureImaging()

'Loading the image from a file.

Dim imageId As Integer = oGdPictureImaging.CreateGdPictureImageFromFile("image.tif")

'Checking if the image has been loaded correctly.

If oGdPictureImaging.GetStat() <> GdPictureStatus.OK Then

    MessageBox.Show("The image can't be loaded. Error: " + oGdPictureImaging.GetStat().ToString(), "OCR Context Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

Else

    'Displaying the image in the GdViewer.

    GdViewer1.DisplayFromGdPictureImage(imageId)

    oGdPictureImaging.ReleaseGdPictureImage(imageId)

End If

oGdPictureImaging.Dispose()

'On the Mouse UP event on the GdViewer, set the ROI based on the rectangle of selection of the GdViewer.

'If a rectangle of selection is painted on the GdViewer, the area will be OCRed with a single line OCR context.

Public Sub OCR_ROI_MouseUP(eventSender As System.Object, eventArgs As System.EventArgs)

    Dim oGdPictureOCR As GdPictureOCR = New GdPictureOCR()

    'Initializing variables to hold the position of the rectangle of selection on the document.

    Dim leftArea As Integer = 0

    Dim topArea As Integer = 0

    Dim widthArea As Integer = 0

    Dim heightArea As Integer = 0

    'Checking if a rectangle of selection has been painted on the GdViewer.

    If GdViewer1.IsRect() Then

        'Getting the location of the selection on the document.

        GdViewer1.GetRectCoordinatesOnDocument(leftArea, topArea, widthArea, heightArea)

        'Setting the ROI on the document.

        oGdPictureOCR.SetROI(leftArea, topArea, widthArea, heightArea)

    Else

        oGdPictureOCR.ResetROI()

    End If

    'Processing the OCR:

    'Selecting the OCR context, here we will use it to recognize a single line on our form.

    oGdPictureOCR.Context = OCRContext.OCRContextSingleLine

    'Setting up the required parameters for the OCR engine.

    oGdPictureOCR.ResourceFolder = "Redist\OCR"

    If (oGdPictureOCR.AddLanguage(OCRLanguage.English) = GdPictureStatus.OK) AndAlso

        (oGdPictureOCR.SetImage(imageId) = GdPictureStatus.OK) Then

        'Running the OCR.

        Dim resID As String = oGdPictureOCR.RunOCR()

        'Checking the status.

        If oGdPictureOCR.GetStat() <> GdPictureStatus.OK Then

            MessageBox.Show("The OCR has failed with the status: " + oGdPictureOCR.GetStat().ToString(), "OCR Context Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Else

            'Checking the results.

            For i As Integer = 1 To oGdPictureOCR.GetCharacterCount(resID)

                oGdPictureImaging.DrawRectangle(imageId,

                    oGdPictureOCR.GetCharacterLeft(resID, i) + leftArea,

                    oGdPictureOCR.GetCharacterTop(resID, i) + topArea,

                    oGdPictureOCR.GetCharacterRight(resID, i) - oGdPictureOCR.GetCharacterLeft(resID, i),

                    oGdPictureOCR.GetCharacterBottom(resID, i) - oGdPictureOCR.GetCharacterTop(resID, i),

                    1, Color.Red, False)



            Next

        End If

    End If

    oGdPictureOCR.Dispose()

End Sub
Copy Code
//We assume that GdPicture has been correctly installed and unlocked.

//We assume a GdViewer object called GdViewer1 has been created and painted on the form.

GdPictureImaging oGdPictureImaging = new GdPictureImaging();

//Loading the image from a file.

int imageId = oGdPictureImaging.CreateGdPictureImageFromFile("image.tif");

//Checking if the image has been loaded correctly.

if (oGdPictureImaging.GetStat() != GdPictureStatus.OK)

{

    MessageBox.Show("The image can't be loaded. Error: " + oGdPictureImaging.GetStat().ToString(), "OCR Context Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

else

{

    //Displaying the image in the GdViewer.

    GdViewer1.DisplayFromGdPictureImage(imageId);

    oGdPictureImaging.ReleaseGdPictureImage(imageId);

}

oGdPictureImaging.Dispose();



//On the Mouse UP event on the GdViewer, set the ROI based on the rectangle of selection of the GdViewer.

//If a rectangle of selection is painted on the GdViewer, the area will be OCRed with a single line OCR context.

public void OCR_ROI_MouseUP(System.Object eventSender, System.EventArgs eventArgs)

{

    GdPictureOCR oGdPictureOCR = new GdPictureOCR();

    //Initializing variables to hold the position of the rectangle of selection on the document.

    int leftArea = 0;

    int topArea = 0;

    int widthArea = 0;

    int heightArea = 0;

    //Checking if a rectangle of selection has been painted on the GdViewer.

    if (GdViewer1.IsRect())

    {

        //Getting the location of the selection on the document.

        GdViewer1.GetRectCoordinatesOnDocument(ref leftArea, ref topArea, ref widthArea, ref heightArea);

        //Setting the ROI on the document.

        oGdPictureOCR.SetROI(leftArea, topArea, widthArea, heightArea);

    }

    else

    {

        oGdPictureOCR.ResetROI();

    }

    //Processing the OCR:

    //Selecting the OCR context, here we will use it to recognize a single line on our form.

    oGdPictureOCR.Context = OCRContext.OCRContextSingleLine;

    //Setting up the required parameters for the OCR engine.

    oGdPictureOCR.ResourceFolder = "Redist\\OCR";

    if ((oGdPictureOCR.AddLanguage(OCRLanguage.English) == GdPictureStatus.OK) &&

        (oGdPictureOCR.SetImage(imageId) == GdPictureStatus.OK))

    {

        //Running the OCR.

        string resID = oGdPictureOCR.RunOCR();

        //Checking the status.

        if (oGdPictureOCR.GetStat() != GdPictureStatus.OK)

        {

            MessageBox.Show("The OCR has failed with the status: " + oGdPictureOCR.GetStat().ToString(), "OCR Context Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        else

        {

            //Checking the results.

            for (int i = 1; i <= oGdPictureOCR.GetCharacterCount(resID); i++)

            {

                oGdPictureImaging.DrawRectangle(imageId,

                    oGdPictureOCR.GetCharacterLeft(resID, i) + leftArea,

                    oGdPictureOCR.GetCharacterTop(resID, i) + topArea,

                    oGdPictureOCR.GetCharacterRight(resID, i) - oGdPictureOCR.GetCharacterLeft(resID, i),

                    oGdPictureOCR.GetCharacterBottom(resID, i) - oGdPictureOCR.GetCharacterTop(resID, i),

                    1, Color.Red, false);



            }

        }

    }

    oGdPictureOCR.Dispose();

}