How to Fix Image Colors for Reading
Fixing image colors involves several techniques to improve the legibility and quality of an image. IronOcr offers binarization, grayscale, inversion, and color replacement methods to make text and content within an image more readable and aesthetically pleasing, which is particularly important when working with OCR (Optical Character Recognition) to extract text from images. Reading only the selected text color is also possible.
Get started with IronOCR
Start using IronOCR in your project today with a free trial.
How to Fix Image Colors for Reading
- Download a C# library to correct image colors
- Import the PDF document and images for reading
- Apply the desired color effects, such as binarization, grayscale, inversion, and color replacement
- Export the corrected image for viewing
- Read specific text colors using the
SelectTextColor
method
Binarize Image Example
This process converts the image into a two-color format, typically black and white. It's useful for separating text from the background and reducing noise, making the text more distinct and easier to read.
To apply the binarization effect to the image, use the Binarize
method. Since OCR processes work best with the highest contrast image, featuring black text on a white background, this method proves significant in making the background very distinctive from the characters.
:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-binarize-image.cs
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Apply binarize affect
imageInput.Binarize();
// Export the modified image
imageInput.SaveAsImages("binarize.jpg");
Imports IronOcr
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Apply binarize affect
imageInput.Binarize()
' Export the modified image
imageInput.SaveAsImages("binarize.jpg")
For convenience, you can export the modified image using the SaveAsImages
method. Below is a comparison of the image before and after binarization.

Before

After
Grayscale Image Example
Converting the image into various shades of gray can make it less distracting and more reader-friendly. This is especially helpful when the original colors in the image are causing visual clutter.
To apply the grayscale effect to the image, use the ToGrayScale
method. The grayscale process involves taking the average of the R, G, and B values.
:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-grayscale-image.cs
// Apply grayscale affect
imageInput.ToGrayScale();
' Apply grayscale affect
imageInput.ToGrayScale()

Before

After
Invert Image Example
Inverting the colors can enhance contrast. For example, turning white text on a black background into black text on a white background can improve readability.
Use the Invert
method to invert the image color. The method optionally accepts a boolean value, which is used to remove all color channels and return a grayscale image.
:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-invert-image.cs
// Apply invert affect
imageInput.Invert();
' Apply invert affect
imageInput.Invert()
The below images show the Invert method with and without the grayscale option.

Inverted

Inverted & Grayscaled
Replace Color Example
This technique allows you to replace specific colors in the image with other colors, which can help highlight or de-emphasize certain elements. It's often used to make text more prominent or to correct problematic color contrasts.
To use the ReplaceColor
method, specify the current color to be replaced as well as the new color. The third parameter of the method, which corresponds to the tolerance value, is also important. A higher tolerance is required in blurry images to achieve the desired result.
:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-replace-color.cs
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
IronSoftware.Drawing.Color currentColor = new IronSoftware.Drawing.Color("#DB645C");
IronSoftware.Drawing.Color newColor = IronSoftware.Drawing.Color.DarkCyan;
// Replace color
imageInput.ReplaceColor(currentColor, newColor, 80);
// Export the modified image
imageInput.SaveAsImages("replaceColor");
Imports IronOcr
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.jpg")
Private currentColor As New IronSoftware.Drawing.Color("#DB645C")
Private newColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.DarkCyan
' Replace color
imageInput.ReplaceColor(currentColor, newColor, 80)
' Export the modified image
imageInput.SaveAsImages("replaceColor")

Before

After
Read Specific Text Color Example
This feature aims to read only the specified text color. Use the SelectTextColor
method to specify the color for IronOcr to focus on, along with the tolerance value. The tolerance value accepts a range of 0-255, which represents the allowable difference between pixel color and the selected color for each R, G, and B value in the color space.
:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-select-text-color.cs
using IronOcr;
using System;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Text color to focus on
IronSoftware.Drawing.Color focusColor = new IronSoftware.Drawing.Color("#DB645C");
// Specify which text color to read
imageInput.SelectTextColor(focusColor, 60);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Output result to console
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports System
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Text color to focus on
Private focusColor As New IronSoftware.Drawing.Color("#DB645C")
' Specify which text color to read
imageInput.SelectTextColor(focusColor, 60)
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output result to console
Console.WriteLine(ocrResult.Text)
Below, you will find the OCR result, which is intended to read only the text in an orange-ish color.

Frequently Asked Questions
How can I enhance image colors for OCR in .NET C#?
You can enhance image colors for OCR in .NET C# by using IronOCR. Start by downloading the IronOCR library from NuGet, import the image, and apply color correction techniques like binarization, grayscale, and inversion to improve text readability.
What is the role of binarization in image color correction?
Binarization in image color correction converts an image into a black and white format, which helps separate text from the background, reducing noise and making the text more distinct for better OCR results using IronOCR.
How does applying grayscale effects benefit OCR processes?
Applying grayscale effects reduces color distractions by converting the image into shades of gray. This simplification makes the image less cluttered and enhances text readability, which is beneficial for OCR processes with IronOCR.
What are the advantages of inverting image colors for text extraction?
Inverting image colors increases contrast by altering color schemes, such as changing white text on a black background to black text on a white background. This contrast improvement can enhance text extraction accuracy in OCR when using IronOCR.
How can you change specific colors in an image for better OCR results?
To change specific colors in an image for better OCR results, use the ReplaceColor
method in IronOCR. Specify the original color, the new color, and adjust the tolerance value to fine-tune the color replacement.
Is it possible to focus on text of a specific color during OCR?
Yes, you can focus on text of a specific color during OCR by using the SelectTextColor
method in IronOCR. This allows you to specify the target text color and a tolerance value, enhancing OCR accuracy for colored text.
How do you export modified images after color correction?
After applying color correction to images, you can export them using the SaveAsImages
method in IronOCR. This allows you to save the modified images for further processing or viewing.
Why is adjusting image contrast important for OCR accuracy?
Adjusting image contrast is crucial for OCR accuracy because it helps distinguish the text from the background, making characters more prominent and easier for OCR algorithms to extract, particularly when using IronOCR.