使用 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);
    }
}
Imports IronOcr

Friend Class CaptchaReader
	Shared Sub Main(ByVal args() As String)
		' Initialize the IronOCR engine
		Dim Ocr = New IronTesseract()

		' Create an OCR input object
		Dim 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
		Dim Result = Ocr.Read(Input)

		' Output the recognized text to the console
		Console.WriteLine(Result.Text)
	End Sub
End Class
$vbLabelText   $csharpLabel

說明:

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

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

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

準備好開始了嗎?
Nuget 下載 5,167,857 | Version: 2025.11 剛發表