如何在 C# 中修復圖像顏色以提升 OCR辨識與圖片轉文字準確率
使用 IronOCR 的二值化、灰度化、反轉和顏色替換方法在 C# 中修復圖像顏色,以提高文字可讀性和文字辨識準確率。 讀取特定文字顏色以進行目標擷取圖片文字。
快速入門:隔離特定文字顏色
使用 IronOCR 的 SelectTextColor 方法,將 OCR 聚焦於特定的文字顏色—無需複雜的影像處理。 載入圖像,選擇文字顏色和容差,然後僅提取該文本,以獲得準確的 OCR 結果。
如何在 C# 中修復圖像顏色以進行讀取
- 下載一個用於校正影像顏色的 C# 庫
- 導入PDF文件和圖像以供閱讀
- 應用顏色效果:二值化、灰階化、反轉和顏色替換
- 匯出校正後的影像以供查看
- 使用`SelectTextColor`讀取特定文字顏色
如何將影像二值化以提高 OCR 辨識率?
二值化將影像轉換為雙色格式,通常為黑白影像。這可以將文字與背景分離並減少雜訊,使文字更加清晰易讀。
為什麼二值化可以提高OCR辨識準確率?
使用 Binarize 方法應用二值化。 OCR技術在高對比影像(例如白底黑字)上效果最佳。 這種方法可以清晰地區分背景和人物。
二值化技術在處理光線不均或背景雜訊較大的掃描文件時效果尤佳。此演算法分析影像並確定最佳閾值,從而將前景文字與背景分開。 有關進階預處理技術,請參閱OCR 影像優化濾鏡指南。
二進位化的程式碼實作是什麼?
: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")
對於批量處理,將二值化與其他濾波器結合使用。 使用濾鏡精靈確定最適合您影像的濾鏡組合。
如何匯出和比較結果?
使用 SaveAsImages 方法匯出修改後的圖片。 對比下圖二值化前後的影像。
前
後
如何將影像轉換為灰階影像以便更好地閱讀?
將影像轉換為灰階影像可以減少視覺混亂並提高可讀性。 這有助於避免原始色彩分散人們對內容的注意力。
何時應該使用灰階影像而不是其他方法?
使用 ToGrayScale 方法應用灰階。 過程對 R、G 和 B 值取平均值。
灰階影像與會幹擾文字辨識的彩色背景或浮水印配合使用效果很好。 與二值化不同,灰階影像能夠保留細微變化中的細節。 適用於具有嵌入式文字或複雜佈局的照片。 對於低品質掃描件,請參閱"修復低品質掃描件和影像" 。
灰階轉換的簡單程式碼是什麼?
: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()
對於不同的影像質量,可將灰階影像與其他預處理步驟結合使用。 OcrInput 類別文件說明如何連結多個篩選器。
灰階影像與原圖相比如何?
前
後
何時應該反轉影像顏色?
反轉顏色可以增強對比。將黑底白字轉換為白底黑字可以提高可讀性。
如何實現顏色反轉?
使用 Invert 方法反轉顏色。 傳遞一個布林值以移除顏色通道並返回灰階影像。
顏色反轉功能可以處理負片或深色主題的螢幕截圖。 現代應用程式使用深色模式介面,這對傳統的OCR技術提出了挑戰。 反轉這些影像可確保最佳辨識效果。 有關各種影像類型,請參閱快速 OCR 設定指南。
: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()
對於混合文件類型的批次處理,實現自動深色背景檢測。 IronOCR中的電腦視覺功能可以辨識何時需要反轉。
反向期權有什麼區別?
比較使用灰階模式和不使用灰階模式的 Invert 方法:
倒
反轉和灰階化
如何替換影像中的特定顏色?
替換特定顏色以突出顯示或弱化元素。 使用此功能可以增強文字的突出性或糾正對比度問題。
為什麼顏色替換中容差很重要?
ReplaceColor 方法需要目前顏色、新顏色和容差值。 更高的容差值能更好地處理模糊影像。
容差決定了像素與目標顏色的匹配程度。 低值(0-50)適合純色。 較高的值(100-200)可以處理抗鋸齒文字或壓縮偽影。 這有助於處理掃描文檔,避免墨水暈染或紙張紋理造成差異。
如何實現顏色替換?
: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")
如何處理進階顏色替換場景?
針對複雜場景進行多次鍊式替換:
/* :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 Using
對於複雜的顏色替換,請參閱影像校正濾鏡以了解其他預處理技術。
顏色替換的視覺效果如何?
前
後
如何只顯示特定顏色的文字?
使用 SelectTextColor 方法讀取特定文字顏色。 指定目標顏色和容差(0-255)。 容差表示 R、G 和 B 值中像素與所選顏色之間允許的差異。
顏色容差如何影響結果?
: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)
使用選擇性顏色閱讀功能,可以閱讀帶有顏色編碼部分的表格、提取突出顯示的文本或閱讀特定的警告訊息。 對於結構化資料擷取,請參閱OcrResult 類別以了解高階處理方法。
顏色選擇後我能得到什麼樣的輸出結果?
OCR辨識結果只辨識出橘色文字:
如何儲存已套用篩選條件的修改後的PDF檔案?
IronOCR可以保存有濾鏡或沒有濾鏡的修改後的 PDF 檔案。 SaveAsSearchablePdf 方法的第二個參數指定是否應用篩選條件進行保存。
可搜尋PDF的實作方式是什麼?
:path=/static-assets/ocr/content-code-examples/how-to/image-quality-correction-searchable-pdf.cs
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)
何時應該對已儲存的 PDF 檔案套用篩選器?
在建立更清晰、更易讀的文件時,可以使用篩選器。 這有助於文件存檔或在不同設備間共用文件。 有關更多信息,請參閱"透過 OCR 建立可搜尋 PDF" 。
對於包含表格的複雜文檔,請探索"讀取文檔中的表格"功能以進行專門提取。 對於大批量處理,請參閱 多執行緒 Tesseract OCR ,以最佳化跨多個文件的過濾器應用。
常見問題解答
如何修正影響 OCR 準確性的不良影像顏色?
IronOCR 提供多種色彩校正方法,包括二值化、灰階轉換、反轉和色彩置換。這些濾鏡可增強文字與背景之間的對比,有助於提高文字的可讀性,讓 OCR 引擎更容易從色彩品質不佳的影像中準確提取文字。
什麼是二進位,什麼時候應該將其用於 OCR?
Binarization 使用 IronOCR 的 Binarize 方法將影像轉換為雙色格式(通常是黑白)。它對於光線不均勻或背景雜訊的掃描文件特別有效,因為它能透過分析影像並決定最佳的分離臨界值,在文字和背景之間建立清晰的區別。
如何從影像中只擷取特定顏色的文字?
IronOCR 的 SelectTextColor 方法可讓您集中對特定文字顏色進行 OCR,而無需複雜的影像處理。只需指定目標顏色和容差等級 - 例如,SelectTextColor(new IronSoftware.Drawing.Color("#DB645C"), 60) - 即可僅擷取該顏色範圍內的文字。
何時應該將圖像轉換為灰階,而不是使用二值化?
在處理會干擾文字辨識的彩色背景或水印時,請使用 IronOCR 的 ToGrayScale 方法。與二值化不同,灰階能夠保留影像中細微變化的細節,因此當您需要維持影像品質,同時減少視覺雜訊時,灰階是最理想的選擇。
我可以匯出修正後的影像來檢視色彩濾鏡的結果嗎?
是的,IronOCR 提供 SaveAsImages 方法,可在套用色彩修正後匯出修改後的影像。這可讓您比較之前和之後的結果,幫助您確定哪種色彩修正方法最適合您的特定影像。
如何為我的影像決定最佳的色彩校正濾鏡?
IronOCR 包含濾鏡精靈工具,可協助您決定影像的最佳濾鏡組合。對於批次處理,您可以將二值化等多種篩選器與其他預處理技術結合,以達到適合您的文件類型的最佳 OCR 結果。

