How to Correct Images Using Filters for Reading
Image correction filters are digital image processing techniques used to improve the quality and characteristics of an image, particularly for the purpose of enhancing text recognition and extraction. Image correction filters available in IronOcr are sharpen, enhance resolution, denoise, dilate, and erode.
These image correction filters play a critical role in preprocessing images before OCR, as they help optimize the image for accurate text extraction by improving the quality and visibility of the text and reducing unwanted noise or artifacts.
Get started with IronOCR
Start using IronOCR in your project today with a free trial.
How to Correct Images Using Filters for Reading
- Download a C# library for image correction using filters
- Import the PDF document and images for reading
- Apply desired filters, such as sharpening, enhancing resolution, denoising, dilation, and erosion
- Export the corrected image for viewing
- Utilize the
Read
method for OCR processing
Sharpen Filter Example
This filter increases the contrast along the edges in the image, giving them a more defined appearance. It improves the clarity of text and details, making it easier for OCR software to recognize characters.
To apply the sharpen filter, invoke the Sharpen
method of the OcrImageInput object.
:path=/static-assets/ocr/content-code-examples/how-to/image-quality-correction-sharpen-filter.cs
using IronOcr;
// This code demonstrates how to use IronTesseract to apply a sharpen filter
// to an image and save the processed image. The sharpen filter enhances image clarity,
// which can improve the accuracy of Optical Character Recognition (OCR).
// Instantiate IronTesseract for Optical Character Recognition (OCR).
var ocrTesseract = new IronTesseract();
// Load an image into OcrInput. The 'using' statement ensures that the resources are disposed of after usage.
using var imageInput = new OcrInput("sample.jpg");
// Apply a sharpen filter to enhance the image quality. This step is helpful for improving OCR accuracy.
imageInput.Sharpen();
// Save the processed image as a new file with the sharpen filter applied.
// This is useful for verifying the pre-processing steps have been correctly applied.
imageInput.SaveAsImages("sharpen.jpg");
Imports IronOcr
' This code demonstrates how to use IronTesseract to apply a sharpen filter
' to an image and save the processed image. The sharpen filter enhances image clarity,
' which can improve the accuracy of Optical Character Recognition (OCR).
' Instantiate IronTesseract for Optical Character Recognition (OCR).
Private ocrTesseract = New IronTesseract()
' Load an image into OcrInput. The 'using' statement ensures that the resources are disposed of after usage.
Private imageInput = New OcrInput("sample.jpg")
' Apply a sharpen filter to enhance the image quality. This step is helpful for improving OCR accuracy.
imageInput.Sharpen()
' Save the processed image as a new file with the sharpen filter applied.
' This is useful for verifying the pre-processing steps have been correctly applied.
imageInput.SaveAsImages("sharpen.jpg")
For convenience, you can export the filtered image using the SaveAsImages
method. Below is a comparison of the image before and after applying the sharpening filter.

Before

After
Enhance Resolution Filter Example
This filter is designed to increase the pixel density of an image, effectively improving its sharpness and clarity. It can be especially useful for enhancing the legibility of text in low-resolution images.
Invoke the EnhanceResolution
method to apply the enhanced resolution filter. This method accepts the target DPI, with 225 DPI as the default value.
:path=/static-assets/ocr/content-code-examples/how-to/image-quality-correction-enhance-resolution-filter.cs
// <summary>
// This code snippet demonstrates how to apply an enhanced resolution filter
// to an image input object. The method called 'EnhanceResolution' should be
// part of the imageInput instance, which represents an image processing object.
// Ensure that 'imageInput' is an instance of a class that has this method defined.
// </summary>
// Assuming the existence of an 'ImageProcessor' class with an 'EnhanceResolution' method
public class ImageProcessor
{
// Method to enhance the resolution of an image
public void EnhanceResolution()
{
// Implementation of resolution enhancement here
// This could involve complex image processing algorithms
// to improve image clarity and detail.
Console.WriteLine("Resolution enhanced.");
}
}
// Usage
// Create an instance of ImageProcessor
ImageProcessor imageInput = new ImageProcessor();
// Apply enhanced resolution filter
imageInput.EnhanceResolution();
' <summary>
' This code snippet demonstrates how to apply an enhanced resolution filter
' to an image input object. The method called 'EnhanceResolution' should be
' part of the imageInput instance, which represents an image processing object.
' Ensure that 'imageInput' is an instance of a class that has this method defined.
' </summary>
' Assuming the existence of an 'ImageProcessor' class with an 'EnhanceResolution' method
Public Class ImageProcessor
' Method to enhance the resolution of an image
Public Sub EnhanceResolution()
' Implementation of resolution enhancement here
' This could involve complex image processing algorithms
' to improve image clarity and detail.
Console.WriteLine("Resolution enhanced.")
End Sub
End Class
' Usage
' Create an instance of ImageProcessor
Private imageInput As New ImageProcessor()
' Apply enhanced resolution filter
imageInput.EnhanceResolution()

Before

After
Denoise Filter Example
Denoising filters reduce the level of noise or unwanted artifacts in an image. Noise reduction is crucial for OCR as it helps isolate text from background interference, leading to cleaner and more accurate recognition.
To apply the denoise filter, use the DeNoise
method. The default morphology is 2x2. To apply a stronger denoise algorithm, pass 'true' to enable a 3x3 morphology.
:path=/static-assets/ocr/content-code-examples/how-to/image-quality-correction-denoise-filter.cs
// Apply a denoise filter on the imageInput object.
// Ensure that the `imageInput` is a valid object of a class that has a `DeNoise` method.
// Check if the imageInput object is not null to prevent a NullReferenceException.
if (imageInput != null)
{
try
{
// Call the DeNoise method on the imageInput object.
// It is assumed that this method processes the image and applies a noise reduction filter.
imageInput.DeNoise();
// Inform the user that the denoise filter was applied successfully.
Console.WriteLine("Denoise filter applied successfully.");
}
catch (Exception ex)
{
// Handle any potential exceptions that might occur during the denoise process.
// Log the exception message to the console.
Console.WriteLine("An error occurred while applying the denoise filter: " + ex.Message);
}
}
else
{
// Inform the user that the imageInput object is invalid (it is null).
Console.WriteLine("The image input is invalid. Please ensure it is properly initialized before applying filters.");
}
' Apply a denoise filter on the imageInput object.
' Ensure that the `imageInput` is a valid object of a class that has a `DeNoise` method.
' Check if the imageInput object is not null to prevent a NullReferenceException.
If imageInput IsNot Nothing Then
Try
' Call the DeNoise method on the imageInput object.
' It is assumed that this method processes the image and applies a noise reduction filter.
imageInput.DeNoise()
' Inform the user that the denoise filter was applied successfully.
Console.WriteLine("Denoise filter applied successfully.")
Catch ex As Exception
' Handle any potential exceptions that might occur during the denoise process.
' Log the exception message to the console.
Console.WriteLine("An error occurred while applying the denoise filter: " & ex.Message)
End Try
Else
' Inform the user that the imageInput object is invalid (it is null).
Console.WriteLine("The image input is invalid. Please ensure it is properly initialized before applying filters.")
End If

Before

After
Dilate Filter Example
Dilation expands the brighter regions (foreground) in an image. It helps thicken and enhance the text, making it more prominent and easier for OCR software to interpret.
To apply the dilation filter, use the Dilate
method. Default morphology is 2x2, and setting 'true' enables the 3x3 option.
:path=/static-assets/ocr/content-code-examples/how-to/image-quality-correction-dilate-filter.cs
// The following code demonstrates the application of a dilate filter on an image input
// using a hypothetical image processing library.
// Ensure `imageInput` is a valid object of the class that contains the `Dilate` method.
// Typically, this object should be an instance of a class provided by the image
// processing library you are using.
// The `Dilate` method is a morphological operation applied to enhance the prominent
// features within an image. It is commonly used to expand the areas of brighter
// pixels, enlarging the size of objects within an image to increase their visibility.
// Example usage:
imageInput.Dilate(); // Apply a dilation operation on the image, enlarging bright areas.
' The following code demonstrates the application of a dilate filter on an image input
' using a hypothetical image processing library.
' Ensure `imageInput` is a valid object of the class that contains the `Dilate` method.
' Typically, this object should be an instance of a class provided by the image
' processing library you are using.
' The `Dilate` method is a morphological operation applied to enhance the prominent
' features within an image. It is commonly used to expand the areas of brighter
' pixels, enlarging the size of objects within an image to increase their visibility.
' Example usage:
imageInput.Dilate() ' Apply a dilation operation on the image, enlarging bright areas.

Before

After
Erode Filter Example
Erosion reduces the size of bright regions in an image, useful for refining characters and lines, especially when they are thick or distorted.
Use the Erode
method to apply the erode filter. As with previous methods, default morphology is 2x2, and setting 'true' enables 3x3.
:path=/static-assets/ocr/content-code-examples/how-to/image-quality-correction-erode-filter.cs
// Apply an erode filter to the image.
// Erosion is a morphological operation that is often used
// in image processing. The purpose is to reduce noise by
// removing small-scale details from a binary image. This
// process shrinks the foreground objects by stripping away
// the boundary pixels based on a specified structuring element.
// Assuming that `imageInput` is an instance of a class that
// has the `Erode` method implemented. This class should manage
// image processing operations and can have methods supporting
// functionalities such as erosion.
// Check if imageInput is not null to avoid runtime exceptions
if (imageInput != null)
{
// Perform the erosion operation
imageInput.Erode();
}
else
{
// Optionally handle the scenario where imageInput is null
Console.WriteLine("The image input is null, cannot apply erosion.");
}
' Apply an erode filter to the image.
' Erosion is a morphological operation that is often used
' in image processing. The purpose is to reduce noise by
' removing small-scale details from a binary image. This
' process shrinks the foreground objects by stripping away
' the boundary pixels based on a specified structuring element.
' Assuming that `imageInput` is an instance of a class that
' has the `Erode` method implemented. This class should manage
' image processing operations and can have methods supporting
' functionalities such as erosion.
' Check if imageInput is not null to avoid runtime exceptions
If imageInput IsNot Nothing Then
' Perform the erosion operation
imageInput.Erode()
Else
' Optionally handle the scenario where imageInput is null
Console.WriteLine("The image input is null, cannot apply erosion.")
End If

Before

After
Frequently Asked Questions
What are image correction filters in IronOCR?
Image correction filters are digital image processing techniques used to improve the quality and characteristics of an image, enhancing text recognition and extraction. Filters include sharpen, enhance resolution, denoise, dilate, and erode.
How do image correction filters improve OCR accuracy?
Image correction filters optimize images by improving text quality and visibility and reducing noise or artifacts, which enhances the accuracy of OCR text extraction.
How can I apply the sharpen filter using IronOCR?
To apply the sharpen filter, use the `Sharpen` method of the OcrImageInput object. It increases contrast along edges, improving text clarity for OCR.
What is the purpose of the enhance resolution filter?
The enhance resolution filter increases the pixel density of an image, improving sharpness and clarity, particularly beneficial for low-resolution images.
How do I use the denoise filter in IronOCR?
Use the `DeNoise` method to apply the denoise filter, which reduces noise in images. For stronger noise reduction, set the option to 'true' for a 3x3 morphology.
What does the dilate filter do?
The dilate filter expands brighter regions in an image, enhancing the prominence of text, which aids OCR in interpreting characters.
How can I apply the erode filter in IronOCR?
To apply the erode filter, use the `Erode` method. It reduces the size of bright regions, refining thick or distorted characters and lines.
What is the default DPI for the enhance resolution filter?
The default DPI for the enhance resolution filter in IronOCR is 225 DPI, but you can specify any desired DPI as needed.
Can I adjust the strength of the dilation effect?
Yes, when applying the dilate filter, you can enable a stronger 3x3 morphology by setting the option to 'true'.