How to Fix Image Colors for Reading in C# | IronOCR

How to Fix Image Colors for Reading in C#

Fix image colors in C# using IronOCR's binarization, grayscale, inversion, and color replacement methods to improve text readability and OCR accuracy. Read specific text colors for targeted extraction.

Quickstart: Isolate Specific Text Colors

Use IronOCR's SelectTextColor method to focus OCR on specific text colors—no complex image manipulation required. Load an image, choose the text color and tolerance, and extract only that text for accurate OCR results.

```cs:title=Fix Text Color Fast with IronOCR new IronTesseract().Read(new IronOcr.OcrImageInput("sample.jpg").SelectTextColor(new IronSoftware.Drawing.Color("#DB645C"), 60));


<div class="hsg-featured-snippet">
    <h3>Minimal Workflow (5 steps)</h3>
    <ol>
        <li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://nuget.org/packages/IronOcr/">Download a C# library to correct image colors</a></li>
        <li>Import the PDF document and images for reading</li>
        <li>Apply color effects: binarization, grayscale, inversion, and color replacement</li>
        <li>Export the corrected image for viewing</li>
        <li>Read specific text colors using <code>SelectTextColor</code></li>
    </ol>
</div>

<br class="clear">

## How Do I Binarize an Image to Improve OCR?

Binarization converts images to two-color format, typically black and white. This separates text from background and reduces noise, making text more distinct and easier to read.

### Why Does Binarization Improve OCR Accuracy?

Apply binarization using the `Binarize` method. OCR works best with high-contrast images featuring black text on white backgrounds. This method creates clear distinction between background and characters.

Binarization excels with scanned documents having uneven lighting or background noise. The algorithm analyzes images and determines optimal thresholds to separate foreground text from background. For advanced preprocessing techniques, see the [OCR Image Optimization Filters guide](https://ironsoftware.com/csharp/ocr/examples/ocr-image-filters-for-net-tesseract/).

### What Is the Code Implementation for Binarization?

```csharp
:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-binarize-image.cs

For batch processing, combine binarization with other filters. Use the Filter Wizard to determine the best filter combination for your images.

How Can I Export and Compare Results?

Export modified images using the SaveAsImages method. Compare images before and after binarization below.

Sample image with red and black text on blue-gray background showing OCR challenges before binarization
Binarized version showing clear black text on pure white background for optimal OCR processing

How Do I Convert Images to Grayscale for Better Reading?

Converting images to grayscale reduces visual clutter and improves readability. This helps when original colors distract from content.

When Should I Use Grayscale Over Other Methods?

Apply grayscale using the ToGrayScale method. The process averages R, G, and B values.

Grayscale works well with colored backgrounds or watermarks that interfere with text recognition. Unlike binarization, grayscale preserves detail in images with subtle variations. Use it for photographs with embedded text or complex layouts. For low-quality scans, see Fixing Low Quality Scans & Images.

What Is the Simple Code for Grayscale Conversion?

:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-grayscale-image.cs
// Apply grayscale affect
imageInput.ToGrayScale();
$vbLabelText   $csharpLabel

For varied image qualities, combine grayscale with other preprocessing steps. The OcrInput Class documentation explains chaining multiple filters.

How Does Grayscale Compare to the Original?

Sample text with red headers and black body text showing multiple colors before grayscale conversion
Grayscale document with clear text about tech executives, demonstrating improved readability after conversion

When Should I Invert Image Colors?

Inverting colors enhances contrast. Converting white text on black backgrounds to black text on white backgrounds improves readability.

How Do I Implement Color Inversion?

Use the Invert method to invert colors. Pass a boolean value to remove color channels and return grayscale.

Color inversion handles negative images or dark-themed screenshots. Modern applications use dark mode interfaces that challenge traditional OCR. Inverting these images ensures optimal recognition. For various image types, see the Fast OCR Configuration guide.

:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-invert-image.cs
// Apply invert affect
imageInput.Invert();
$vbLabelText   $csharpLabel

For batch processing with mixed document types, implement automatic dark background detection. Computer Vision features in IronOCR identify when inversion is needed.

What Is the Difference Between Inversion Options?

Compare the Invert method with and without grayscale:

Color-inverted document maintaining original color channels with reversed values
Business text with inverted colors showing white text on dark background demonstrating color inversion effect

How Can I Replace Specific Colors in an Image?

Replace specific colors to highlight or de-emphasize elements. Use this to improve text prominence or correct problematic contrasts.

Why Is Tolerance Important in Color Replacement?

The ReplaceColor method requires the current color, new color, and tolerance value. Higher tolerance handles blurry images better.

Tolerance determines how closely pixels must match your target color. Low values (0-50) suit uniform colors. Higher values (100-200) handle anti-aliased text or compression artifacts. This helps with scanned documents where ink bleeding or paper texture creates variations.

How Do I Implement Color Replacement?

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

How Do I Handle Advanced Color Replacement Scenarios?

Chain multiple replacements for complex scenarios:

/* :path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-multiple-replacements.cs */
// Replace multiple colors in sequence
using var imageInput = new OcrImageInput("multi-color-document.jpg");

// Replace red text with black
imageInput.ReplaceColor(IronSoftware.Drawing.Color.Red, IronSoftware.Drawing.Color.Black, 70);

// Replace blue headers with dark gray
imageInput.ReplaceColor(IronSoftware.Drawing.Color.Blue, IronSoftware.Drawing.Color.DarkGray, 60);

// Replace light yellow background with white
imageInput.ReplaceColor(new IronSoftware.Drawing.Color("#FFFACD"), IronSoftware.Drawing.Color.White, 40);

// Perform OCR on the cleaned image
var result = ocrTesseract.Read(imageInput);
/* :path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-multiple-replacements.cs */
// Replace multiple colors in sequence
using var imageInput = new OcrImageInput("multi-color-document.jpg");

// Replace red text with black
imageInput.ReplaceColor(IronSoftware.Drawing.Color.Red, IronSoftware.Drawing.Color.Black, 70);

// Replace blue headers with dark gray
imageInput.ReplaceColor(IronSoftware.Drawing.Color.Blue, IronSoftware.Drawing.Color.DarkGray, 60);

// Replace light yellow background with white
imageInput.ReplaceColor(new IronSoftware.Drawing.Color("#FFFACD"), IronSoftware.Drawing.Color.White, 40);

// Perform OCR on the cleaned image
var result = ocrTesseract.Read(imageInput);
$vbLabelText   $csharpLabel

For complex color replacements, see Image Correction Filters for additional preprocessing techniques.

What Are the Visual Results of Color Replacement?

Text sample with orange-red colored names and content on blue background showing original colors before replacement
Document after color replacement showing dark cyan headers replacing original orange-red text

How Do I Read Only Specific Text Colors?

Read specific text colors using the SelectTextColor method. Specify the target color and tolerance (0-255). Tolerance represents allowable differences between pixel and selected color for R, G, and B values.

How Does Color Tolerance Affect Results?

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

Use selective color reading for forms with color-coded sections, extracting highlighted text, or reading specific warning messages. For structured data extraction, see OcrResult Class for advanced processing.

What Output Can I Expect from Color Selection?

The OCR result reads only orange-colored text:

Debug console showing OCR extracted orange text: MASAYOSHI SON and YASUMITSU SHIGETA from business document

How Do I Save Modified PDFs with Applied Filters?

IronOCR saves modified PDFs with or without filters. The SaveAsSearchablePdf method's second parameter specifies whether to save with filters applied.

What Is the Implementation for Searchable PDFs?

:path=/static-assets/ocr/content-code-examples/how-to/image-quality-correction-searchable-pdf.cs
using IronOcr;

var ocr = new IronTesseract();
var ocrInput = new OcrInput();

// Load a PDF file
ocrInput.LoadPdf("invoice.pdf");

// Apply gray scale filter
ocrInput.ToGrayScale();
OcrResult result = ocr.Read(ocrInput);

// Save the result as a searchable PDF with filters applied
result.SaveAsSearchablePdf("outputGrayscale.pdf", true);
$vbLabelText   $csharpLabel

When Should I Apply Filters to the Saved PDF?

Apply filters when creating cleaner, more readable documents. This helps with archival purposes or sharing documents across different devices. See Create Searchable PDFs by OCR for more information.

For complex documents with tables, explore Read Table in Document for specialized extraction. For high-volume processing, see Multithreaded Tesseract OCR to optimize filter application across multiple documents.

Frequently Asked Questions

How can I fix poor image colors that affect OCR accuracy?

IronOCR provides several color correction methods including binarization, grayscale conversion, inversion, and color replacement. These filters help improve text readability by enhancing contrast between text and background, making it easier for the OCR engine to accurately extract text from images with poor color quality.

What is binarization and when should I use it for OCR?

Binarization converts images to two-color format (typically black and white) using IronOCR's Binarize method. It's particularly effective for scanned documents with uneven lighting or background noise, as it creates clear distinction between text and background by analyzing the image and determining optimal thresholds for separation.

How do I extract only specific colored text from an image?

IronOCR's SelectTextColor method allows you to focus OCR on specific text colors without complex image manipulation. Simply specify the target color and tolerance level - for example, SelectTextColor(new IronSoftware.Drawing.Color("#DB645C"), 60) - to extract only text in that color range.

When should I convert images to grayscale instead of using binarization?

Use IronOCR's ToGrayScale method when dealing with colored backgrounds or watermarks that interfere with text recognition. Unlike binarization, grayscale preserves detail in images with subtle variations, making it ideal when you need to maintain image quality while reducing visual clutter.

Can I export corrected images to view the results of color filters?

Yes, IronOCR provides the SaveAsImages method to export modified images after applying color corrections. This allows you to compare before and after results, helping you determine which color correction methods work best for your specific images.

How can I determine the best color correction filter for my images?

IronOCR includes a Filter Wizard tool that helps determine the optimal filter combination for your images. For batch processing, you can combine multiple filters like binarization with other preprocessing techniques to achieve the best OCR results for your document type.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
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
Ready to Get Started?
Nuget Downloads 5,269,558 | Version: 2025.12 just released