Guide to using IronOCR Filters

IronOCR has the tools you need to read images that may need some preprocessing in the form of Filters. You can choose from a wide arrange of filters that can manipulate your images to become processable.

List of OCR Image Filters

The following Image filters can really improve performance:

  • Filters to change the Image Orientation
    • Rotate - Rotates images by a number of degrees clockwise. For anti-clockwise, use negative numbers.
    • Deskew - Rotates an image so it is the right way up and orthogonal. This is very useful for OCR because Tesseract's tolerance for skewed scans can be as low as 5 degrees
    • Scale - Scales Ocr Input pages proportionally.
  • Filters to manipulate Image Colors
    • Binarize - This image filter turns every pixel black or white with no middle ground. May Improve OCR performance cases of very low contrast of text to background.
    • ToGrayScale - This image filter turns every pixel into a shade of grayscale. Unlikely to improve OCR accuracy but may improve speed
    • Invert - Inverts every color. E.g. White becomes black : black becomes white.
    • ReplaceColor - Replaces a color within an image with another color with a certain threshold.
  • Filters to try improve Contrast in an Image
    • Contrast - Increases contrast automatically. This filter often improves OCR speed and accuracy in low contrast scans.
    • Dilate - Advanced Morphology. Dilation adds pixels to the boundaries of objects in an image. Opposite of Erode
    • Erode - Advanced Morphology. Erosion removes pixels on object boundaries. Opposite of Dilate
  • Filters to try reduce Image Noise
    • Sharpen - Sharpens blurred OCR Documents and flattens alpha channels to white.
    • DeNoise - Removes digital noise. This filter should only be used where noise is expected.
    • DeepCleanBackgroundNoise - Heavy background noise removal. Only use this filter in case extreme document background noise is known, because this filter will also risk reducing OCR accuracy of clean documents, and is very CPU expensive.
    • EnhanceResolution - Enhances the resolution of low quality images. This filter is not often needed because OcrInput.MinimumDPI and OcrInput.TargetDPI will automatically catch and resolve low resolution inputs.

Filter Example and Usage

In the following example, we demonstrate how to apply filters within your code

using IronOcr;

var Ocr = new IronTesseract();
using (var Input = new OcrInput(@"my_image.png"))
{
    Input.Deskew();
    var result = Ocr.Read(Input);
    Console.WriteLine(result.Text);
}
using IronOcr;

var Ocr = new IronTesseract();
using (var Input = new OcrInput(@"my_image.png"))
{
    Input.Deskew();
    var result = Ocr.Read(Input);
    Console.WriteLine(result.Text);
}
Imports IronOcr

Private Ocr = New IronTesseract()
Using Input = New OcrInput("my_image.png")
	Input.Deskew()
	Dim result = Ocr.Read(Input)
	Console.WriteLine(result.Text)
End Using
VB   C#

Debug Filter / What is the filter doing?

If you are having difficulty with reading images or barcodes within your program. There is a way to save an image of a filtered result within your program. This way you can debug and see exactly what each filter does and how it is manipulating your image.

using IronOcr;

var file = @"skewed_image.tiff";
var Ocr = new IronTesseract();
using (var Input = new OcrInput(file))
{
    // Here we apply the filter: Deskew
    Input.Deskew();

    // Save the input with filter(s) applied
    Input.SaveAsImages("my_deskewed");

    // We read, then print the text to the console
    var result = Ocr.Read(Input);
    Console.WriteLine(result.Text);
}
using IronOcr;

var file = @"skewed_image.tiff";
var Ocr = new IronTesseract();
using (var Input = new OcrInput(file))
{
    // Here we apply the filter: Deskew
    Input.Deskew();

    // Save the input with filter(s) applied
    Input.SaveAsImages("my_deskewed");

    // We read, then print the text to the console
    var result = Ocr.Read(Input);
    Console.WriteLine(result.Text);
}
Imports IronOcr

Private file = "skewed_image.tiff"
Private Ocr = New IronTesseract()
Using Input = New OcrInput(file)
	' Here we apply the filter: Deskew
	Input.Deskew()

	' Save the input with filter(s) applied
	Input.SaveAsImages("my_deskewed")

	' We read, then print the text to the console
	Dim result = Ocr.Read(Input)
	Console.WriteLine(result.Text)
End Using
VB   C#

Filter Use Cases

Rotate

API Reference

Filter Explanation

Rotate is a filter used to manually set a known rotation to an image to get it closest to straight. IronOCR has functionality to run Deskew() however, the degree of tolerance for this is rather narrow and is best used for images that are almost perfectly straight (within 15 degrees or so). For input images that are 90 degrees off, or upside down, we should call Rotate().

Use-Case Code Example

This is an example of calling Rotate to correct an upside-down image:

var image = @"screenshot.png";
var Ocr = new IronTesseract();
using (var Input = new OcrInput(image))
{
    // Rotate 180 degrees because image is upside-down
    Input.Rotate(180);

    // Read image into variable: result
    var result = Ocr.Read(Input);

    // Example print to console
    Console.WriteLine(result.Text);
}
var image = @"screenshot.png";
var Ocr = new IronTesseract();
using (var Input = new OcrInput(image))
{
    // Rotate 180 degrees because image is upside-down
    Input.Rotate(180);

    // Read image into variable: result
    var result = Ocr.Read(Input);

    // Example print to console
    Console.WriteLine(result.Text);
}
Dim image = "screenshot.png"
Dim Ocr = New IronTesseract()
Using Input = New OcrInput(image)
	' Rotate 180 degrees because image is upside-down
	Input.Rotate(180)

	' Read image into variable: result
	Dim result = Ocr.Read(Input)

	' Example print to console
	Console.WriteLine(result.Text)
End Using
VB   C#
Before Input.Rotate(180)After Input.Rotate(180)

Deskew

API Reference

Filter Explanation

Uses a Hough Transform to attempt to straightness an image within a certain degrees of tolerance. This is important for images that are not completely straight because sometimes a tilted document may cause a misread.

Note: This method returns a boolean which is true if the filter was applied, and false if it failed to apply due to not being able to detect image orientation. This will fail if the page has no contents to base orientation off.

Use-Case Code Example

This is an example of calling Deskew to correct a skewed image:

var image = @"paragraph_skewed.png";
var Ocr = new IronTesseract();
using (var Input = new OcrInput(image))
{
    // Apply deskew with 15 degree snap
    bool didDeskew = Input.Deskew(15);

    if (didDeskew)
    {
        // Read image into variable: result
        var result = Ocr.Read(Input);
        Console.WriteLine(result.Text);
    }
    else
    {
        Console.WriteLine("Deskew not applied because Image Orientation could not be determined.");
    }
}
var image = @"paragraph_skewed.png";
var Ocr = new IronTesseract();
using (var Input = new OcrInput(image))
{
    // Apply deskew with 15 degree snap
    bool didDeskew = Input.Deskew(15);

    if (didDeskew)
    {
        // Read image into variable: result
        var result = Ocr.Read(Input);
        Console.WriteLine(result.Text);
    }
    else
    {
        Console.WriteLine("Deskew not applied because Image Orientation could not be determined.");
    }
}
Dim image = "paragraph_skewed.png"
Dim Ocr = New IronTesseract()
Using Input = New OcrInput(image)
	' Apply deskew with 15 degree snap
	Dim didDeskew As Boolean = Input.Deskew(15)

	If didDeskew Then
		' Read image into variable: result
		Dim result = Ocr.Read(Input)
		Console.WriteLine(result.Text)
	Else
		Console.WriteLine("Deskew not applied because Image Orientation could not be determined.")
	End If
End Using
VB   C#
Before Deskew()After Deskew()

Scale

API Reference

Filter Explanation

Scale is a useful image manipulation filter that will help to resize an image with the pixels it already has. This can be used when a barcode is not being scanned because the image is only tens of pixels wide with each bar as one pixel, or if text is too small with no anti-aliasing.

Note: There is a sweet-spot for barcode sizes of 1000px x 1000px which Barcodes can be read well in which should be considered if your barcode is not being found.

Use-Case Code Example

This is an example of calling Scale to enlarge the gaps between bars in a Barcode for scanning:

var image = @"small_barcode.png";
var Ocr = new IronTesseract();

// Optional: This example uses a barcode
Ocr.Configuration.ReadBarCodes = true;

using (var Input = new OcrInput(image))
{
    // Apply scale
    Input.Scale(400); // 400% is 4 times larger

    // Read image into variable: result
    var result = Ocr.Read(Input);

    // Example print to console
    Console.WriteLine(result.Text);
}
var image = @"small_barcode.png";
var Ocr = new IronTesseract();

// Optional: This example uses a barcode
Ocr.Configuration.ReadBarCodes = true;

using (var Input = new OcrInput(image))
{
    // Apply scale
    Input.Scale(400); // 400% is 4 times larger

    // Read image into variable: result
    var result = Ocr.Read(Input);

    // Example print to console
    Console.WriteLine(result.Text);
}
Dim image = "small_barcode.png"
Dim Ocr = New IronTesseract()

' Optional: This example uses a barcode
Ocr.Configuration.ReadBarCodes = True

Using Input = New OcrInput(image)
	' Apply scale
	Input.Scale(400) ' 400% is 4 times larger

	' Read image into variable: result
	Dim result = Ocr.Read(Input)

	' Example print to console
	Console.WriteLine(result.Text)
End Using
VB   C#
Before Scale()After Scale()

Binarize

API Reference

Filter Explanation

The Binarize filter classifies all pixels in an image as either black or white depending on an adaptive algorithm that assesses the image and colors it deems to be the background. When you have an image with many colors but the text can stand out, a binarization filter will remove all the colors and seperate the background into a flat white, and anything it recognizes as text will be colored a full black for easy reading.

Use-Case Code Example

This is an example of calling Binarize to align colored text, and remove background colors and noise:

var image = @"no-binarize.jpg";
var Ocr = new IronTesseract();

using (var Input = new OcrInput(image))
{
    // Apply Binarize
    Input.Binarize();

    // Read image into variable: result
    var result = Ocr.Read(Input);

    // Example print to console
    Console.WriteLine(result.Text);
}
var image = @"no-binarize.jpg";
var Ocr = new IronTesseract();

using (var Input = new OcrInput(image))
{
    // Apply Binarize
    Input.Binarize();

    // Read image into variable: result
    var result = Ocr.Read(Input);

    // Example print to console
    Console.WriteLine(result.Text);
}
Dim image = "no-binarize.jpg"
Dim Ocr = New IronTesseract()

Using Input = New OcrInput(image)
	' Apply Binarize
	Input.Binarize()

	' Read image into variable: result
	Dim result = Ocr.Read(Input)

	' Example print to console
	Console.WriteLine(result.Text)
End Using
VB   C#
Before Binarize()After Binarize()

Invert

API Reference

Filter Explanation

IronOCR reads best when the image read is black text on a white background. When applying a series of filters it is important to try to achieve this result before a read. Invert is a simple filter that inverts all colors on an image. White becomes black, black becomes white, and everything in between flips.

Use-Case Code Example

This is an example of calling Invert to turn white on black into black on white:

var image = @"before-invert.png";
var Ocr = new IronTesseract();

using (var Input = new OcrInput(image))
{
    // Apply Invertc
    Input.Invert(true);

    // Read image into variable: result
    var result = Ocr.Read(Input);

    // Example print to console
    Console.WriteLine(result.Text);
}
var image = @"before-invert.png";
var Ocr = new IronTesseract();

using (var Input = new OcrInput(image))
{
    // Apply Invertc
    Input.Invert(true);

    // Read image into variable: result
    var result = Ocr.Read(Input);

    // Example print to console
    Console.WriteLine(result.Text);
}
Dim image = "before-invert.png"
Dim Ocr = New IronTesseract()

Using Input = New OcrInput(image)
	' Apply Invertc
	Input.Invert(True)

	' Read image into variable: result
	Dim result = Ocr.Read(Input)

	' Example print to console
	Console.WriteLine(result.Text)
End Using
VB   C#
BeforeAfter