C# Guide: Using IronOCR Image Filters for Better OCR

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

IronOCR 提供您所需的工具,用於讀取可能需要透過濾鏡進行預處理的圖像。 您可以從多種濾鏡中選擇,藉此處理您的圖片使其可進行後續處理。

快速入門:套用濾鏡來清理 OCR 影像

只需一組簡單的呼叫鏈,您即可套用 BinarizeDeskew 濾鏡,在 OCR 處理前提升掃描畫面的清晰度。 此範例展示了使用 IronOCR 內建濾鏡來增強影像是多麼簡單,並能立即上手。

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

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

    using var input = new IronOcr.OcrInput("scan.jpg"); input.DeNoise(true).Binarize().Deskew(45); var result = new IronOcr.IronTesseract().Read(input);
  3. 部署至您的生產環境進行測試

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

    arrow pointer

OCR 影像濾鏡清單

以下圖片濾鏡能顯著提升效能:

  • Filters to change the Image Orientation
    • Rotate - 將圖片順時針旋轉指定角度。若要逆時針旋轉,請使用負數。
    • Deskew - 將圖片旋轉至正確方向並使其呈垂直狀態。 這對 OCR 非常有用,因為 Tesseract 對傾斜掃描的容錯度最低可達 5 度。
    • Scale - 按比例縮放 OCR 輸入頁面。
  • Filters to manipulate Image Colors
    • Binarize - 此影像濾鏡會將每個像素轉為純黑或純白,不留任何中間色調。 這有助於在文字與背景對比度極低的情況下提升 OCR 效能。
    • ToGrayScale - 此影像濾鏡會將每個像素轉為灰階。 不太可能提高 OCR 準確度,但可能會提升速度。
    • Invert - 將所有顏色反轉。 例如:白色變為黑色,反之亦然。
    • ReplaceColor - 在特定閾值範圍內,將圖片中的某種顏色替換為另一種顏色。
  • Filters to improve Contrast in an Image
    • Contrast - 自動提高對比度。 此濾鏡通常能提升低對比度掃描檔的 OCR 速度與準確度。
    • Dilate - 進階形態學。 擴散 會在影像中物件的邊界添加像素。 "侵蝕"的反義詞。
    • Erode - 進階形態學。 Erosion 會從物件邊界移除像素。 Dilate 的反義詞。
  • Filters to reduce Image Noise
    • Sharpen - 銳化模糊的 OCR 文件,並將透明通道轉換為白色。
    • DeNoise - 移除數位雜訊。此濾鏡僅應在預期會出現雜訊的情境下使用。
    • EnhanceResolution - 提升低畫質影像的解析度。 此篩選器通常不需要,因為 OcrInput.MinimumDPIOcrInput.TargetDPI 會自動擷取並處理低解析度的輸入內容。

篩選範例與用法

在以下範例中,我們將示範如何在程式碼中套用篩選器。

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-1.cs
using IronOcr;
using System;

var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("my_image.png");
input.Deskew();

var result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr
Imports System

Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("my_image.png")
input.Deskew()

Dim result = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

除錯篩選器 / 此篩選器有何作用?

若您在程式中難以讀取圖片或BarCode,有一種方法可以將過濾後的結果儲存為圖片。 如此一來,您便能進行除錯,並確切了解每個濾鏡的功能及其如何處理您的影像。

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-2.cs
using IronOcr;
using System;

var file = "skewed_image.tiff";
var ocr = new IronTesseract();
using var input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(file, pageindices);
// Here we apply the filter: Deskew
input.Deskew();

// Save the input with filter(s) applied
input.SaveAsImages("my_deskewed");

// We read, then print the text to the console
var result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr
Imports System

Private file = "skewed_image.tiff"
Private ocr = New IronTesseract()
Private input = New OcrInput()
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames(file, pageindices)
' Here we apply the filter: Deskew
input.Deskew()

' Save the input with filter(s) applied
input.SaveAsImages("my_deskewed")

' We read, then print the text to the console
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

篩選使用情境

旋轉

API 參考指南

篩選說明

"旋轉"是一種濾鏡,用於手動為圖片設定已知的旋轉角度,使其盡可能接近水平。 IronOCR 具備執行 Deskew() 的功能,然而其容錯範圍相當有限,最適合用於幾乎完全筆直(誤差在 15 度左右)的圖像。 對於旋轉 90 度或倒置的輸入圖片,應呼叫 Rotate()

用例程式碼範例

以下是呼叫 Rotate() 來修正倒置圖片的範例:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-3.cs
using IronOcr;
using System;

var image = "screenshot.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);

// Rotate 180 degrees because image is upside-down
input.Rotate(180);

// Read image into variable: result
var result = ocr.Read(input);

// Example print to console
Console.WriteLine(result.Text);
Imports IronOcr
Imports System

Private image = "screenshot.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)

' Rotate 180 degrees because image is upside-down
input.Rotate(180)

' Read image into variable: result
Dim result = ocr.Read(input)

' Example print to console
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel
在 `Input.Rotate(180) ``
Screenshot related to 用例程式碼範例 Screenshot Rotated related to 用例程式碼範例

校正

API 參考指南

篩選說明

使用霍夫變換(Hough Transform)嘗試在特定容差範圍內校正圖像。 對於未完全平放的影像而言,這點至關重要,因為傾斜的文件可能會導致讀取錯誤。

請注意此方法會傳回一個布林值,若篩選器已套用則為 true;若因無法偵測圖片方向而套用失敗,則為 false。 (若頁面無內容可定義方向,此操作將失敗。)}]

用例程式碼範例

以下是呼叫 Deskew() 來修正傾斜圖片的範例:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-4.cs
using IronOcr;
using System;

var image = @"paragraph_skewed.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);

// Apply deskew with 15 degree snap
bool didDeskew = input.Deskew(15);
if (didDeskew)
{
    // Read image into variable: result
    var result = ocr.Read(input);
    Console.WriteLine(result.Text);
}
else
{
    Console.WriteLine("Deskew not applied because Image Orientation could not be determined.");
}
Imports IronOcr
Imports System

Private image = "paragraph_skewed.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)

' Apply deskew with 15 degree snap
Dim didDeskew As Boolean = input.Deskew(15)
If didDeskew Then
	' Read image into variable: result
	Dim result = ocr.Read(input)
	Console.WriteLine(result.Text)
Else
	Console.WriteLine("Deskew not applied because Image Orientation could not be determined.")
End If
$vbLabelText   $csharpLabel

規模

API 參考指南

篩選說明

Scale 是一個實用的影像處理濾鏡,可協助根據影像現有的像素數來調整其大小。 此功能適用於以下情況:當BARCODE因圖像寬度僅有數十像素(每條BARCODE佔一個像素)而無法掃描時,或文字過小且未進行抗鋸齒處理時。

請注意BarCode尺寸存在一個 1000px x 1000px 的最佳範圍,在此範圍內BarCode可被良好讀取,若您的BarCode無法被識別,應將此點納入考量。

用例程式碼範例

以下是呼叫 Scale() 以擴大 BARCODE 中 BARCODE 間距以便掃描的範例:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-5.cs
using IronOcr;
using System;

var image = @"small_barcode.png";
var ocr = new IronTesseract();

// Optional: This example uses a barcode
ocr.Configuration.ReadBarCodes = true;

using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);

// Apply scale
input.Scale(400); // 400% is 4 times larger

// Read image into variable: result
var result = ocr.Read(input);

// Example print to console
Console.WriteLine(result.Text);
Imports IronOcr
Imports System

Private image = "small_barcode.png"
Private ocr = New IronTesseract()

' Optional: This example uses a barcode
ocr.Configuration.ReadBarCodes = True

Dim input = New OcrInput()
' Load at least one image
input.LoadImage(image)

' Apply scale
input.Scale(400) ' 400% is 4 times larger

' Read image into variable: result
Dim result = ocr.Read(input)

' Example print to console
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

Binarize

API 參考指南

篩選說明

Binarize 濾鏡會根據自適應演算法,將影像中的所有像素分類為黑色或白色。 此設定將移除所有色彩,並將背景統一為純白色,同時將所有識別出的文字設為純黑色,以利閱讀。

用例程式碼範例

以下是呼叫 Binarize() 來對齊彩色文字並移除背景顏色與雜訊的範例:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-6.cs
using IronOcr;
using System;

var image = @"no-binarize.jpg";
var ocr = new IronTesseract();

using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);

// Apply Binarize
input.Binarize();

// Read image into variable: result
var result = ocr.Read(input);

// Example print to console
Console.WriteLine(result.Text);
Imports IronOcr
Imports System

Private image = "no-binarize.jpg"
Private ocr = New IronTesseract()

Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)

' Apply Binarize
input.Binarize()

' Read image into variable: result
Dim result = ocr.Read(input)

' Example print to console
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel
在 `Binarize() 之前 ``
No Binarize related to 用例程式碼範例 After Binarize related to 用例程式碼範例

反轉

API 參考指南

篩選說明

當圖片格式為 black text on a white background 時,IronOCR 的讀取效果最佳。 Invert 濾鏡透過將圖像中的所有顏色反轉來實現此效果。

用例程式碼範例

以下是呼叫 Invert() 將"白底黑字"轉換為"黑底白字"的範例:

:path=/static-assets/ocr/content-code-examples/tutorials/c-sharp-ocr-image-filters-7.cs
using IronOcr;
using System;

var image = @"before-invert.png";
var ocr = new IronTesseract();

using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);

// Apply Invert
input.Invert(true);

// Read image into variable: result
var result = ocr.Read(input);

// Example print to console
Console.WriteLine(result.Text);
Imports IronOcr
Imports System

Private image = "before-invert.png"
Private ocr = New IronTesseract()

Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)

' Apply Invert
input.Invert(True)

' Read image into variable: result
Dim result = ocr.Read(input)

' Example print to console
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel
Before After
Before Invert related to 用例程式碼範例 After Invert related to 用例程式碼範例

常見問題

在 C# 中,影像濾鏡如何提升 OCR 的準確度?

IronOCR 中的影像濾鏡可對影像進行預處理以提升畫質,從而提高 OCR 辨識準確度。諸如「二值化」和「對比度」等濾鏡可透過調整色彩與對比度來提升可讀性,而「旋轉」和「校正傾斜」則能修正影像方向。

有哪些可用濾鏡可修正圖片方向?

IronOCR 提供「旋轉」與「校正傾斜」濾鏡,用以修正影像方向問題。「旋轉」功能可手動調整影像角度,而「校正傾斜」則能自動校正略微傾斜的影像。

Binarize 濾鏡如何影響影像預處理?

IronOCR 中的 Binarize 濾鏡可將影像像素轉換為黑白,藉此去除背景顏色並提升文字可讀性,特別是在低對比度環境下能顯著提高 OCR 辨識準確度。

何時適合使用降噪濾波器?

當影像中出現數位雜訊時,應使用「銳化」和「去噪」等降噪濾鏡。這些濾鏡能清理影像,使文字更清晰,從而提升 IronOCR 的 OCR 辨識效果。

提高影像解析度會影響 OCR 效能嗎?

是的,使用 EnhanceResolution 濾鏡可透過提高低品質影像的解析度來提升 OCR 效能。雖然 IronOCR 的預設 MinimumDPI 和 TargetDPI 設定通常已足夠,但若需要,此濾鏡可提供額外的解析度增強效果。

色彩處理濾鏡在 OCR 中扮演什麼角色?

IronOCR 中的色彩處理濾鏡(如 Invert、ToGrayScale 和 Binarize)可調整圖像色彩以提升文字可讀性。Invert 會變更色彩方案,ToGrayScale 將圖像轉換為灰階,而 Binarize 則將圖像轉為黑白。

「對比」與「銳化」濾鏡有何區別?

IronOCR 中的「對比度」濾鏡可增加明暗區域之間的差異,從而提升文字清晰度;而「銳化」濾鏡則能強化邊緣,使文字更為鮮明,這兩項功能皆有助於提升 OCR 辨識效果。

如何在 IronOCR 中儲存並除錯經過篩選的圖片?

若要在 IronOCR 中儲存並除錯經過濾波處理的影像,請在套用濾波器後使用 SaveAsImages 函式。此舉有助於視覺化濾波效果,並確保預處理步驟已提升 OCR 的影像品質。

IronOCR 提供了哪些進階形態學篩選器?

IronOCR 提供「擴張」與「侵蝕」等進階形態學濾波器。「擴張」會在物件邊界添加像素以增強特徵,「侵蝕」則會移除像素,兩者皆用於釐清影像細節,從而提升 OCR 準確度。

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
執行範例 觀看您的圖片轉為可搜尋文字。