如何在 C# 中調整圖片色彩以利閱讀 | IronOCR

如何在 C# 中修正圖片色彩以利閱讀

This article was translated from English: Does it need improvement?
Translated
View the article in English

using IronOCR 的二值化、灰階化、反轉及顏色替換方法,在 C# 中修正圖片顏色,以提升文字可讀性與 OCR 準確度。 讀取特定文字顏色以進行目標提取。

快速入門:隔離特定文字顏色 using IronOCR 的 SelectTextColor 方法,可讓 OCR 專注於特定文字顏色——無需進行複雜的影像處理。 載入圖片、選擇文字顏色與容差值,並僅擷取該文字以獲得精準的 OCR 結果。

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronOcr

    PM > Install-Package IronOcr
  2. 請複製並執行此程式碼片段。

    new IronTesseract().Read(new IronOcr.OcrImageInput("sample.jpg").SelectTextColor(new IronSoftware.Drawing.Co/lor("#DB645C"), 60));
  3. 部署至您的生產環境進行測試

    立即透過免費試用,在您的專案中開始使用 IronOCR

    arrow pointer


如何將圖像二值化以提升 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")
$vbLabelText   $csharpLabel

若需批次處理,請將二值化與其他濾鏡結合使用。 請使用"濾鏡精靈"來確定最適合您影像的濾鏡組合。

如何匯出並比較結果?

請使用 SaveAsImages 方法匯出修改後的圖片。 請參閱下方二值化前後的圖片對比。

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

如何將圖片轉換為灰階以提升可讀性?

將圖片轉換為灰階可減少視覺雜亂感,並提升可讀性。 當原始色彩會分散對內容的注意力時,此舉將有所助益。

何時應優先採用灰階處理而非其他方法?

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

針對不同畫質的圖片,請將灰階處理與其他預處理步驟結合使用。 OcrInput 類別的文件說明了如何串接多個濾鏡。

Grayscale 與原文相比如何?

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

何時該反轉圖片顏色?

反轉顏色可增強對比度。將黑色背景上的白色文字轉換為白色背景上的黑色文字,能提升可讀性。

如何實作色彩反轉?

請使用 Invert 方法來反轉顏色。 傳入布林值以移除色彩通道並返回灰階。

色彩反轉功能可處理負片影像或深色主題的螢幕截圖。 現代應用程式採用深色模式介面,這對傳統 OCR 技術構成挑戰。 將這些圖片翻轉可確保最佳辨識效果。 有關各種圖像類型的資訊,請參閱《Fast 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()
$vbLabelText   $csharpLabel

針對混合文件類型的批次處理,請實作自動深色背景偵測功能。 IronOCR 的電腦視覺功能可識別何時需要進行反轉處理。

各種反轉選項之間有何差異?

比較 Invert 方法在啟用與未啟用灰階效果時的差異:

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

如何在圖片中替換特定顏色?

請替換特定顏色以突顯或淡化元素。 請使用此功能來提升文字的突顯度或修正有問題的對比。

為何在顏色替換中,容差值至關重要?

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

如何處理進階的顏色替換情境?

針對複雜情境,可串接多個替換操作:

/* :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.Co/lor.Red, IronSoftware.Drawing.Co/lor.Black, 70);

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

// Replace light yellow background with white
imageInput.ReplaceColor(new IronSoftware.Drawing.Co/lor("#FFFACD"), IronSoftware.Drawing.Co/lor.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.Co/lor.Red, IronSoftware.Drawing.Co/lor.Black, 70);

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

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

// Perform OCR on the cleaned image
var result = ocrTesseract.Read(imageInput);
Imports IronSoftware.Drawing
Imports IronSoftware.Ocr

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

若需進行複雜的色彩替換,請參閱"影像校正濾鏡"以了解其他預處理技術。

顏色替換會產生什麼樣的視覺效果?

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

如何僅讀取特定文字顏色?

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

針對採用顏色編碼區段的表單,請使用選擇性色彩辨識功能,以擷取標示文字或讀取特定警告訊息。 關於結構化資料擷取,請參閱 OcrResult 類別以進行進階處理。

"顏色選取"功能能提供哪些輸出結果?

OCR 結果僅能讀取橘色文字:

除錯主控台顯示 OCR 擷取的橘色文字:來自商業文件的 MASAYOSHI SON 和 YASUMITSU SHIGETA

如何儲存已套用濾鏡的修改後 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)
$vbLabelText   $csharpLabel

何時應對儲存的 PDF 套用濾鏡?

在建立更簡潔、易讀的文件時,請運用篩選器。 這有助於文件歸檔或在不同裝置間分享文件。 如需更多資訊,請參閱《透過 OCR 建立可搜尋的 PDF》

若需處理包含表格的複雜文件,請參閱"讀取文件中的表格"以進行專業的資料擷取。 若需處理大量文件,請參閱《多執行緒 Tesseract OCR》,以優化跨多份文件的濾鏡套用。

常見問題

如何修正影響 OCR 準確度的劣質圖片色彩?

IronOCR 提供多種色彩校正方法,包括二值化、灰階轉換、反轉及色彩替換。這些濾鏡透過增強文字與背景之間的對比度,有助於提升文字可讀性,使 OCR 引擎更容易從色彩品質不佳的圖像中準確擷取文字。

何謂二值化?在 OCR 應用中何時應使用此技術?

二值化功能透過 IronOCR 的 Binarize 方法,將影像轉換為雙色格式(通常為黑白)。此功能對於光線不均或背景雜訊較多的掃描文件特別有效,因為它會分析影像並確定最佳的分離閾值,從而使文字與背景之間產生清晰的區別。

如何從圖片中僅擷取特定顏色的文字?

IronOCR 的 SelectTextColor 方法讓您無需進行複雜的影像處理,即可將 OCR 聚焦於特定文字顏色。只需指定目標顏色和容差等級——例如,SelectTextColor(new IronSoftware.Drawing.Co/lor("#DB645C"), 60)——即可僅擷取該顏色範圍內的文字。

何時應將圖像轉換為灰階,而非使用二值化?

處理會干擾文字辨識的彩色背景或浮水印時,請使用 IronOCR 的 ToGrayScale 方法。與二值化不同,灰階處理能保留細微變化處的影像細節,因此當您需要在降低視覺雜亂度的同時維持影像品質時,此方法是理想選擇。

我可以匯出修正後的圖片來查看色彩濾鏡的效果嗎?

是的,IronOCR 提供 SaveAsImages 方法,可在套用色彩校正後匯出修改後的影像。這讓您能夠比較前後的結果,協助您判斷哪些色彩校正方法最適合您的特定影像。

我該如何為我的圖片選擇最佳的色彩校正濾鏡?

IronOCR 內建「濾鏡精靈」工具,可協助您為圖片找出最佳的濾鏡組合。進行批次處理時,您可以將二值化等多種濾鏡與其他預處理技術結合,以針對您的文件類型獲得最佳的 OCR 結果。

IronOCR 能否整合至現有應用程式中?

IronOCR 設計上可輕鬆透過 C# 整合至現有應用程式中,讓開發人員能以最少的努力,為其軟體增添 OCR 功能。

使用 IronOCR 進行文件管理有哪些好處?

使用 IronOCR 進行文件管理,可將掃描文件轉換為可搜尋且可編輯的文字,從而簡化工作流程,減少人工資料輸入的需求,並提升文件的可存取性。

IronOCR 如何提升資料準確性?

IronOCR 透過其先進的辨識演算法與影像校正功能來提升資料準確性,確保文字擷取過程既可靠又精確。

IronOCR 是否有提供免費試用版?

是的,Iron Software 提供 IronOCR 的免費試用版,讓使用者能在決定購買前測試其功能與效能。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

審閱者:
Jeff Fritz
Jeffrey T. Fritz
首席程式經理 - .NET 社群團隊
Jeff 同時也是 .NET 與 Visual Studio 團隊的首席程式經理。他是 .NET Conf 虛擬會議系列的執行製作人,並主持每週播出兩次的開發者直播節目《Fritz and Friends》,在節目中他會與觀眾一起探討技術話題並共同編寫程式碼。Jeff 負責撰寫工作坊內容、準備簡報,並為 Microsoft Build、Microsoft Ignite、.NET Conf 以及 Microsoft MVP Summit 等微軟最大規模的開發者活動規劃內容。
準備開始了嗎?
Nuget 下載 5,896,332 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronOcr
執行範例 觀看您的圖片轉為可搜尋文字。