如何修正C#中的图像颜色以便阅读
使用 IronOCR 的二值化、灰度、反转和颜色替换方法在 C# 中修复图像颜色,以提高文本可读性和 OCR 准确性。 阅读特定的文本颜色,进行有针对性的提取。
快速入门:隔离特定文本颜色使用IronOCR的SelectTextColor方法来专注于特定文本颜色的OCR - 无需复杂的图像处理。 加载图像,选择文本颜色和容差,然后只提取文本以获得准确的 OCR 结果。
-
1使用 IronOCR 安装 NuGet Package Manager
-
2复制并运行这段代码。
new IronTesseract().Read(new IronOcr.OcrImageInput("sample.jpg").SelectTextColor(new IronSoftware.Drawing.Color("#DB645C"), 60));Dim OcrEngine As New IronTesseract() OcrEngine.Read(New IronOcr.OcrImageInput("sample.jpg").SelectTextColor(New IronSoftware.Drawing.Color("#DB645C"), 60))Dim OcrEngine As New IronTesseract() OcrEngine.Read(New IronOcr.OcrImageInput("sample.jpg").SelectTextColor(New IronSoftware.Drawing.Color("#DB645C"), 60)) -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronOCR
如何在 C# 中为阅读修复图像颜色
- 下载C#库以校正图像颜色
- 导入 PDF 文档和图像进行读取
- 应用色彩效果:二值化、灰度、反转和色彩替换
- 导出校正后的图像以供查看
- 使用
SelectTextColor阅读特定文本颜色
如何二值化图像以提高OCR?
二值化将图像转换为双色格式,通常是黑白图像。这样可以将文本与背景分开并减少噪音,使文本更加清晰易读。
为什么二值化能提高OCR准确性?
使用Binarize方法应用二值化。 OCR 对于白底黑字的高对比度图像效果最佳。 这种方法可以明确区分背景和字符。
二值化技术在处理光照不均匀或背景噪音的扫描文档时表现出色。该算法会分析图像并确定最佳阈值,以分离前景文本和背景。 对于高级预处理技术,请参阅OCR图像优化滤镜指南。
二值化的代码实现是什么?
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");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")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")对于批量处理,请将二值化与其他过滤器结合起来。 使用滤镜向导来确定适合您图像的最佳滤镜组合。
如何导出和比较结果?
使用SaveAsImages方法导出修改后的图像。 请比较下面二值化前后的图像。

前

后
如何将图像转换为灰度以改善阅读?
将图像转换为灰度可减少视觉干扰,提高可读性。 当原始颜色分散内容注意力时,这将有所帮助。
我在什么情况下应选择灰度而不是其他方法?
使用ToGrayScale方法应用灰度。 翻译过程中需要计算 R、G 和 B 值的平均值。
如果彩色背景或水印会干扰文本识别,灰度效果会很好。 与二值化不同,灰度可以保留图像中细微变化的细节。 将其用于带有嵌入式文本或复杂布局的照片。 对于低质量扫描,请参阅修复低质量扫描和图像。
灰度转换的简单代码是什么?
// Apply grayscale affect
imageInput.ToGrayScale();// Apply grayscale affect
imageInput.ToGrayScale();' Apply grayscale affect
imageInput.ToGrayScale()' Apply grayscale affect
imageInput.ToGrayScale()为获得不同的图像质量,请将灰度与其他预处理步骤结合起来。 OcrInput Class文档解释了多个滤镜的链接。
灰度与原始图像相比如何?

前

后
我何时应反转图像颜色?
反转颜色可增强对比度。将黑底白字转换为白底黑字可以提高可读性。
如何实现颜色反转?
使用Invert方法反转颜色。 通过布尔值移除颜色通道并返回灰度。
颜色反转可处理负片图像或深色主题截图。 现代应用程序使用暗模式界面,对传统 OCR 提出了挑战。 反转这些图像可确保最佳识别效果。 对于各种图像类型,请参阅快速OCR配置指南。
// Apply invert affect
imageInput.Invert();// Apply invert affect
imageInput.Invert();' Apply invert affect
imageInput.Invert()' Apply invert affect
imageInput.Invert()对于混合文档类型的批量处理,要实现自动深色背景检测。 计算机视觉功能在IronOCR中识别何时需要反转。
反转选项之间有什么区别?
比较Invert方法在有灰度和没有灰度时的效果:

反转

反转和灰度化
我如何替换图像中特定的颜色?
替换特定颜色以突出或淡化元素。 利用这一点来提高文本的突出度或纠正有问题的对比度。
为什么色彩替换中的容差很重要?
ReplaceColor方法需要当前颜色、新颜色和容差值。 更高的宽容度可以更好地处理模糊图像。
容差决定了像素与目标颜色的匹配程度。 低值(0-50)适合统一颜色。 较高的数值(100-200)可处理抗锯齿文本或压缩人工痕迹。 这有助于处理因渗墨或纸张纹理而产生差异的扫描文件。
如何实现颜色替换?
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");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
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sample.jpg")
Dim currentColor As New IronSoftware.Drawing.Color("#DB645C")
Dim newColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.DarkCyan
' Replace color
imageInput.ReplaceColor(currentColor, newColor, 80)
' Export the modified image
imageInput.SaveAsImages("replaceColor")
End UsingImports IronOcr
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sample.jpg")
Dim currentColor As New IronSoftware.Drawing.Color("#DB645C")
Dim newColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.DarkCyan
' Replace color
imageInput.ReplaceColor(currentColor, newColor, 80)
' Export the modified image
imageInput.SaveAsImages("replaceColor")
End Using我怎样处理高级颜色替换场景?
在复杂的情况下,可进行连锁多次替换:
/* :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);Imports IronSoftware.Drawing
' Replace multiple colors in sequence
Using imageInput As New OcrImageInput("multi-color-document.jpg")
' Replace red text with black
imageInput.ReplaceColor(Color.Red, Color.Black, 70)
' Replace blue headers with dark gray
imageInput.ReplaceColor(Color.Blue, Color.DarkGray, 60)
' Replace light yellow background with white
imageInput.ReplaceColor(New Color("#FFFACD"), Color.White, 40)
' Perform OCR on the cleaned image
Dim result = ocrTesseract.Read(imageInput)
End UsingImports IronSoftware.Drawing
' Replace multiple colors in sequence
Using imageInput As New OcrImageInput("multi-color-document.jpg")
' Replace red text with black
imageInput.ReplaceColor(Color.Red, Color.Black, 70)
' Replace blue headers with dark gray
imageInput.ReplaceColor(Color.Blue, Color.DarkGray, 60)
' Replace light yellow background with white
imageInput.ReplaceColor(New Color("#FFFACD"), Color.White, 40)
' Perform OCR on the cleaned image
Dim result = ocrTesseract.Read(imageInput)
End Using对于复杂的颜色替换,请参阅图像校正滤镜以获取其他预处理技术。
颜色替换的视觉效果如何?

前

后
我如何仅读取特定文本颜色?
使用SelectTextColor方法读取特定文本颜色。 指定目标颜色和容差(0-255)。 容差表示像素与所选颜色之间在 R、G 和 B 值上的允许差异。
颜色容差如何影响结果?
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);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)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)在使用彩色编码部分的表单、提取高亮文本或阅读特定警告信息时,使用选择性彩色阅读。 对于结构化数据提取,请参阅OcrResult Class以获取高级处理。
我可以从颜色选择中期待什么输出?
OCR 结果只能读取橙色文本:

我如何保存应用滤镜的修改后的PDF?
IronOCR 可保存带或不带过滤器的修改过的 PDF 文件。 SaveAsSearchablePdf方法的第二个参数指定是否应用滤镜保存。
可搜索PDF的实现是什么?
using IronOcr;
// Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
var ocr = new IronTesseract();
var ocrInput = new OcrInput();
// Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf");
// Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale();
// Run OCR on the preprocessed input
OcrResult result = ocr.Read(ocrInput);
// Write the searchable PDF; true = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", true);using IronOcr;
// Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
var ocr = new IronTesseract();
var ocrInput = new OcrInput();
// Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf");
// Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale();
// Run OCR on the preprocessed input
OcrResult result = ocr.Read(ocrInput);
// Write the searchable PDF; true = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", true);Imports IronOcr
' Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
Dim ocr As New IronTesseract()
Dim ocrInput As New OcrInput()
' Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf")
' Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale()
' Run OCR on the preprocessed input
Dim result As OcrResult = ocr.Read(ocrInput)
' Write the searchable PDF; True = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", True)Imports IronOcr
' Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
Dim ocr As New IronTesseract()
Dim ocrInput As New OcrInput()
' Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf")
' Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale()
' Run OCR on the preprocessed input
Dim result As OcrResult = ocr.Read(ocrInput)
' Write the searchable PDF; True = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", True)何时应对保存的PDF应用滤镜?
在创建更简洁、更易读的文档时应用筛选器。 这有助于存档或在不同设备上共享文档。 请参阅通过OCR创建可搜索的PDF以获取更多信息。
对于包含表格的复杂文档,请探索读取文档中的表格以获得专门的提取。 对于高量处理,请参阅多线程Tesseract OCR以优化多个文档的滤镜应用。
常见问题
C# OCR中图像二值化的目的是什么?
二值化将图像转换为双色格式,通常是黑白,用于将文本与背景分离并减少噪声。这通过创建高对比度图像增强了文本可读性和OCR准确性。
SelectTextColor方法如何改善OCR结果?
SelectTextColor方法允许您将OCR聚焦于特定的文本颜色,使得无需复杂的图像处理即可进行目标文本的提取。这提高了处理包含多种文本颜色的文档的准确性。
在图像预处理中何时应使用灰度转换?
灰度转换对减少带有干扰颜色的图像的视觉复杂性非常有用。它适用于与文本识别发生干扰的彩色背景或水印,同时保留复杂布局中的细节。
为什么颜色反转对OCR有利?
颜色反转通过将黑背景上的白色文字转换为白背景上的黑色文字来增强对比度,从而提高可读性。这对深色主题的截图或负片图像特别有帮助。
容差在ReplaceColor方法中扮演什么角色?
ReplaceColor方法中的容差定义了像素与目标颜色的匹配程度,对于处理模糊图像或墨水渗透的文件至关重要,从而提高文字的显著性和对比度。
IronOCR如何提高OCR处理速度?
IronOCR支持多线程处理,在处理大量文档时可加快OCR速度。这加快了多个图像的滤镜应用速度,提高了整体效率。
从选择性颜色读取的OCR预期有哪些输出?
从选择性颜色读取的OCR结果仅专注于指定的颜色,例如提取高亮文本或特定的警告信息,实现从颜色编码文档中精确数据提取。
如何使用IronOCR创建可搜索的PDF?
IronOCR允许您使用SaveAsSearchablePdf方法保存修改后的PDF。可以应用灰度等滤镜来提高结果文件的文本可读性和噪声减少。
哪些预处理步骤可以提高低质量扫描的OCR准确性?
对于低质量扫描,二值化、灰度转换和颜色反转等预处理步骤通过减少噪声和提高对比度,可以显著提高文本可读性和OCR准确性。
IronOCR如何帮助进行结构化数据提取?
IronOCR提供了高级处理能力,如OcrResult类,支持从图像中提取结构化数据,非常适合用于带有颜色编码区段和高亮文本的表单。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。