使用IronOCR處理CAPTCHA

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

IronOCR會識別captcha程式碼嗎?

這是可能的,但不能保證。

大多數CAPTCHA生成器故意設計來愚弄OCR軟體,有些甚至使用類似Tesseract的"不能被OCR軟體讀取"作為單元測試。

Captcha程式碼本質上對OCR引擎來說非常難以識別。 解析度非常低,每個字元的角度和間隙都與其他字元不同,並且包括可變的背景噪音。

去除背景噪音的灰階圖像比彩色圖像更成功,但仍然具有挑戰性:

以下是嘗試去除噪音並將CAPTCHA圖像轉換為灰階以改善OCR結果的C#範例程式碼:

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:此方法用於減少圖像中的背景噪音。
  • DeNoise不夠,此方法用於更積極地降噪。
  • ToGrayScale:這將圖像轉換為灰階以提高識別準確性。
  • Read:此方法用於從預處理的圖像中提取文字。
Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 6,151,372 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動?

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