使用IronOCR進行驗證碼 OCR辨識:圖片轉文字挑戰與解決方案

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,584,558 | 版本: 2026.4 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronOcr
執行範例 觀看您的圖片變成可搜尋的文字。