Detecting Characters

This code snippet shows how to detect characters in an image, using a few parameters and a topology.

C++

#include <eVision/EasyOcr/EasyOCR2/EOCR2.h>
using namespace eVision;
// Load an Image
EImageBW8 image;
image.Load("image.tif");
// Attach a ROI to the image
EROIBW8 roi;
roi.Attach(&image, 50, 224, 340, 96);
// Create an EOCR2 instance
EOCR2 ocr2;
// Enable the topology usage
ocr2.SetEnabledTopology(true);
// Set the expected character sizes
ocr2.SetCharsWidthRange(EIntegerRange(25, 25));
ocr2.SetCharsHeight(37);
// Set the text polarity, in this case WhiteOnBlack
ocr2.SetTextPolarity(EasyOCR2TextPolarity_WhiteOnBlack);
// Set the topology
ocr2.SetTopology(".{10}\n.{3} .{4}");
// Detect the text in the image. The output Text structure contains:
// - an individual textbox for each character
// - an individual bitmap image for each character
// - a threshold value to binarize the bitmap image for each character
// All structured in a hierarchy with Lines -> Words -> Characters
EOCR2Text text = ocr2.Detect(roi);

C#

using eVision;
// Load an Image
EImageBW8 image = new EImageBW8();
image.Load("image.tif");
// Attach a ROI to the image
EROIBW8 roi = new EROIBW8();
roi.Attach(image, 50, 224, 340, 96);
// Create an EOCR2 instance
EOCR2 ocr2 = new EOCR2();
// Enable the topology usage
ocr2.EnabledTopology = true;
// Set the expected character sizes
ocr2.CharsWidthRange = new EIntegerRange(25, 25);
ocr2.CharsHeight = 37;
// Set the text polarity, in this case WhiteOnBlack
ocr2.TextPolarity = EasyOCR2TextPolarity.WhiteOnBlack;
// Set the topology
ocr2.Topology = ".{10}\n.{3} .{4}";
// Detect the text in the image. The output Text structure contains:
// - an individual textbox for each character
// - an individual bitmap image for each character
// - a threshold value to binarize the bitmap image for each character
// All structured in a hierarchy with Lines –> Words -> Characters
EOCR2Text text = ocr2.Detect(roi);

Python

import eVision
# Load an Image
image = eVision.EImageBW8()
image.Load("image.tif")
# Attach a ROI to the image
roi = eVision.EROIBW8()
roi.Attach(image, 50, 224, 340, 96)
# Create an EOCR2 instance
ocr2 = eVision.EOCR2()
# Enable the topology usage
ocr2.EnabledTopology = True;
# Set the expected character sizes
ocr2.CharsWidthRange = eVision.EIntegerRange(25, 25)
ocr2.CharsHeight = 37
# Set the text polarity, in this case WhiteOnBlack
ocr2.TextPolarity = eVision.EasyOCR2TextPolarity.WhiteOnBlack
# Set the topology
ocr2.Topology = ".{10}\n.{3} .{4}"
# Detect the text in the image. The output Text structure contains:
# - an individual textbox for each character
# - an individual bitmap image for each character
# - a threshold value to binarize the bitmap image for each character
# All structured in a hierarchy with Lines -> Words -> Characters
text = ocr2.Detect(roi)