Guide to using IronOCR Filters

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

Get started with IronOCR

Start using IronOCR in your project today with a free trial.

First Step:
green arrow pointer


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. This may improve OCR performance in cases of very low text-to-background contrast.
    • ToGrayScale - This image filter turns every pixel into shades of gray. Unlikely to improve OCR accuracy but may improve speed.
    • Invert - Inverts every color. E.g. White becomes black and vice versa.
    • ReplaceColor - Replaces a color in an image with another color, within a certain threshold.
  • Filters to 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 from object boundaries. Opposite of Dilate.
  • Filters to reduce Image Noise
    • Sharpen - Sharpens blurred OCR Documents and flattens alpha channels to white.
    • DeNoise - Removes digital noise. This filter should only be used in scenarios where noise is expected.
    • DeepCleanBackgroundNoise - Heavy background noise removal. Only use this filter if extreme document background noise is known, as it might reduce OCR accuracy of clean documents and is CPU intensive.
    • 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.

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-1.cs
// Import the necessary namespaces
using IronOcr;
using System;

// Create an instance of IronTesseract to perform OCR (Optical Character Recognition) operations
var ocr = new IronTesseract();

// Create a new OcrInput object to manage the image input
using var input = new OcrInput();

// Load the image from the specified file path into the OcrInput instance.
// Ensure that the file path is correct and the image exists at this location.
input.AddImage("my_image.png");

// Correct the image's skew, if any, to improve OCR accuracy
input.Deskew();

// Perform OCR on the input image and obtain the result
var result = ocr.Read(input);

// Output the recognized text from the image to the console
Console.WriteLine(result.Text);
$vbLabelText   $csharpLabel

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. This way, you can debug and see exactly what each filter does and how it is manipulating your image.

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-2.cs
using IronOcr;
using System;

// This program processes a specified TIFF image file using Optical Character Recognition (OCR)
// The file is deskewed for better recognition accuracy and the recognized text is output to the console

// Define the file name to be processed by OCR
var file = "skewed_image.tiff";

// Create a new instance of IronTesseract, which will perform the OCR
var ocr = new IronTesseract();

// Create a new OcrInput object for processing input images. The 'using' statement ensures proper disposal of resources.
using var input = new OcrInput();

// Define the page indices to load from the TIFF file. Change these indices based on the actual pages you want to process.
var pageIndices = new int[] { 0, 1 };

// Load the specified image frames from the file. The first index is 0, corresponding to the first page.
input.LoadImageFrames(file, pageIndices);

// Deskew the input to correct any skewness in the scanned images; improves OCR accuracy.
input.Deskew();

// Save the processed images with the Deskew filter applied, for verification or further use
input.SaveAsImages("my_deskewed");

// Perform OCR on the input and obtain the result
var result = ocr.Read(input);

// Print the recognized text to the console
Console.WriteLine(result.Text);
$vbLabelText   $csharpLabel

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 being 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:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-3.cs
using IronOcr;
using System;

// Define the image file path to be processed
var image = "screenshot.png";

// Create an instance of the IronTesseract OCR engine 
var ocr = new IronTesseract();

// Initialize an OcrInput instance to hold the image data
using var input = new OcrInput();

// Load the image into the OcrInput instance
input.AddImage(image);

// Rotate the image 180 degrees, presumably because it is upside-down
input.Rotate(180);

// Process the OcrInput with the IronTesseract engine 
// and store the OCR results in the 'result' variable
var result = ocr.Read(input);

// Output the recognized text from the image to the console
Console.WriteLine(result.Text);
$vbLabelText   $csharpLabel
BeforeInput.Rotate(180)``AfterInput.Rotate(180)``
Screenshot related to Use-Case Code ExampleScreenshot Rotated related to Use-Case Code Example

Deskew

API Reference

Filter Explanation

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

Please 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 define orientation.

Use-Case Code Example

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

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-4.cs
using IronOcr;
using System;

// Define the path to the image that needs OCR processing
var imageFilePath = @"paragraph_skewed.png";

// Create a new instance of IronTesseract for OCR processing
var ocr = new IronTesseract();

// Create an OCR input to load and process the image
using (var input = new OcrInput())
{
    // Load the image specified by the file path into the OCR input
    input.AddImage(imageFilePath);

    // Attempt to deskew the image with a tolerance of up to 15 degrees to correct for any orientation issues
    bool didDeskew = input.Deskew(15);

    // Check if the deskew operation was successful
    if (didDeskew)
    {
        // If deskew was successful, perform OCR on the image
        // Store the OCR result in the variable 'result'
        var result = ocr.Read(input);

        // Output the recognized text to the console
        Console.WriteLine(result.Text);
    }
    else
    {
        // If deskew was not successful, notify the user
        Console.WriteLine("Deskew not applied because the image orientation could not be determined.");
    }
}
$vbLabelText   $csharpLabel
BeforeDeskew()``AfterDeskew()``
Paragraph Skewed related to Use-Case Code ExampleParagraph Deskewed related to Use-Case Code Example

Scale

API Reference

Filter Explanation

Scale is a useful image manipulation filter that helps to resize an image using 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.

Please note
There is a sweet-spot for barcode sizes of 1000px x 1000px where barcodes can be read well, 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:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-5.cs
// Import necessary namespaces
using IronOcr;
using System;

// Define the path to the image containing text or barcodes for OCR processing
var image = @"small_barcode.png";

// Initialize the IronTesseract OCR engine which will perform the OCR scanning
var ocr = new IronTesseract();

// Enable barcode reading in the OCR configuration so that barcodes are detected and decoded
ocr.Configuration.ReadBarCodes = true;

// Create an OcrInput instance which will be used to hold the image(s) to be processed
using var input = new OcrInput();

// Load the image into the OcrInput instance
// This method prepares the image for OCR processing
input.AddImage(image);

// Optionally, scale the image to enhance OCR accuracy
// Here we scale the image by 400% of its original size which can improve recognition quality
input.Scale(scalePercentage: 400);

// Perform OCR on the input image and store the result
// The 'Read' method processes the input and returns recognition results
var result = ocr.Read(input);

// Output the recognized text, including any barcode values, to the console
// The 'Text' property contains all recognized text and barcodes in string format
Console.WriteLine(result.Text);
$vbLabelText   $csharpLabel
BeforeScale()``AfterScale()``
C Sharp Ocr Image Filters 1 related to Use-Case Code ExampleC Sharp Ocr Image Filters 2 related to Use-Case Code Example

Binarize

API Reference

Filter Explanation

The Binarize filter classifies all pixels in an image as either black or white, depending on an adaptive algorithm. This removes all colors and separates the background into a flat white, with anything recognized as text 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:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-6.cs
// Import necessary namespaces
using IronOcr; // For Optical Character Recognition (OCR) using IronTesseract
using System;   // For basic system operations

// Define the image file to be processed
var image = @"no-binarize.jpg";

// Instantiate a new IronTesseract object, which provides OCR functionality
var ocr = new IronTesseract();

// Initiate a new OcrInput object to load and preprocess the image.
// The 'using' statement ensures that the OcrInput resources are disposed of properly after use.
using (var input = new OcrInput())
{
    // Load at least one image into the input object; in this case, it's from a file path
    input.LoadImage(image);

    // Apply Binarize filter to the input image, which enhances the image by converting it to black and white
    input.Binarize();

    // Use the OCR engine to read the processed input image and capture the result in a variable
    var result = ocr.Read(input);

    // Output the recognized text to the console
    Console.WriteLine(result.Text);
}
$vbLabelText   $csharpLabel
BeforeBinarize()``AfterBinarize()``
No Binarize related to Use-Case Code ExampleAfter Binarize related to Use-Case Code Example

Invert

API Reference

Filter Explanation

IronOCR reads best when the image is black text on a white background. The Invert filter is used to achieve this by inverting all colors on an image.

Use-Case Code Example

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

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-7.cs
// Import necessary namespaces
using IronOcr;
using System;

// Define the path to the image file to be processed
var imagePath = @"before-invert.png";

// Initialize a new instance of the IronTesseract OCR engine
var ocrEngine = new IronTesseract();

// Create a disposable OcrInput object to manage image loading and processing
using (var ocrInput = new OcrInput())
{
    // Load the image file into the OCR input
    ocrInput.AddImage(imagePath);

    // Invert the colors of the image for better OCR accuracy
    // (useful if the image has white text on a black background)
    ocrInput.Invert();

    // Process the image using the OCR engine and store the result
    var ocrResult = ocrEngine.Read(ocrInput);

    // Output the recognized text to the console
    Console.WriteLine(ocrResult.Text);
}
$vbLabelText   $csharpLabel
BeforeAfter
Before Invert related to Use-Case Code ExampleAfter Invert related to Use-Case Code Example

Frequently Asked Questions

What are OCR image filters in IronOCR?

OCR image filters in IronOCR are tools that allow you to preprocess images to improve their quality for Optical Character Recognition. These filters can manipulate image orientation, colors, contrast, and noise.

How can image orientation be adjusted using IronOCR?

Image orientation can be adjusted using the Rotate and Deskew filters. Rotate allows you to rotate images by a specified number of degrees, while Deskew automatically straightens images that are slightly tilted.

What is the purpose of the Binarize filter in IronOCR?

The Binarize filter converts all pixels in an image to either black or white, removing colors and flattening the background to improve OCR performance, especially in cases of low text-to-background contrast.

When should the DeepCleanBackgroundNoise filter be used?

The DeepCleanBackgroundNoise filter should be used in scenarios where there is extreme document background noise. It is CPU intensive and may reduce OCR accuracy on clean documents.

Can the Scale filter improve barcode scanning in IronOCR?

Yes, the Scale filter can resize an image, making it useful for enlarging barcodes that are too small to scan effectively. It helps increase the gaps between bars for better scanning accuracy.

What does the Invert filter do in IronOCR?

The Invert filter reverses the colors in an image, turning white into black and vice versa. This is useful for converting images to have black text on a white background, which is optimal for OCR.

How can you save a filtered image for debugging in IronOCR?

You can save a filtered image for debugging by using the SaveAsImages function after applying any desired filters. This allows you to visually inspect the effects of the filters on the image.

What is the function of the Contrast filter in IronOCR?

The Contrast filter automatically increases the contrast of an image, improving OCR speed and accuracy in scans with low contrast between the text and background.

What is the difference between Dilate and Erode filters?

Dilate adds pixels to the boundaries of objects in an image, whereas Erode removes pixels from object boundaries. Both are advanced morphology filters used to enhance the clarity of image features.

How does the EnhanceResolution filter work in IronOCR?

The EnhanceResolution filter improves the resolution of low-quality images, though it is not often needed because IronOCR's OcrInput.MinimumDPI and TargetDPI settings automatically handle low-resolution inputs.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.