IronOCR 教程 IronOCR 過濾器 C# 指南:使用 IronOCR 影像濾鏡提升 OCR 辨識效果 Curtis Chau 更新:6月 9, 2025 下載 IronOCR NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronOCR 提供讀取影像所需的工具,這些影像可能需要以濾鏡的形式進行預處理。 您可以從各種各樣的濾鏡中進行選擇,這些濾鏡可以處理您的影像,使其可以進行後續處理。 快速入門:應用濾鏡清理 OCR 影像 只需一個簡單的呼叫鏈,即可應用去雜訊、二值化和去斜濾波器來提高 OCR 前的掃描清晰度。 本範例展示了使用 IronOCR 的內建濾鏡增強影像是多麼容易,並且可以立即上手。 立即開始使用 NuGet 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronOCR PM > Install-Package IronOcr 複製並運行這段程式碼。 using var input = new IronOcr.OcrInput("scan.jpg"); input.DeNoise(true).Binarize().Deskew(45); var result = new IronOcr.IronTesseract().Read(input); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronOCR,免費試用! 免費試用30天 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 - 高階形態學。 腐蝕會從物體邊界移除像素。 與擴張相反。 Filters to reduce Image Noise Sharpen - 銳利化模糊的 OCR 文檔,並將 alpha 通道展平為白色。 DeNoise - 去除數位雜訊。此濾波器僅套用於預期會出現雜訊的場景。 DeepCleanBackgroundNoise - 消除嚴重的背景噪音。 只有在已知文件背景噪音極大時才使用此過濾器,因為它可能會降低乾淨文件的 OCR 準確率,並且會佔用大量 CPU 資源。 EnhanceResolution - 提高低品質影像的解析度。 這個過濾器並不經常需要,因為 OcrInput.MinimumDPI 和 OcrInput.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 調試過濾器/過濾器正在執行什麼操作? 如果您在程式中讀取影像或條碼時遇到困難,可以儲存篩選結果的影像。 這樣,你就可以進行調試,並準確地看到每個濾鏡的作用以及它是如何處理圖像的。 :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 在輸入Before旋轉(180)`` 輸入After旋轉(180)`` 桌子 API 參考 篩選說明 使用霍夫變換來嘗試在一定容差範圍內矯正影像。 對於不完全水平的影像來說,這一點很重要,因為傾斜的文件可能會導致誤讀。 請注意此方法傳回一個布林值,如果應用了過濾器,則傳回 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 調整Before ()`` 調整斜面After ()`` 規模 API 參考 篩選說明 縮放是一個有用的影像處理濾鏡,它可以利用影像已有的像素來調整影像大小。 當條碼無法掃描時(因為圖像只有幾十個像素寬,每個條形佔一個像素),或者文字太小而沒有抗鋸齒,可以使用這種方法。 對於1000px x 1000px的條碼尺寸來說,存在一個最佳尺寸範圍,在這個範圍內條碼可以很好地被讀取。如果您的條碼無法被找到,則應考慮此尺寸範圍。 用例程式碼範例 這是一個呼叫 Scale 函數來增加條碼中條之間的間隙以便掃描的範例: :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 縮放Before ()`` 縮放After ()`` 二值化 API 參考 篩選說明 二值化濾波器根據自適應演算法將影像中的所有像素分類為黑色或白色。 這會移除所有顏色,並將背景變成純白色,所有被識別為文字的部分都會變成全黑色,以便於閱讀。 用例程式碼範例 這是一個呼叫 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 在二值化Before ()`` 二進位化After ()`` 倒置 API 參考 篩選說明 IronOCR 辨識black text on a white background影像效果最佳。 反轉濾鏡透過反轉影像上的所有顏色來實現這一目的。 用例程式碼範例 這是一個呼叫 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 常見問題解答 圖像過濾器如何在 C# 中提高 OCR 準確性? IronOCR 中的圖像過濾器可以對圖像進行預處理來提升其質量,從而提高 OCR 準確性。二值化和對比度等過濾器通過調整顏色和對比度提高可讀性,而旋轉和去斜則校正圖像方向。 更正圖像方向有什麼過濾器可用? IronOCR 提供旋轉和去斜過濾器來糾正圖像方向問題。旋轉允許手動調整圖像角度,而去斜則自動校正輕微傾斜的圖像。 二值化過濾器如何影響圖像預處理? IronOCR 中的二值化過濾器將圖像像素轉換為黑白,從而去除背景顏色並增強文本可見性,特別是在低對比度條件下提高 OCR 準確性。 什麼時候適合使用噪音降低過濾器? 當圖像中存在數字噪音時,應使用如銳化和去噪等噪音降低過濾器。這些過濾器清理圖像,使文本更清晰,以便在 IronOCR 中獲得更好的 OCR 結果。 增強圖像解析度會影響 OCR 性能嗎? 是的,使用增強解析度過濾器可以通過增加低質量圖像的解析度來提高 OCR 性能。儘管 IronOCR 的默認 MinimumDPI 和 TargetDPI 設置通常已經足夠,但如有需要,該過濾器可以提供額外的解析度增強。 顏色操作過濾器在 OCR 中扮演什麼角色? IronOCR 中的顏色操作過濾器如反轉、灰度化和二值化調整圖像顏色以提升文本可讀性。反轉更改色彩方案,灰度化將圖像轉化為灰階,而二值化將圖像簡化為黑白。 對比度和銳化過濾器有什麼區別? IronOCR 中的對比度過濾器增強亮暗區域間的差異,提高文本清晰度,而銳化過濾器增強邊緣,使文本更鮮明,兩者皆助於更好的OCR識別。 如何在 IronOCR 中保存和調試過濾后的圖像? 要保存和調試 IronOCR 中的過濾圖像,請在應用過濾器後使用 SaveAsImages 函數。這有助於可視化過濾器效果,確保預處理步驟提高了圖像品質以進行 OCR。 IronOCR 中有哪些高階形態學過濾器? IronOCR 提供高階形態學過濾器如膨脹和腐蝕。膨脹在物體邊界增加像素以增強特徵,而腐蝕則去除像素,兩者皆用於明確圖像細節以提高 OCR 準確性。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 審核人 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 剛發表 免費下載 NuGet 下載總數:5,167,857 檢視授權