使用 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
  • PdfWriter:此对象负责写入PDF文件。它作用于文件路径,并写入有效PDF文档所需的结构。
  • IronOcr :该库用于从图像中读取文本。
  • OcrInput :此类表示用于 OCR 处理的图像输入。
  • DeNoise :此方法用于减少图像中的背景噪声。
  • DeepCleanBackgroundNoise :如果基本DeNoise不佳,则采用此方法进行更彻底的降噪。
  • ToGrayScale :将图像转换为灰度图像,以提高识别准确率。
  • Read :调用此方法从预处理的图像中提取文本。
Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 5,167,857 | Version: 2025.11 刚刚发布