OCR: How to recognize CIJ characters from image in .NET
In This Topic
Because CIJ (Continuous Inkjet) characters are formed by tiny droplets, the Tesseract OCR engine struggles to find continuous edges.
If you want to recognize text, which contains CIJ characters, please process image before text recognition and "convert" CIJ characters into text characters with smooth lines.
CIJ characters can be "converted" into text characters with smooth lines using the morphological filters - first, dilate symbols on image (
DilateCommand class); next, erode symbols on image (
ErodeCommand class).
Here is C#/VB.NET code that shows how to "convert" text with CIJ characters into text characters with smooth lines and recognize text using Tesseract OCR engine:
/// <summary>
/// Recognizes CIJ characters from image using Tesseract OCR engine.
/// </summary>
/// <param name="filename">The name of file, which stores images with CIJ characters.</param>
public static void RecognizeCIJCharactersUsingTesseractOCR(string filename)
{
// create an image collection
using (Vintasoft.Imaging.ImageCollection images =
new Vintasoft.Imaging.ImageCollection())
{
// add images from file to the image collection
images.Add(filename);
System.Console.WriteLine("Create Tesseract OCR engine...");
// create the Tesseract OCR engine
using (Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr tesseractOcr =
new Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr())
{
System.Console.WriteLine("Initialize OCR engine...");
// specify that English text should be recognized
Vintasoft.Imaging.Ocr.OcrLanguage language = Vintasoft.Imaging.Ocr.OcrLanguage.English;
// create the Tesseract OCR settings
Vintasoft.Imaging.Ocr.Tesseract.TesseractOcrSettings settings =
new Vintasoft.Imaging.Ocr.Tesseract.TesseractOcrSettings(language);
// specify that image contains single text block
settings.RecognitionRegionType = Vintasoft.Imaging.Ocr.RecognitionRegionType.RecognizeSingleBlock;
// init the Tesseract OCR engine
tesseractOcr.Init(settings);
// for each image in image collection
foreach (Vintasoft.Imaging.VintasoftImage image in images)
{
System.Console.WriteLine("Recognize the image...");
// dilate image
DilateCommand dilateCommand = new DilateCommand();
dilateCommand.WindowSize = 5;
dilateCommand.ExecuteInPlace(image);
// erode image
ErodeCommand erodeCommand = new ErodeCommand();
erodeCommand.WindowSize = 3;
erodeCommand.ExecuteInPlace(image);
// recognize text in image
Vintasoft.Imaging.Ocr.Results.OcrPage ocrResult = tesseractOcr.Recognize(image);
// output the recognized text
System.Console.WriteLine("Page Text:");
System.Console.WriteLine(ocrResult.GetText());
System.Console.WriteLine();
}
// shutdown the Tesseract OCR engine
tesseractOcr.Shutdown();
}
// free images
images.ClearAndDisposeItems();
}
}
''' <summary>
''' Recognizes CIJ characters from image using Tesseract OCR engine.
''' </summary>
''' <param name="filename">The name of file, which stores images with CIJ characters.</param>
Public Shared Sub RecognizeCIJCharactersUsingTesseractOCR(filename As String)
' create an image collection
Using images As New Vintasoft.Imaging.ImageCollection()
' add images from file to the image collection
images.Add(filename)
System.Console.WriteLine("Create Tesseract OCR engine...")
' create the Tesseract OCR engine
Using tesseractOcr As New Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr()
System.Console.WriteLine("Initialize OCR engine...")
' specify that English text should be recognized
Dim language As Vintasoft.Imaging.Ocr.OcrLanguage= Vintasoft.Imaging.Ocr.OcrLanguage.English
' create the Tesseract OCR settings
Dim settings As Vintasoft.Imaging.Ocr.Tesseract.TesseractOcrSettings =
New Vintasoft.Imaging.Ocr.Tesseract.TesseractOcrSettings(language)
' specify that image contains single text block
settings.RecognitionRegionType = Vintasoft.Imaging.Ocr.RecognitionRegionType.RecognizeSingleBlock
' init the Tesseract OCR engine
tesseractOcr.Init(settings)
' for each image in image collection
For Each image As Vintasoft.Imaging.VintasoftImage In images
System.Console.WriteLine("Recognize the image...")
' dilate image
Dim dilateCommand As DilateCommand = New DilateCommand()
dilateCommand.WindowSize = 5
dilateCommand.ExecuteInPlace(image)
' erode image
Dim erodeCommand As ErodeCommand = New ErodeCommand()
erodeCommand.WindowSize = 3
erodeCommand.ExecuteInPlace(image)
' recognize text in image
Dim ocrResult As Vintasoft.Imaging.Ocr.Results.OcrPage = tesseractOcr.Recognize(image)
' output the recognized text
System.Console.WriteLine("Page Text:")
System.Console.WriteLine(ocrResult.GetText())
System.Console.WriteLine()
Next
' shutdown the Tesseract OCR engine
tesseractOcr.Shutdown()
End Using
' free images
images.ClearAndDisposeItems()
End Using
End Sub