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; // Using the IronOCR library for Optical Character Recognition (OCR)

// Instantiate IronTesseract, which provides the OCR functionality.
var ocrTesseract = new IronTesseract();

// Create an OcrInput object with the specified image file.
// The 'using' statement ensures that the resources are automatically disposed of when done.
using (var imageInput = new OcrInput("sample.jpg"))
{
    // Apply a binarization effect to the image to enhance OCR accuracy.
    imageInput.Binarize(); // Binarization improves contrast and can lead to better OCR results.

    // Save the binarized version of the image. Specify a filename to store the modified image.
    imageInput.SaveAsImages("binarize.jpg"); // The processed image is saved as "binarize.jpg".
}
Imports IronOcr ' Using the IronOCR library for Optical Character Recognition (OCR)

' Instantiate IronTesseract, which provides the OCR functionality.
Private ocrTesseract = New IronTesseract()

' Create an OcrInput object with the specified image file.
' The 'using' statement ensures that the resources are automatically disposed of when done.
Using imageInput = New OcrInput("sample.jpg")
	' Apply a binarization effect to the image to enhance OCR accuracy.
	imageInput.Binarize() ' Binarization improves contrast and can lead to better OCR results.

	' Save the binarized version of the image. Specify a filename to store the modified image.
	imageInput.SaveAsImages("binarize.jpg") ' The processed image is saved as "binarize.jpg".
End Using
$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
// Ensure the necessary namespaces are included for image processing.
using System;
using System.Drawing;

// This extension method applies a grayscale effect to an image.
public static class ImageExtensions
{
    /// <summary>
    /// Converts an input image to grayscale.
    /// </summary>
    /// <param name="imageInput">An instance of an Image object representing the image to be converted.</param>
    /// <exception cref="ArgumentNullException">Thrown when the image input is null.</exception>
    public static void ToGrayScale(this Bitmap imageInput)
    {
        // Check if the imageInput is null to prevent null reference exceptions
        if (imageInput == null)
        {
            throw new ArgumentNullException(nameof(imageInput), "The image cannot be null.");
        }

        // Loop through each pixel in the image and apply the grayscale effect
        for (int x = 0; x < imageInput.Width; x++)
        {
            for (int y = 0; y < imageInput.Height; y++)
            {
                // Get the pixel color at (x, y)
                Color pixelColor = imageInput.GetPixel(x, y);

                // Calculate the luminance using a common formula that considers human perception
                // This formula takes into account the different contributions to brightness from R, G, B
                int grayScaleValue = (int)(pixelColor.R * 0.3 + pixelColor.G * 0.59 + pixelColor.B * 0.11);

                // Create a new grayscale color with the same alpha value
                Color grayColor = Color.FromArgb(pixelColor.A, grayScaleValue, grayScaleValue, grayScaleValue);

                // Set the new grayscale color to the pixel
                imageInput.SetPixel(x, y, grayColor);
            }
        }
    }
}

// Note: This code assumes the existence of a 'Bitmap' object with appropriate
// 'GetPixel' and 'SetPixel' methods and 'Width' and 'Height' properties for manipulation.
// Additionally, 'Color' is used to get and set pixel information, which is typical
// when dealing with image processing.
' Ensure the necessary namespaces are included for image processing.
Imports System
Imports System.Drawing

' This extension method applies a grayscale effect to an image.
Public Module ImageExtensions
	''' <summary>
	''' Converts an input image to grayscale.
	''' </summary>
	''' <param name="imageInput">An instance of an Image object representing the image to be converted.</param>
	''' <exception cref="ArgumentNullException">Thrown when the image input is null.</exception>
	<System.Runtime.CompilerServices.Extension> _
	Public Sub ToGrayScale(ByVal imageInput As Bitmap)
		' Check if the imageInput is null to prevent null reference exceptions
		If imageInput Is Nothing Then
			Throw New ArgumentNullException(NameOf(imageInput), "The image cannot be null.")
		End If

		' Loop through each pixel in the image and apply the grayscale effect
		For x As Integer = 0 To imageInput.Width - 1
			For y As Integer = 0 To imageInput.Height - 1
				' Get the pixel color at (x, y)
				Dim pixelColor As Color = imageInput.GetPixel(x, y)

				' Calculate the luminance using a common formula that considers human perception
				' This formula takes into account the different contributions to brightness from R, G, B
				Dim grayScaleValue As Integer = CInt(Math.Truncate(pixelColor.R * 0.3 + pixelColor.G * 0.59 + pixelColor.B * 0.11))

				' Create a new grayscale color with the same alpha value
				Dim grayColor As Color = Color.FromArgb(pixelColor.A, grayScaleValue, grayScaleValue, grayScaleValue)

				' Set the new grayscale color to the pixel
				imageInput.SetPixel(x, y, grayColor)
			Next y
		Next x
	End Sub
End Module

' Note: This code assumes the existence of a 'Bitmap' object with appropriate
' 'GetPixel' and 'SetPixel' methods and 'Width' and 'Height' properties for manipulation.
' Additionally, 'Color' is used to get and set pixel information, which is typical
' when dealing with image processing.
$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
IRON VB CONVERTER ERROR developers@ironsoftware.com
$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 for Optical Character Recognition
IronTesseract ocrTesseract = new IronTesseract();

// Use a using statement to ensure the image is properly disposed of after use
using (var imageInput = new OcrInput("sample.jpg"))
{
    // Define the color to be replaced
    IronSoftware.Drawing.Color currentColor = IronSoftware.Drawing.Color.FromHtml("#DB645C");
    
    // Define the new color that will replace the current color
    IronSoftware.Drawing.Color newColor = IronSoftware.Drawing.Color.DarkCyan;

    // Replace the specified color in the image with the new color
    // The 80 represents the tolerance level, determining how close a color should match to be replaced
    imageInput.ReplaceColor(currentColor, newColor, 80);

    // Export the modified image to a specified directory and with a specified name pattern
    imageInput.SaveAsImages("replaceColor");
}

/* 
Details:
- The `IronTesseract` class is used to create an instance of the IronOCR OCR engine. 
- `OcrInput` here appears to be used more to manipulate an image's color rather than OCR processing.
- The image loaded is "sample.jpg", and it will have a specified color (#DB645C) replaced with DarkCyan.
- The `ReplaceColor` function adjusts color based on a tolerance level; colors similar to the specified color within the tolerance will be altered.
- The modified image(s) are saved with a naming pattern that starts with "replaceColor".
*/
Imports IronOcr

' Instantiate IronTesseract for Optical Character Recognition
Private ocrTesseract As New IronTesseract()

' Use a using statement to ensure the image is properly disposed of after use
Using imageInput = New OcrInput("sample.jpg")
	' Define the color to be replaced
	Dim currentColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.FromHtml("#DB645C")

	' Define the new color that will replace the current color
	Dim newColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.DarkCyan

	' Replace the specified color in the image with the new color
	' The 80 represents the tolerance level, determining how close a color should match to be replaced
	imageInput.ReplaceColor(currentColor, newColor, 80)

	' Export the modified image to a specified directory and with a specified name pattern
	imageInput.SaveAsImages("replaceColor")
End Using

' 
'Details:
'- The `IronTesseract` class is used to create an instance of the IronOCR OCR engine. 
'- `OcrInput` here appears to be used more to manipulate an image's color rather than OCR processing.
'- The image loaded is "sample.jpg", and it will have a specified color (#DB645C) replaced with DarkCyan.
'- The `ReplaceColor` function adjusts color based on a tolerance level; colors similar to the specified color within the tolerance will be altered.
'- The modified image(s) are saved with a naming pattern that starts with "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
// Import necessary namespaces
using IronOcr;
using System;

// The code utilizes the IronTesseract OCR engine to read text from a specified image file.
// This is especially useful for extracting readable content from images where text is prominently displayed.
// Instantiate IronTesseract
var ocrTesseract = new IronTesseract();

// Using statement to ensure the OcrInput is cleaned up after use, freeing any used resources
// It references an image file named "sample.jpg". Ensure the file path is correct and the file exists.
using (var imageInput = new OcrInput("sample.jpg"))
{
    // Define the text color to focus on by using a specific RGB color. 
    // This example sets it to a shade of red with RGB values (219, 100, 92).
    var focusColor = IronSoftware.Drawing.Color.FromRgb(219, 100, 92);

    // Set the color tolerance to 60 which allows for minor variations in the color shade.
    // This is helpful if the text color in the image slightly varies due to lighting or other factors.
    imageInput.SelectTextColor(focusColor, 60);

    // Perform OCR on the image input using the IronTesseract engine
    var ocrResult = ocrTesseract.Read(imageInput);

    // Output the text extracted from the image to the console
    Console.WriteLine(ocrResult.Text);
}
' Import necessary namespaces
Imports IronOcr
Imports System

' The code utilizes the IronTesseract OCR engine to read text from a specified image file.
' This is especially useful for extracting readable content from images where text is prominently displayed.
' Instantiate IronTesseract
Private ocrTesseract = New IronTesseract()

' Using statement to ensure the OcrInput is cleaned up after use, freeing any used resources
' It references an image file named "sample.jpg". Ensure the file path is correct and the file exists.
Using imageInput = New OcrInput("sample.jpg")
	' Define the text color to focus on by using a specific RGB color. 
	' This example sets it to a shade of red with RGB values (219, 100, 92).
	Dim focusColor = IronSoftware.Drawing.Color.FromRgb(219, 100, 92)

	' Set the color tolerance to 60 which allows for minor variations in the color shade.
	' This is helpful if the text color in the image slightly varies due to lighting or other factors.
	imageInput.SelectTextColor(focusColor, 60)

	' Perform OCR on the image input using the IronTesseract engine
	Dim ocrResult = ocrTesseract.Read(imageInput)

	' Output the text extracted from the image to the console
	Console.WriteLine(ocrResult.Text)
End Using
$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 OCR.

How can I get started with IronOCR for image color correction?

To get started with IronOCR for image color correction, download the 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.

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.

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.

How can I replace specific colors in an image using IronOCR?

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

Can IronOCR focus on reading text of a specific color?

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 in IronOCR?

The SaveAsImages method 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.

Chaknith related to Read Specific Text Color Example
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.