使用 IronOCR 處理驗證碼

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

IronOCR能讀取驗證碼嗎?

這有可能,但不能保證。

大多數 CAPTCHA 產生器都經過精心設計,旨在欺騙 OCR 軟體,有些甚至使用"無法被 OCR 軟體(如 Tesseract)讀取"作為單元測試。

根據定義,驗證碼對於OCR引擎來說非常難以讀取。 解析度非常低,每個字元都經過精心安排,與其他字元的角度和間距各不相同,並且還加入了變化的背景噪音。

移除背景雜訊後的灰階影像比彩色影像更容易處理,但仍具有挑戰性:

以下是 C# 程式碼範例,嘗試移除雜訊並將 CAPTCHA 影像轉換為灰階影像,以提高 OCR 結果:

using IronOcr;

class CaptchaReader
{
    static void Main(string[] args)
    {
        // Initialize the IronOCR engine
        var Ocr = new IronTesseract();

        // Create an OCR input object
        var Input = new OcrInput("captcha-image.jpg");

        // Apply noise reduction to improve OCR accuracy
        // This removes background noise while preserving text
        Input.DeNoise();

        // Optionally apply a deep clean for more aggressive noise removal
        Input.DeepCleanBackgroundNoise();

        // Convert the image to grayscale 
        // OCR works better on grayscale images compared to colored ones
        Input.ToGrayScale();

        // Perform OCR to extract text from the image
        var Result = Ocr.Read(Input);

        // Output the recognized text to the console
        Console.WriteLine(Result.Text);
    }
}
using IronOcr;

class CaptchaReader
{
    static void Main(string[] args)
    {
        // Initialize the IronOCR engine
        var Ocr = new IronTesseract();

        // Create an OCR input object
        var Input = new OcrInput("captcha-image.jpg");

        // Apply noise reduction to improve OCR accuracy
        // This removes background noise while preserving text
        Input.DeNoise();

        // Optionally apply a deep clean for more aggressive noise removal
        Input.DeepCleanBackgroundNoise();

        // Convert the image to grayscale 
        // OCR works better on grayscale images compared to colored ones
        Input.ToGrayScale();

        // Perform OCR to extract text from the image
        var Result = Ocr.Read(Input);

        // Output the recognized text to the console
        Console.WriteLine(Result.Text);
    }
}
$vbLabelText   $csharpLabel

說明:

  • IronOcr :此程式庫用於從圖像中讀取文字。
  • OcrInput :此類表示用於 OCR 處理的影像輸入。
  • DeNoise :此方法用於減少影像中的背景雜訊。
  • DeepCleanBackgroundNoise :如果基本DeNoise不佳,則採用此方法進行更徹底的降噪。
  • ToGrayScale :此操作將影像轉換為灰階影像,以提高辨識準確率。
  • Read :呼叫此方法從預處理的圖像中提取文字。
柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

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

準備好開始了嗎?
Nuget 下載 5,299,091 | 版本: 2025.12 剛剛發布