如何在 C# 中修復圖像顏色以進行讀取

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

修復影像顏色涉及多種技術,旨在提高影像的清晰度和品質。 IronOcr 提供二值化、灰度化、反轉和顏色替換方法,使圖像中的文字和內容更易讀、更美觀,這在使用 OCR(光學字元辨識)從圖像中提取文字時尤其重要。 也可以只讀取選取的文字顏色。

快速入門:一次隔離特定文字顏色

使用 IronOCR 的 SelectTextColor 方法,將 OCR 的重點放在您關心的文字顏色上—無需複雜的影像處理。 只需一行程式碼,即可載入圖像、選擇文字顏色和容差,並僅提取該文本,從而獲得準確的 OCR 結果。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronOCR

    PM > Install-Package IronOcr

  2. 複製並運行這段程式碼。

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

    立即開始在您的專案中使用 IronOCR,免費試用!
    arrow pointer


二值化圖像範例

這個過程將影像轉換為雙色格式,通常是黑白影像。它有助於將文字與背景分離並減少雜訊,使文字更加清晰易讀。

若要對影像套用二值化效果,請使用Binarize方法。 由於 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
Binarized image

灰階影像範例

將圖像轉換為各種灰度可以減少干擾,使讀者更容易閱讀。 當影像中的原始顏色造成視覺混亂時,這尤其有用。

若要將灰階效果套用至影像,請使用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
Sample image
Grayscaled image

反轉影像範例

反轉顏色可以增強對比。例如,將黑底白字改為白底黑字可以提高可讀性。

使用Invert方法反轉影像顏色。 該方法可以選擇性地接受一個布林值,該值用於移除所有顏色通道並返回灰階影像。

: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

下圖顯示了啟用和停用灰階選項的反轉方法。

倒 image
倒 and grayscaled image

替換顏色範例

這種技術可讓您將圖像中的特定顏色替換為其他顏色,這有助於突出或弱化某些元素。 它通常用於使文字更加突出或糾正有問題的顏色對比。

若要使用ReplaceColor方法,請指定要替換的目前顏色以及新顏色。 此方法的第三個參數,即容差值,也很重要。 對於模糊影像,需要更高的容忍度才能達到預期效果。

: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
Sample image
Replaced color image

閱讀特定文字顏色範例

此功能旨在僅讀取指定顏色的文字。 使用SelectTextColor方法指定 IronOcr 要注意的顏色以及容差值。 容差值接受 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

下面您將看到 OCR 結果,該結果旨在僅讀取橙色文字。

OCR結果

可搜尋的PDF

除了提供影像過濾選項外,IronOcr 還允許開發人員保存套用了或未套用這些濾鏡的修改後的 PDF 檔案。 SaveAsSearchablePdf方法的第二個參數是一個布林標誌,允許使用者指定是否啟用或停用篩選器來儲存 PDF。

: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);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

常見問題解答

如何在.NET C#中強化影像顏色以進行OCR?

你可以使用IronOCR在.NET C#中強化影像顏色進行OCR。首先從NuGet下載IronOCR庫,導入影像,然後應用顏色校正技術,如二值化、灰階和反轉,以提高文字可讀性。

二值化在影像顏色校正中有何作用?

影像顏色校正中的二值化將影像轉換為黑白格式,有助於將文字從背景中分離,減少噪音,使文字更為明顯,以便使用IronOCR獲得更好的OCR結果。

應用灰階效果如何有利於OCR過程?

應用灰階效果通過將影像轉換為灰色調來減少顏色干擾。這種簡化使影像變得更簡潔,提高了文字的可讀性,對使用IronOCR進行OCR過程有利。

反轉影像顏色在文字提取中有何優勢?

反轉影像顏色透過改變顏色方案增加了對比度,例如將黑色背景上的白色文字更改為白色背景上的黑色文字。這種對比度的提升可以提高使用IronOCR進行OCR的文字提取準確性。

如何更改影像中的特定顏色以獲得更好的OCR結果?

要更改影像中的特定顏色以獲得更好的OCR結果,請使用IronOCR中的ReplaceColor方法。指定原色、新顏色,並調整容差值以微調顏色替換。

在OCR過程中是否可以專注於特定顏色的文字?

是的,你可以在OCR過程中使用IronOCR的SelectTextColor方法專注於特定顏色的文字。這讓你能夠指定目標文字顏色和容差值,從而提高有色文字的OCR準確性。

顏色校正後如何導出修改過的影像?

在對影像進行顏色校正後,你可以使用IronOCR的SaveAsImages方法來導出它們。這讓你可以保存修改過的影像以供進一步處理或查看。

為何調整影像對比度對OCR準確性很重要?

調整影像對比度對OCR準確性至關重要,因為這有助於將文本與背景區分開來,使字符更為突出,OCR算法提取時更容易,特別是在使用IronOCR時。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

審核人
Jeff Fritz
Jeffrey T. Fritz
首席程序经理 - .NET 社区团队
Jeff 也是 .NET 和 Visual Studio 团队的首席程序经理。他是 .NET Conf 虚拟会议系列的执行制作人,并主持“Fritz 和朋友”这一每周两次的开发者的直播节目,在节目上讨论技术并与观众一起编写代码。Jeff 撰写研讨会、主持演讲,并计划大型 Microsoft 开发者活动(包括 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP Summit)的内容。
準備好開始了嗎?
Nuget 下載 5,167,857 | Version: 2025.11 剛發表