跳至页脚内容
与其他组件比较

IronOCR 和 Dynamsoft OCR 之间的比较

光学字符识别,或 OCR,是一种数据输入过程,涉及识别和数字化文本,包括手写和打印文本。 这是一种计算机技术,通过图像分析将打印文本的数字照片转换为可以被其他程序使用的字母和数字,例如文字处理器。 文本被转换为字符代码,以便可以在计算机上搜索和更改。

过去的世界是一个所有文件都是实体的世界,未来可能是一个所有文件都数字化的社会,而现在正处于变化中。 在这个过渡状态中,物理和数字文件共存——因此像 OCR 这样的技术对于前后转换至关重要。

文件恢复、数据输入和可访问性只是 OCR 应用的一部分。 大多数 OCR 应用程序来自扫描文档,尽管有时也使用照片。 OCR 是一个有价值的时间节省工具,因为重新输入材料通常是唯一的其他选择。 以下是一些 OCR 使用示例:

  • 可编辑的文本文件可以从扫描的文档中恢复,包括传真。
  • 使用书籍扫描创建可搜索和可编辑的电子书。
  • 使用截图照片搜索和更改文本。
  • 文本到语音技术用于向视障人士阅读书籍。

尽管这些只是 OCR 应用的几个示例,但它们展示了技术在广泛行业中的多样性。 几乎所有公司的员工每天都大量依赖文档,因此商业用途是 OCR 系统开发中的一个重要考虑因素。

在本文中,我们将比较两个最强大的 OCR 阅读器:

  • IronOCR
  • Dynamsoft OCR

IronOCR 和 Dynamsoft OCR 是两个 .NET OCR 库,它们支持扫描图像的转换和 PDF 文件的 OCR 处理。 您可以只需几行代码将图像转换为可搜索的文本。 您还可以检索单个单词,字母和段落。

IronOCR — 杰出的功能

IronOCR 拥有独特的能力,能够检测,读取和解释尚未精确扫描的图片和 PDF 文档中的文本。 IronOCR 提供了从文档和照片中提取文本的最简单方法,即使这并不总是最快,因为它会自动锐化和校正低质量扫描,减少倾斜,失真,背景噪声和视角问题,同时提高分辨率和对比度。

IronOCR 允许开发人员发送单页或多页扫描图像,并返回所有的文本、条形码和 QR 信息。 OCR 库中的一组类为基于网络的,桌面或控制台应用程序提供 OCR 功能。 Tesseract OCR C# 以及 net 应用程序 JPG、PNG、TIFF、PDF、GIF 和 BMP 只是可以用作输入的几种格式。

IronOCR 的光学字符识别 (OCR) 引擎可以读取使用许多常见字体、斜体、粗体和下划线准备的文本。 裁剪类使 OCR 能够快速而精确地工作。 当处理多页文档时,IronOCR 的多线程引擎加速了 OCR。

IronOCR 功能

对于 Tesseract 管理,我们使用 IronOCR,因为它在以下几个方面很独特:

  • 在纯粹的 .NET 中即开即用
  • 无需在您的计算机上安装 Tesseract
  • 运行最新的引擎:Tesseract 5(以及 Tesseract 4 & 3)
  • 适用于任何 .NET 项目:.NET Framework 4.5 +, .NET Standard 2 + 和 .NET Core 2, 3 & .NET 5
  • 与传统 Tesseract 相比,准确性和速度都有所提高
  • 支持 Xamarin、Mono、Azure 和 Docker
  • 使用 NuGet 包管理复杂的 Tesseract 词典系统
  • 支持 PDF、MultiFrame Tiffs 和所有主要图像格式,无需配置
  • 可以纠正低质量和倾斜的扫描,以从 Tesseract 中获得最佳结果。

Dynamsoft OCR — 功能

Dynamsoft.NET OCR 库是一个 .NET 组件,提供快速可靠的光学字符识别。 它用于使用 C# 或 VB.NET 创建 .NET 桌面应用程序。 您可以简单地创建代码,将 PDF 或照片中的无用文本转换为数字文本,便于编辑、搜索、存档等。

图像从扫描仪和其他支持 TWAIN 的设备获取的方式:

  • 支持本地、缓冲内存和磁盘文件图像传输机制。
  • 使用自动文档进纸器,可以进行批量扫描(ADF)。
  • 可以使用 TWAIN 属性修改常见设备功能。
  • IfAutoFeed, IfAutoScan、分辨率、位深、亮度、对比度、单位、双面打印和其他功能都可以更改。
  • 支持检测空白页。
  • 允许您更改和保存扫描仪配置文件。

从符合 UVC 和 WIA 的网络摄像头捕获图像:

  • 显示实时视频流,同时从选定的网络摄像头捕获照片。
  • 自定义相机设置:亮度、对比度、色调、饱和度、锐度、伽玛、白平衡、背光补偿、增益、色彩启用、变焦、对焦、曝光、光圈、平移、倾斜、旋转。

稳健的图像加载/查看

  • 可以载入 BMP、JPEG、PNG、TIFF 和多页 TIFF 格式的图像。
  • 支持放大和缩小照片。
  • 可以从本地驱动器、FTP 服务器、HTTP 服务器或数据库中获取图像。
  • 使用最全面的 .NET 图像组件之一对 BMP、JPEG、PNG 和 TIFF 进行图像解码

保存和上传/下载

  • 允许您通过文件流读取和写入照片。
  • 支持将捕获的照片保存为 BMP、JPEG、PNG、TIFF 或多页 TIFF 到本地驱动器、网络服务器或数据库。
  • 支持 RLE, G3/G4, LZW, PackBits 和 TIFF 压缩。
  • 支持 HTTPS 上传和下载。
  • 可用的最广泛的 .NET 图像组件之一,支持 BMP、JPEG、PNG 和 TIFF 图像编码。
  • 允许您将新获取的照片附加到现有的 TIFF 文件中。

读取扫描 PDF 或其他图像中的文本在 ASP.NET(光学字符识别)

在当今快节奏的世界中,客户希望工作能够快速完成。 客户经常与我们联系紧急项目。 我们的技术可以简单地识别图片的内容并将其转换为文本,如果项目涉及扫描包含图像的文件。 光学字符识别(OCR)为您的公司节省时间和金钱,同时减少数据输入错误。

使用 IronOCR

IronOCR 使用 IronOcr.IronTesseract 类进行其 OCR 转换。

我们使用 IronOcr.IronTesseract 类从图像中读取文本,并在此基本示例中自动以字符串形式返回其结果。

// PM> Install-Package IronOcr
using IronOcr;

class Program
{
    static void Main(string[] args)
    {
        // Create a new instance of the IronTesseract class
        var ironOcr = new IronTesseract();

        // Read the text from the image
        var result = ironOcr.Read(@"img\Screenshot.png");

        // Output the text to the console
        Console.WriteLine(result.Text);
    }
}
// PM> Install-Package IronOcr
using IronOcr;

class Program
{
    static void Main(string[] args)
    {
        // Create a new instance of the IronTesseract class
        var ironOcr = new IronTesseract();

        // Read the text from the image
        var result = ironOcr.Read(@"img\Screenshot.png");

        // Output the text to the console
        Console.WriteLine(result.Text);
    }
}
' PM> Install-Package IronOcr
Imports IronOcr

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Create a new instance of the IronTesseract class
		Dim ironOcr = New IronTesseract()

		' Read the text from the image
		Dim result = ironOcr.Read("img\Screenshot.png")

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

因此,以下段落的准确性是 100%:

IronOCR 简单示例

在这个简单的示例中,我们将测试我们 C# OCR 库的准确性以从 PNG 图像中读取文本
图像。 这是一个非常基本的测试,但随着教程的推进,事情会变得更加复杂。

敏捷的棕色狐狸跳过懒惰的狗

虽然在表面上看起来很简单,但在表面下有着复杂的行为:扫描图像以进行对齐、质量和分辨率,查看其属性,优化 OCR 引擎,最后像人一样读取文本。

OCR 对机器来说是一项艰巨的任务,读取速度可能与人类相当。 换句话说,OCR 不是一个快速的过程。 在这种情况下,这绝对是正确的。

C# OCR 应用程序结果准确性

在大多数真实世界的情况下,开发人员希望他们的项目尽可能快地运行。 在这种情况下,我们建议您使用 IronOCR 插件命名空间的 OcrInput 和 IronTesseract 类。

您可以使用 OcrInput 设置 OCR 作业的确切特征,例如:

  • JPEG、TIFF、GIF、BMP 和 PNG 只是可以使用的图像格式之一
  • 完整或部分导入 PDF 文档
  • 增加图像的对比度、分辨率和尺寸
  • 旋转,扫描噪声,数字噪声,倾斜和负片校正

IronTesseract

从数百种预打包语言和方言中选择

  • 立即使用 Tesseract 5、4 或 3 OCR 引擎
  • 如果我们正在查看截图、片段或整个文档,请指定文档类型
  • 识别条形码
  • 可搜索的 PDF、Hocr HTML, DOM 和字符串都是 OCR 结果的选项
using IronOcr;

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of IronTesseract
        var ocr = new IronTesseract();

        // Use the OcrInput class to read from an image file
        using (var input = new OcrInput(@"img\Potter.tiff"))
        {
            // Perform the OCR operation
            var result = ocr.Read(input);

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

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of IronTesseract
        var ocr = new IronTesseract();

        // Use the OcrInput class to read from an image file
        using (var input = new OcrInput(@"img\Potter.tiff"))
        {
            // Perform the OCR operation
            var result = ocr.Read(input);

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

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Create an instance of IronTesseract
		Dim ocr = New IronTesseract()

		' Use the OcrInput class to read from an image file
		Using input = New OcrInput("img\Potter.tiff")
			' Perform the OCR operation
			Dim result = ocr.Read(input)

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

我们甚至可以在中等质量的扫描上使用它,准确度为 100%。

C# OCR 从 TIFF 扫描示例

如您所见,从扫描图像(例如 TIFF)读取文本(如果需要,还可以读取条形码)相对简单。 此 OCR 作业的准确度为 100%。

接下来,我们将尝试同一页面的更低质量扫描,低 DPI 和大量失真和数字噪声以及原始文档损坏。

C# OCR 低分辨率扫描带数字噪声

这就是 IronOCR 真正发挥作用的地方,与其他 OCR 库如 Tesseract 相比,我们会发现其他 OCR 项目避免讨论在真实世界的扫描图像上使用 OCR,而不是为了实现 100% 的 OCR 精度,利用数字创建的不切实际的"完美"测试案例。

using IronOcr;

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of IronTesseract
        var ocr = new IronTesseract();

        // Use the OcrInput class to read from a low-quality image file
        using (var input = new OcrInput(@"img\Potter.LowQuality.tiff"))
        {
            // Deskew the image to improve accuracy
            input.Deskew();

            // Perform the OCR operation
            var result = ocr.Read(input);

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

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of IronTesseract
        var ocr = new IronTesseract();

        // Use the OcrInput class to read from a low-quality image file
        using (var input = new OcrInput(@"img\Potter.LowQuality.tiff"))
        {
            // Deskew the image to improve accuracy
            input.Deskew();

            // Perform the OCR operation
            var result = ocr.Read(input);

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

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Create an instance of IronTesseract
		Dim ocr = New IronTesseract()

		' Use the OcrInput class to read from a low-quality image file
		Using input = New OcrInput("img\Potter.LowQuality.tiff")
			' Deskew the image to improve accuracy
			input.Deskew()

			' Perform the OCR operation
			Dim result = ocr.Read(input)

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

不添加 Input.Deskew() 来矫正图像时,我们得到的准确率为 52.5%。 这还不够好。

添加 Input.Deskew() 将我们带到 99.8% 的准确度,这几乎和高质量扫描的 OCR 一样准确。

使用 Dynamsoft OCR

我们将展示一些代码片段,用于使用 Dynamic Web TWAIN 在 JavaScript 中执行 TWAIN 扫描和客户端 OCR。

扫描图像

您可以更改扫描设置并使用 Dynamic Web TWAIN 的简单 API 从 TWAIN 扫描仪中获取照片。

function acquireImage() {
    // Select an available TWAIN scanner
    DWObject.SelectSourceByIndex(document.getElementById("source").selectedIndex);

    // Set scanning settings like pixel type, resolution, ADF, etc.
    DWObject.IfShowUI = false; // Do not show the user interface of the scanner
    DWObject.PixelType = 1; // Scan in grayscale
    DWObject.Resolution = 300;
    DWObject.IfFeederEnabled = true; // Scan from auto feeder
    DWObject.IfDuplexEnabled = false;
    DWObject.IfDisableSourceAfterAcquire = true;

    // Acquire images from scanners
    DWObject.AcquireImage();
}
function acquireImage() {
    // Select an available TWAIN scanner
    DWObject.SelectSourceByIndex(document.getElementById("source").selectedIndex);

    // Set scanning settings like pixel type, resolution, ADF, etc.
    DWObject.IfShowUI = false; // Do not show the user interface of the scanner
    DWObject.PixelType = 1; // Scan in grayscale
    DWObject.Resolution = 300;
    DWObject.IfFeederEnabled = true; // Scan from auto feeder
    DWObject.IfDuplexEnabled = false;
    DWObject.IfDisableSourceAfterAcquire = true;

    // Acquire images from scanners
    DWObject.AcquireImage();
}
JAVASCRIPT

下载 OCR 专业模块

要使用 OCR 专业模块进行客户端 OCR,您需要在头部包含 ocrpro.js 并下载 OCR Pro DLL。

<script type="text/javascript" src="Resources/addon/dynamsoft.webtwain.addon.ocrpro.js"></script>
<script type="text/javascript" src="Resources/addon/dynamsoft.webtwain.addon.ocrpro.js"></script>
HTML

对 .js 文件进行编辑:

// Define base path
var CurrentPathName = unescape(location.pathname);
var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);

// Download the OCR Pro add-on
DWObject.Addon.OCRPro.Download(CurrentPath + "Resources/addon/OCRPro.zip", OnSuccess, OnFailure);
// Define base path
var CurrentPathName = unescape(location.pathname);
var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);

// Download the OCR Pro add-on
DWObject.Addon.OCRPro.Download(CurrentPath + "Resources/addon/OCRPro.zip", OnSuccess, OnFailure);
JAVASCRIPT

使用 OCR 识别文本

使用 JS OCR 识别 API 从扫描图像中提取文本就像插入下面的代码一样简单。

// Recognize text from the image at index 0
DWObject.Addon.OCRPro.Recognize(0, GetOCRProInfo, GetErrorInfo); // 0 is the index of the image
// Recognize text from the image at index 0
DWObject.Addon.OCRPro.Recognize(0, GetOCRProInfo, GetErrorInfo); // 0 is the index of the image
JAVASCRIPT

读取图像的裁剪区域

这两组软件都提供了裁剪图像进行 OCR 的解决方案。

使用 IronOCR 读取裁剪区域

Iron 的 Tesseract OCR 分支擅长读取图像的特定区域,如下代码示例所示。

我们使用 System.Drawing.Rectangle 来描述图像要读取的确切像素区域。

当处理填写的标准化表格时,这可以非常方便,而只有部分内容会因情况而异。

扫描页面的一部分:我们使用 System.Drawing.Rectangle 来指定从文档中读取的区域,提高速度和准确性。

C# OCR 从 TIFF 扫描示例
C# OCR 从 TIFF 扫描示例
using IronOcr;
using System.Drawing;

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of IronTesseract
        var ocr = new IronTesseract();

        using (var input = new OcrInput())
        {
            // Define content area of interest
            var contentArea = new Rectangle() { X = 215, Y = 1250, Height = 280, Width = 1335 };

            // Add the specific region to the input
            input.AddImage("img/ComSci.png", contentArea);

            // Perform OCR operation
            var result = ocr.Read(input);

            // Output recognized text to console
            Console.WriteLine(result.Text);
        }
    }
}
using IronOcr;
using System.Drawing;

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of IronTesseract
        var ocr = new IronTesseract();

        using (var input = new OcrInput())
        {
            // Define content area of interest
            var contentArea = new Rectangle() { X = 215, Y = 1250, Height = 280, Width = 1335 };

            // Add the specific region to the input
            input.AddImage("img/ComSci.png", contentArea);

            // Perform OCR operation
            var result = ocr.Read(input);

            // Output recognized text to console
            Console.WriteLine(result.Text);
        }
    }
}
Imports IronOcr
Imports System.Drawing

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Create an instance of IronTesseract
		Dim ocr = New IronTesseract()

		Using input = New OcrInput()
			' Define content area of interest
			Dim contentArea = New Rectangle() With {
				.X = 215,
				.Y = 1250,
				.Height = 280,
				.Width = 1335
			}

			' Add the specific region to the input
			input.AddImage("img/ComSci.png", contentArea)

			' Perform OCR operation
			Dim result = ocr.Read(input)

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

这导致速度提高了 41%,同时也使我们可以更具针对性。 这对于涉及可比较且一致的文档的 .NET OCR 应用程序极为重要,包括发票、收据、支票、表格、费用索赔等。

在读取 PDF 时,也支持 ContentAreas(OCR 裁剪)。

使用 Dynamsoft OCR 读取裁剪区域

首先,启动 Visual Studio 并创建一个新的 C# Windows 窗体应用程序,或打开现有的应用程序。 我们需要包含 DynamicDotNetTWAIN.dll, DynamicOCR.dll 和适当的语言包。

  1. 导航到 Tools -> Choose Toolbox Items,然后转到 .NET Framework Components 选项卡,单击浏览按钮,找到 DynamicDotNetTWAIN.dll

  2. 右键单击解决方案资源管理器中的项目文件,选择 Add-> Existing Item... 然后从 OCR 资源目录添加必要的项目。

这是点击 LoadImage 按钮的代码:

private void button1_Click(object sender, EventArgs e) 
{
    OpenFileDialog filedlg = new OpenFileDialog(); 
    if (filedlg.ShowDialog() == DialogResult.OK) 
    { 
        dynamicDotNetTwain1.LoadImage(filedlg.FileName);
        // Choose an image from your local disk and load it into Dynamic .NET TWAIN
    } 
}

private void dynamicDotNetTwain1_OnImageAreaSelected(short sImageIndex, int left, int top, int right, int bottom) 
{
    dynamicDotNetTwain1.OCRTessDataPath = "../../"; 
    dynamicDotNetTwain1.OCRLanguage = "eng";
    OcrResultFormat ocrResultFormat = Dynamsoft.DotNet.TWAIN.OCR.ResultFormat.Text;

    byte [] sbytes = dynamicDotNetTwain1.OCR(dynamicDotNetTwain1.CurrentImageIndexInBuffer, left, top, right, bottom);
    // OCR the selected area of the image

    if (sbytes != null) 
    { 
        SaveFileDialog filedlg = new SaveFileDialog(); 
        filedlg.Filter = "Text File(*.txt)| *.txt"; 
        if (filedlg.ShowDialog() == DialogResult.OK) 
        {
            FileStream fs = File.OpenWrite(filedlg.FileName); 
            fs.Write(sbytes, 0, sbytes.Length);
            // Save the OCR result as a text file
            fs.Close(); 
        }
        MessageBox.Show("OCR successful");
    } 
    else 
    {
        MessageBox.Show(dynamicDotNetTwain1.ErrorString); 
    }
}
private void button1_Click(object sender, EventArgs e) 
{
    OpenFileDialog filedlg = new OpenFileDialog(); 
    if (filedlg.ShowDialog() == DialogResult.OK) 
    { 
        dynamicDotNetTwain1.LoadImage(filedlg.FileName);
        // Choose an image from your local disk and load it into Dynamic .NET TWAIN
    } 
}

private void dynamicDotNetTwain1_OnImageAreaSelected(short sImageIndex, int left, int top, int right, int bottom) 
{
    dynamicDotNetTwain1.OCRTessDataPath = "../../"; 
    dynamicDotNetTwain1.OCRLanguage = "eng";
    OcrResultFormat ocrResultFormat = Dynamsoft.DotNet.TWAIN.OCR.ResultFormat.Text;

    byte [] sbytes = dynamicDotNetTwain1.OCR(dynamicDotNetTwain1.CurrentImageIndexInBuffer, left, top, right, bottom);
    // OCR the selected area of the image

    if (sbytes != null) 
    { 
        SaveFileDialog filedlg = new SaveFileDialog(); 
        filedlg.Filter = "Text File(*.txt)| *.txt"; 
        if (filedlg.ShowDialog() == DialogResult.OK) 
        {
            FileStream fs = File.OpenWrite(filedlg.FileName); 
            fs.Write(sbytes, 0, sbytes.Length);
            // Save the OCR result as a text file
            fs.Close(); 
        }
        MessageBox.Show("OCR successful");
    } 
    else 
    {
        MessageBox.Show(dynamicDotNetTwain1.ErrorString); 
    }
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
	Dim filedlg As New OpenFileDialog()
	If filedlg.ShowDialog() = DialogResult.OK Then
		dynamicDotNetTwain1.LoadImage(filedlg.FileName)
		' Choose an image from your local disk and load it into Dynamic .NET TWAIN
	End If
End Sub

Private Sub dynamicDotNetTwain1_OnImageAreaSelected(ByVal sImageIndex As Short, ByVal left As Integer, ByVal top As Integer, ByVal right As Integer, ByVal bottom As Integer)
	dynamicDotNetTwain1.OCRTessDataPath = "../../"
	dynamicDotNetTwain1.OCRLanguage = "eng"
	Dim ocrResultFormat As OcrResultFormat = Dynamsoft.DotNet.TWAIN.OCR.ResultFormat.Text

	Dim sbytes() As Byte = dynamicDotNetTwain1.OCR(dynamicDotNetTwain1.CurrentImageIndexInBuffer, left, top, right, bottom)
	' OCR the selected area of the image

	If sbytes IsNot Nothing Then
		Dim filedlg As New SaveFileDialog()
		filedlg.Filter = "Text File(*.txt)| *.txt"
		If filedlg.ShowDialog() = DialogResult.OK Then
			Dim fs As FileStream = File.OpenWrite(filedlg.FileName)
			fs.Write(sbytes, 0, sbytes.Length)
			' Save the OCR result as a text file
			fs.Close()
		End If
		MessageBox.Show("OCR successful")
	Else
		MessageBox.Show(dynamicDotNetTwain1.ErrorString)
	End If
End Sub
$vbLabelText   $csharpLabel

这是应用程序的外观:

使用动态 .NET TWAIN OCR SDK 的 Zone OCR 演示应用程序

图像性能优化

输入图像的质量是 OCR 任务速度的最关键决定因素。 背景噪音越低,dpi 越高,目标值约为 200 dpi,OCR 输出的速度和准确性越高。

用于 Dynamsoft OCR 的图像处理技术

我们需要在各种情况下使用 OCR,例如使用我们的手机扫描信用卡号码,或从纸质文档中提取文本。 Dynamsoft 标签识别 (DLR) 和 Dynamic Web TWAIN (DWT) 中包含 OCR 功能。

虽然它们在总体上可以做得很好,但我们可以通过各种图像处理技术来提高结果。

减轻/去除阴影

光照不良可能会影响 OCR 结果。 为了改善结果,我们可以美白图像或消除图像中的阴影。

Invert

由于 OCR 引擎通常针对深色文本进行训练,浅色文本可能更难被发现和识别。

轻文字

我们可以更容易识别其颜色。

浅色文字反转

要进行反转,我们可以在 DLR 中使用 GrayscaleTransformationModes 参数。

这里是 JSON 设置:

"GrayscaleTransformationModes": [
    {
        "Mode": "DLR_GTM_INVERTED"
    }
]

DLR .net 的读取结果:

浅色文本结果

Rescale

如果字母高度太低,OCR 引擎可能不会产生好的结果。 一般而言,图像应具有至少 300 的 DPI。

DLR 1.1 中有一个 ScaleUpModes 参数,可以让您放大字母。 当然,我们也可以自己缩放图像。

直接读取图像会产生错误的结果:

1x 图像

将图像放大 x2 后,结果正确:

2倍图像

Deskew

如果文本有点扭曲,这没关系。 然而,如果它太倾斜,结果会受到负面影响。 为了改善结果,我们需要裁剪图像。

为此,我们可以使用 OpenCV 和 Python 中的 Hough Line Transform。

这是对上述图像去斜的代码:

import cv2
import numpy as np
import math
from PIL import Image

def deskew():
    src = cv2.imread("neg.jpg", cv2.IMREAD_COLOR)
    gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
    kernel = np.ones((5,5), np.uint8)
    erode_img = cv2.erode(gray, kernel)
    eroDil = cv2.dilate(erode_img, kernel)
    show_and_wait_key("eroDil", eroDil)

    canny = cv2.Canny(eroDil, 50, 150)
    show_and_wait_key("canny", canny)

    lines = cv2.HoughLinesP(canny, 0.8, np.pi / 180, 90, minLineLength=100, maxLineGap=10)
    drawing = np.zeros(src.shape[:], dtype=np.uint8)

    maxY = 0
    degree_of_bottomline = 0
    index = 0
    for line in lines:
        x1, y1, x2, y2 = line[0]
        cv2.line(drawing, (x1, y1), (x2, y2), (0, 255, 0), 1, lineType=cv2.LINE_AA)
        k = float(y1-y2)/(x1-x2)
        degree = np.degrees(math.atan(k))
        if index == 0:
            maxY = y1
            degree_of_bottomline = degree
        else:        
            if y1 > maxY:
                maxY = y1
                degree_of_bottomline = degree
        index += 1
    show_and_wait_key("houghP", drawing)

    img = 图像。fromarray(src)
    rotate_img = img.rotate(degree_of_bottomline)
    rotate_img_cv = np.array(rotate_img)
    cv2.imshow("rotateImg", rotate_img_cv)
    cv2.imwrite("deskewed.jpg", rotate_img_cv)
    cv2.waitKey()

def show_and_wait_key(win_name, img):
    cv2.imshow(win_name, img)
    cv2.waitKey()

if __name__ == "__main__":
    deskew()
import cv2
import numpy as np
import math
from PIL import Image

def deskew():
    src = cv2.imread("neg.jpg", cv2.IMREAD_COLOR)
    gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
    kernel = np.ones((5,5), np.uint8)
    erode_img = cv2.erode(gray, kernel)
    eroDil = cv2.dilate(erode_img, kernel)
    show_and_wait_key("eroDil", eroDil)

    canny = cv2.Canny(eroDil, 50, 150)
    show_and_wait_key("canny", canny)

    lines = cv2.HoughLinesP(canny, 0.8, np.pi / 180, 90, minLineLength=100, maxLineGap=10)
    drawing = np.zeros(src.shape[:], dtype=np.uint8)

    maxY = 0
    degree_of_bottomline = 0
    index = 0
    for line in lines:
        x1, y1, x2, y2 = line[0]
        cv2.line(drawing, (x1, y1), (x2, y2), (0, 255, 0), 1, lineType=cv2.LINE_AA)
        k = float(y1-y2)/(x1-x2)
        degree = np.degrees(math.atan(k))
        if index == 0:
            maxY = y1
            degree_of_bottomline = degree
        else:        
            if y1 > maxY:
                maxY = y1
                degree_of_bottomline = degree
        index += 1
    show_and_wait_key("houghP", drawing)

    img = 图像。fromarray(src)
    rotate_img = img.rotate(degree_of_bottomline)
    rotate_img_cv = np.array(rotate_img)
    cv2.imshow("rotateImg", rotate_img_cv)
    cv2.imwrite("deskewed.jpg", rotate_img_cv)
    cv2.waitKey()

def show_and_wait_key(win_name, img):
    cv2.imshow(win_name, img)
    cv2.waitKey()

if __name__ == "__main__":
    deskew()
PYTHON

检测到的线条:

检测到线路

去斜后:

图像倾斜

用于 IronOCR 的图像处理技术

这里输入图像的质量并不重要,因为 IronOCR 擅长修复有缺陷的文件(虽然这需要时间,并会导致您的 OCR 作业消耗更多的 CPU 周期)。

选择具有较少数字噪声的输入图像格式,如 TIFF 或 PNG,与 JPEG 等有损图像格式相比,还可以获得更快的结果。

下面列出的图像过滤器可以显著提升性能:

  • OcrInput.Rotate(double degrees) — 按顺时针旋转图像指定角度。 负整数用于逆时针旋转。
  • OcrInput.Binarize() — 此图像过滤器将每个像素变为黑色或白色,没有中间状态。 在文本与背景对比度非常低的情况下,可能会提高 OCR 性能。
  • OcrInput.ToGrayScale() — 此图像过滤器将每个像素转换为灰度色调。 这不太可能提高 OCR 准确性,但可能会提高速度。
  • OcrInput.Contrast() — 自动增加对比度。在低对比度扫描中,此过滤器经常提高 OCR 的速度和准确性。
  • OcrInput.DeNoise() — 该过滤器仅在预期有噪音时使用。
  • OcrInput.Invert() — 反转所有颜色。 例如,白色变成黑色:黑色变成白色。
  • OcrInput.Dilate() — 高级形态学。 扩张是在图像对象的边缘添加像素的过程。 (Inverse of Erode)
  • OcrInput.Erode() — 高级形态学功能。 侵蚀是从对象的边缘移除像素的过程。 (Inverse of Dilate)
  • OcrInput.Deskew() — 将图像旋转,使其正交并正确朝上。Tesseract 的倾斜扫描容忍度可能低至 5 度,这对 OCR 非常有用。
  • DeepCleanBackgroundNoise() — 去除大量背景噪声。只有在文件中确实有大量背景噪声时才使用此过滤器,因为它会降低清晰文档上的 OCR 准确性,并且会消耗大量 CPU。
  • OcrInput.EnhanceResolution — 改善低分辨率照片的分辨率。 由于 OcrInput,此过滤器很少使用。 OcrInput 将自动检测和解决低分辨率问题。

我们可能希望使用 IronTesseract 加速高质量扫描的 OCR。 如果我们追求速度,我们可以从这里开始,然后逐步开启特性,直到达到适当的平衡。

using IronOcr;

class Program
{
    static void Main(string[] args)
    {
        var ocr = new IronTesseract();

// Configuration for speed tuning
        ocr.Configuration.BlackListCharacters = "~`$#^*_}{][|\\";
        ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto;
        ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
        ocr.Configuration.EngineMode = TesseractEngineMode.LstmOnly;
        ocr.Language = OcrLanguage.EnglishFast;

        using (var input = new OcrInput(@"img\Potter.tiff"))
        {
            var result = ocr.Read(input);
            Console.WriteLine(result.Text);
        }
    }
}
using IronOcr;

class Program
{
    static void Main(string[] args)
    {
        var ocr = new IronTesseract();

// Configuration for speed tuning
        ocr.Configuration.BlackListCharacters = "~`$#^*_}{][|\\";
        ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto;
        ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
        ocr.Configuration.EngineMode = TesseractEngineMode.LstmOnly;
        ocr.Language = OcrLanguage.EnglishFast;

        using (var input = new OcrInput(@"img\Potter.tiff"))
        {
            var result = ocr.Read(input);
            Console.WriteLine(result.Text);
        }
    }
}
Imports IronOcr

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		Dim ocr = New IronTesseract()

' Configuration for speed tuning
		ocr.Configuration.BlackListCharacters = "~`$#^*_}{][|\"
		ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto
		ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5
		ocr.Configuration.EngineMode = TesseractEngineMode.LstmOnly
		ocr.Language = OcrLanguage.EnglishFast

		Using input = New OcrInput("img\Potter.tiff")
			Dim result = ocr.Read(input)
			Console.WriteLine(result.Text)
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

这一结果与基准100% 相比准确99.8%——但快了 35%。

授权和定价

Dynamsoft 许可证及定价

每年许可证。 所有价格包括一年的维护,其中包括免费软件升级和高级支持。

Dynamsoft 提供两种类型的许可证:

每客户端设备许可证

"一个客户端设备许可证"提供访问同源应用程序(同协议、同主机和同端口),以使用软件功能,来自单个客户端设备。一个不活动的客户端设备是指连续 90 天内未访问过任何软件功能的设备。 一个不活跃的客户端设备的许可证席位将立即被释放并提供给任何其他活跃的客户端设备使用。当您达到允许的最大许可证席位数时,Dynamsoft 将为您提供客户端设备补贴的额外 10%,供紧急使用。 一旦额外的客户端设备补助用完,在有关联的许可证席位可用之前,将无法访问和使用该软件。 请注意,超出您的客户端设备补助不会对已获得许可证的任何客户端设备产生影响。

每服务器许可证

要将应用程序部署到单个服务器,需要"一台服务器许可证"。 服务器是指物理服务器和虚拟服务器,包括但不限于,生产服务器、故障转移服务器、用于测试的开发服务器、质量保证服务器、测试服务器和阶段服务器,所有这些都需要许可证。 连续集成服务器(构建服务器)或本地主机开发服务器不需要额外的许可证。 每服务器许可证仅适用于本地服务器安装,不适用于云部署。

Dynamsoft OCR 的定价从 1249 美元/年开始。

IronOCR 许可证及定价

作为开发人员,我们都希望以尽可能少的钱和资源完成我们的项目——预算是至关重要的。 查看图表,以确定哪种许可证最适合您的需求和预算。

IronOCR 提供具有可定制数量的开发人员、项目和位置的许可证,允许您满足项目的需求,同时只支付您所需的覆盖范围。

IronOCR 的许可证密钥允许您发布没有水印的产品。

许可证从 $799 开始,并包括一年的支持和升级。

您还可以使用试用许可证密钥免费试用 IronOCR。

结论

Mac、Windows、Linux、Azure OCR 和 Docker 上的 Tesseract OCR 都可以通过 IronOCR for C# 使用。 .NET Framework 4.0 或更高版本是必需的,.NET Standard 2.0+,.NET Core 2.0+,.NET 5,Mono for macOS 和 Linux,以及 Xamarin for macOS 都是跨平台发展的例子。 IronOCR 还使用最新的 Tesseract 5 引擎从所有主要图像和 PDF 格式读取文本、条形码和 QR 码。 在几分钟内,这个库就可以为您的桌面、控制台或 Web 应用添加 OCR 功能!OCR 还可以读取 PDF 和多页 TIFF,并可以保存在任何 OCR 扫描中的可搜索 PDF 文档或 XHTML。 其数据输出选项包括纯文本,条形码数据,以及包含段落、行、单词和字符的 OCR 结果类。 提供 125 种语言,包括阿拉伯语、中文、英语、芬兰语、法语、德语、希伯来语、意大利语、日语、韩语、葡萄牙语、俄语和西班牙语,但请注意,还可以生成定制语言包。

Dynamic .NET TWAIN OCR 附件是一个快速可靠的 .NET 组件,用于光学字符识别,您可以在 C# 或 VB .NET 编写的 WinForms 和 WPF 应用程序中使用。 您可以使用 Dynamic .NET TWAIN 的图像捕获模块扫描文档或从网络摄像头捕获照片,然后对图像进行 OCR,以将图像中的文本转换为文本、可搜索的 PDF 文件或字符串。 除了英语,还提供多种亚洲语言以及阿拉伯语。

IronOCR 提供比 Dynamsoft OCR 更好的许可证; IronOcr 从 $799 开始,包括一年免费,而 Dynamsoft 从 $1249 开始,包括免费试用。 IronOCR 还提供多个用户的许可证,而 Dynamsoft 只能为每个用户提供一个许可证。

尽管这两款软件都致力于在条形码、图像转换为文本和图像转文本的 OCR 阅读中提供最佳性能,但 IronOCR 脱颖而出,因为即使图像状况非常糟糕,它仍然发挥其光芒。 它自动实施其复杂的调优方法,为您提供最佳的 OCR 结果。 IronOCR 还利用 Tesseract,为您提供最佳结果,几乎没有错误。

Iron Software 还为其客户和用户提供选项,通过两次点击即可获取其整个软件套件。 这意味着以 Iron 软件套件中两个组件的价格,您现在可以获得所有五个组件以及不间断的支持。

请注意Dynamsoft OCR 是其各自所有者的注册商标。 本网站与 Dynamsoft OCR 无关、未获其认可或赞助。 所有产品名称、徽标和品牌均为各自所有者的财产。 比较仅供参考,反映的是撰写时的公开信息。

常见问题解答

我如何使用C#将文本图像转换为数字格式?

您可以使用C#中的IronOCR将文本图像转换为数字格式。IronOCR提供了自动锐化和纠正低质量扫描的方法,非常适合将各种图像格式转换为文本。

处理低质量扫描IronOCR有哪些优点?

IronOCR通过降低倾斜、失真和背景噪声,改善分辨率和对比度,自动增强低质量扫描,从而确保更高的文本识别准确性。

哪个OCR库更适合跨平台应用程序?

IronOCR适合跨平台应用程序,因为它支持Xamarin和Azure等环境,为在不同平台工作开发人员提供了灵活性。

IronOCR支持哪些图像格式?

IronOCR支持多种图像格式,使其在不同OCR应用中具有多功能性。它可以处理图像和PDF文档,为处理各种输入源提供灵活性。

OCR技术能否在企业的文档管理中提供帮助?

是的,OCR技术可以通过数字化物理文档,使其可以搜索和编辑,显著促进文档管理。这减少了手动数据输入,减少了错误,并提高了可访问性。

Dynamsoft OCR如何处理设备的图像获取?

Dynamsoft OCR支持从符合TWAIN标准的设备和网络摄像头获取图像,允许批量扫描和修改扫描仪属性以实现高效图像处理。

与其他库相比,IronOCR的定价选项是什么?

IronOCR提供可自定义的许可证,能够根据开发人员数量、项目和位置量身定制,与某些其他库相比,更具灵活性和成本效益的选择。

使用OCR技术时遇到的一些常见问题是什么?

OCR技术的常见问题包括处理低分辨率图像、失真和不同的文本字体。然而,像IronOCR这样的库拥有内置功能来解决这些问题,提高OCR的准确性。

Kannaopat Udonpant
软件工程师
在成为软件工程师之前,Kannapat 在日本北海道大学完成了环境资源博士学位。在攻读学位期间,Kannapat 还成为了车辆机器人实验室的成员,隶属于生物生产工程系。2022 年,他利用自己的 C# 技能加入 Iron Software 的工程团队,专注于 IronPDF。Kannapat 珍视他的工作,因为他可以直接从编写大多数 IronPDF 代码的开发者那里学习。除了同行学习外,Kannapat 还喜欢在 Iron Software 工作的社交方面。不撰写代码或文档时,Kannapat 通常可以在他的 PS5 上玩游戏或重温《最后生还者》。