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.

First Step:
green arrow pointer



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")
$vbLabelText   $csharpLabel

For convenience, you can export the modified image using the SaveAsImages method. Below is a comparison of the image before and after binarization.

Sample image
Binarized image

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()
$vbLabelText   $csharpLabel
Sample image
Grayscaled image

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()
$vbLabelText   $csharpLabel

The below images show the Invert method with and without the grayscale option.

Inverted image
Inverted and grayscaled image

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")
$vbLabelText   $csharpLabel
Sample image
Replaced color image

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)
$vbLabelText   $csharpLabel

Below, you will find the OCR result, which is intended to read only the text in an orange-ish color.

OCR result

Frequently Asked Questions

What is the purpose of image color correction in OCR?

Image color correction enhances the legibility and quality of an image, making text and content more readable and aesthetically pleasing, which is crucial for accurate text extraction using IronOCR.

How can I get started with image color correction?

To get started with image color correction, download the IronOCR C# library from NuGet, import your images, apply color effects like binarization, grayscale, inversion, or color replacement, and export the corrected image.

What is binarization and how does it help in OCR?

Binarization converts the image into a two-color format, typically black and white, which helps separate text from the background and reduces noise, making text more distinct and easier to read for OCR processes using IronOCR.

How does the grayscale effect improve image readability?

The grayscale effect reduces color distractions by converting the image into various shades of gray, which can make the image less visually cluttered and more reader-friendly, especially when processed by IronOCR.

What is the purpose of inverting image colors?

Inverting image colors enhances contrast by changing color schemes, such as turning white text on a black background into black text on a white background, which can improve readability, particularly with IronOCR.

How can specific colors be replaced in an image?

To replace specific colors, use the IronOCR ReplaceColor method, specifying the current color to replace, the new color, and a tolerance value to adjust the sensitivity of the color change.

Can text of a specific color be focused on during reading?

Yes, IronOCR can focus on reading text of a specific color by using the SelectTextColor method, which enhances OCR accuracy on colored text by specifying the target color and a tolerance value.

What is the function of the SaveAsImages method?

The SaveAsImages method in IronOCR is used to export the modified or corrected image after applying color effects like binarization, grayscale, or inversion, facilitating further processing or viewing.

Why is high contrast important for OCR?

High contrast is important for OCR because it makes the background very distinctive from the characters, enabling more accurate text extraction by the OCR algorithms, especially when using IronOCR.

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.
Reviewed by
Jeff Fritz
Jeffrey T. Fritz
Principal Program Manager - .NET Community Team
Jeff is also a Principal Program Manager for the .NET and Visual Studio teams. He is the executive producer of the .NET Conf virtual conference series and hosts 'Fritz and Friends' a live stream for developers that airs twice weekly where he talks tech and writes code together with viewers. Jeff writes workshops, presentations, and plans content for the largest Microsoft developer events including Microsoft Build, Microsoft Ignite, .NET Conf, and the Microsoft MVP Summit