使用 IRONOCR

如何在 C# 中進行車牌 OCR(教程)

Kannaopat Udonpant
坎納帕特·烏頓潘
2023年2月26日
已更新 2024年11月12日
分享:

介紹

車牌識別已成為許多行業中不可或缺的工具,從交通管理和停車系統到執法和收費解決方案都依賴它。 通過利用光學字元識別(OCR)技術,開發人員可以有效地從圖像中提取文本,自動化識別車牌的過程。 在本教程中,我們將演示如何使用IronOCR,一個功能強大的 C# OCR 庫,來精確地從圖像中讀取車牌。 IronOCR 與用於計算機視覺任務的 OpenCV 原始碼無縫整合,提供一個強大的解決方案,即使是從複雜或噪聲影像來源中也能識別文字。 無論您是在處理清晰的車牌圖像還是完整的車輛照片,本指南將引導您使用現代OCR技術建立可靠的車牌識別系統的步驟。

如何使用車牌識別C

  1. 安裝C#函式庫以使用車牌識別功能

  2. 將車牌圖像導入新的 OcrImageInput 實例中

  3. 在 C# 中應用影像濾鏡以改善文字擷取

  4. 透過在照片中指定車牌區域來提高識別速度

  5. 使用 OcrLicensePlateResult 實例打印出提取的文本

開始使用IronOCR

IronOCR 是一個基於 Tesseract OCR 引擎的 C# OCR 函式庫,專門為 .NET 應用中的文本識別專案提供高準確性和效率而設計。 IronOCR 對於處理噪聲或低質量圖像非常理想,它包含強大的圖像預處理功能,如自動降噪和灰度轉換,這些功能可以增強文本提取的清晰度。

IronOCR 的一些突出功能包括:

  • 高 OCR 準確性:優化支持各種語言和字體,IronOCR 即使在處理複雜或扭曲的文字時,也能保持高度的準確性。
  • 影像與 PDF 支援:它能夠從多種影像格式和 PDF 文件中讀取文字,使其在不同文件類型中具有多功能性。
  • 與 OpenCV 的整合:透過 OpenCV 支援,IronOCR 可以執行電腦視覺任務,例如在圖像中檢測特定文字區域,這對於車牌識別特別有用。
  • 進階預處理:包含灰階轉換、旋轉、去偏斜和對比度增強的過濾器,以提高識別質量。
  • 靈活的輸入選項:支持多頁文檔和可調整的區域,讓開發人員可以將OCR處理集中於選定的區域,以獲得更快且更具針對性的結果。

    憑藉這些功能,IronOCR 是一個強大的解決方案,適用於構建需要準確性、靈活性以及易於與其他計算機視覺工具集成的 OCR 應用程序。

創建一個 Visual Studio 專案

首先,打開 Visual Studio,然後選擇「建立新專案」。 這將帶您進入一個頁面,您可以在那裡選擇要構建的專案類型(在我們的情況下,我們將製作一個控制台應用程式)。 選擇所需的應用程式類型,然後點選“下一步”。

License Plate Ocr Csharp Tutorial 1 related to 創建一個 Visual Studio 專案

現在,請為您的專案命名並選擇儲存位置。

License Plate Ocr Csharp Tutorial 2 related to 創建一個 Visual Studio 專案

最後,選擇您的目標 .NET 框架,然後按一下「Create」按鈕。 這將創建項目,如下所示。

License Plate Ocr Csharp Tutorial 3 related to 創建一個 Visual Studio 專案

下一步是安裝IronOCR庫,以便我們可以開始處理車牌。

安裝 IronOCR

要在您的 C# 專案中開始使用 IronOCR,您需要從 NuGet 安裝 IronOCR 套件。 IronOCR 與 .NET Framework 和 .NET Core 兼容,使其易於整合到各種 .NET 應用程式中。

步驟 1:開啟套件管理控制台

在 Visual Studio 中,導航到 工具 > NuGet 套件管理員 > 套件管理器主控台

步驟 2:安裝 IronOCR 套件

在封裝管理器主控台中輸入以下命令:

Install-Package IronOcr
Install-Package IronOcr
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronOcr
$vbLabelText   $csharpLabel

此命令將安裝 IronOCR 庫,包括在您的專案中運行 OCR 功能所需的所有依賴項。 由於我們的應用程式需要使用電腦視覺的進階功能,如車牌識別,您也可以這樣安裝可選的IronOcr.ComputerVision.Windows套件:

Install-Package IronOcr.ComputerVision.Windows
Install-Package IronOcr.ComputerVision.Windows
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronOcr.ComputerVision.Windows
$vbLabelText   $csharpLabel

並確保您已安裝 IronOcr.Extensions.AdvancedScan 擴展,以便您可以使用其強大的 ReadLicensePlate 方法:

Install-Package IronOcr.Extensions.AdvancedScan
Install-Package IronOcr.Extensions.AdvancedScan
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronOcr.Extensions.AdvancedScan
$vbLabelText   $csharpLabel

或者,您可以使用工具 > NuGet 套件管理員 > 管理解決方案的 NuGet 套件來安裝套件,並搜尋您需要的套件:

License Plate Ocr Csharp Tutorial 4 related to 步驟 2:安裝 IronOCR 套件

設定程式碼

最後,我們必須在程式碼的頂部添加必要的匯入和使用語句:

using IronOcr;
using IronOcr;
Imports IronOcr
$vbLabelText   $csharpLabel

使用光學字符識別讀取車牌號碼

在本節中,我們將使用IronOCR創建一個程序來讀取車牌,這是一個在從圖像中提取文字方面表現卓越的Tesseract OCR引擎。 為了實現車輛檢測,我們可能還需要整合其他機器學習庫。 值得注意的是,IronOCR 與 OpenCV 整合,這是一個領先的開源計算機視覺庫,使我們能夠執行物體檢測任務,比如識別車輛和車牌。

示例車牌圖片

我們將使用以下車牌:

License Plate Ocr Csharp Tutorial 5 related to 示例車牌圖片

接下來,新增以下程式碼來對車牌進行光學字符識別(OCR):

using IronOcr;

var ocr = new IronTesseract();

using (var input = new OcrImageInput("licensePlate.jpeg"))
{
    // Fixing the digital noise and making the image easier to read
    input.DeNoise();
    input.ToGrayScale();

    // Using the OcrLicensePlateResult and ReadLicensePlate methods to read the license plate information and store it for further use
    OcrLicensePlateResult result = ocr.ReadLicensePlate(input);

    // Saving the license plate text to a string variable
    string output = result.Text;

    // Outputting the license plate text to the console
    Console.WriteLine(output);
}
using IronOcr;

var ocr = new IronTesseract();

using (var input = new OcrImageInput("licensePlate.jpeg"))
{
    // Fixing the digital noise and making the image easier to read
    input.DeNoise();
    input.ToGrayScale();

    // Using the OcrLicensePlateResult and ReadLicensePlate methods to read the license plate information and store it for further use
    OcrLicensePlateResult result = ocr.ReadLicensePlate(input);

    // Saving the license plate text to a string variable
    string output = result.Text;

    // Outputting the license plate text to the console
    Console.WriteLine(output);
}
Imports IronOcr

Private ocr = New IronTesseract()

Using input = New OcrImageInput("licensePlate.jpeg")
	' Fixing the digital noise and making the image easier to read
	input.DeNoise()
	input.ToGrayScale()

	' Using the OcrLicensePlateResult and ReadLicensePlate methods to read the license plate information and store it for further use
	Dim result As OcrLicensePlateResult = ocr.ReadLicensePlate(input)

	' Saving the license plate text to a string variable
	Dim output As String = result.Text

	' Outputting the license plate text to the console
	Console.WriteLine(output)
End Using
$vbLabelText   $csharpLabel

License Plate Ocr Csharp Tutorial 6 related to 如何在 C# 中進行車牌 OCR(教程)

程式碼解析:

  • 初始化:這行程式碼 var ocr = new IronTesseract(); 建立一個新的 IronTesseract 類別實例,提供光學字元辨識(OCR)的方法。
  • 圖片輸入:using 語句創建了一個新的 OcrImageInput 對象,使用指定的圖像文件 "licensePlate.jpeg"。 此物件旨在保存用於OCR處理的圖像數據。
  • 影像預處理

    • input.DeNoise(); 應用數位去噪濾鏡以提高圖像質量,使得 OCR 引擎更容易讀取文字。
  • input.ToGrayScale(); 會將圖像轉換為灰階,這可以提高識別準確性和處理速度。
  • 車牌識別:OcrLicensePlateResult result = ocr.ReadLicensePlate(input); 使用 ReadLicensePlate 方法分析處理後的圖像並提取檢測到的任何車牌資訊,將結果存儲在 OcrLicensePlateResult 對象中。
  • 輸出存儲:車牌的文字通過訪問 result.Text 儲存在字串變數 output 中,此變數包含從車牌辨識出的文字。
  • 控制台輸出:最後,Console.WriteLine(output); 將提取的車牌文字列印到控制台以進行驗證。

掃描汽車的車牌號碼

如果我們有一整輛汽車的圖片而不僅僅是車牌,我們可以指定一個矩形區域來關注車牌區域。 我們可以使用System.Drawing.Rectangle來定義此區域的像素。

原始圖像

我們將使用下面的圖像檔作為範例:

License Plate Ocr Csharp Tutorial 7 related to 原始圖像

透過指定關注的區域,我們提升了處理速度並避免提取不必要的文本。

實施代碼

using IronOcr;
using System.Drawing;

var ocr = new IronTesseract();

using (var input = new OcrInput())
{
    var contentArea = new Rectangle(x: 252, y: 282, width: 148, height: 47);
    input.LoadImage("CarPlate.jpeg", contentArea);
    OcrLicensePlateResult result = ocr.ReadLicensePlate(input);
    Console.WriteLine(result.Text);
}
using IronOcr;
using System.Drawing;

var ocr = new IronTesseract();

using (var input = new OcrInput())
{
    var contentArea = new Rectangle(x: 252, y: 282, width: 148, height: 47);
    input.LoadImage("CarPlate.jpeg", contentArea);
    OcrLicensePlateResult result = ocr.ReadLicensePlate(input);
    Console.WriteLine(result.Text);
}
Imports IronOcr
Imports System.Drawing

Private ocr = New IronTesseract()

Using input = New OcrInput()
	Dim contentArea = New Rectangle(x:= 252, y:= 282, width:= 148, height:= 47)
	input.LoadImage("CarPlate.jpeg", contentArea)
	Dim result As OcrLicensePlateResult = ocr.ReadLicensePlate(input)
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

License Plate Ocr Csharp Tutorial 8 related to 實施代碼

程式碼解析:

  • 初始化:var ocr = new IronTesseract(); 創建一個新的 IronTesseract 類實例,負責執行 OCR 操作。
  • OCR Input:using (var input = new OcrInput()) 語句會創建一個新的 OcrInput 物件,它將被用來加載和處理圖片以進行 OCR。
  • 定義興趣區域:var contentArea = new Rectangle(x: 252, y: 282, width: 148, height: 47); 定義圖像中的矩形區域(contentArea)。 此矩形指定預期放置車牌的座標和尺寸(寬度和高度)。
  • 載入圖片:input.LoadImage("CarPlate.jpeg", contentArea); 加載指定的圖像文件 ("CarPlate.jpeg") 並聚焦於定義的矩形 (contentArea) 以將 OCR 處理限制在該特定區域。
  • 讀取車牌號碼:OcrLicensePlateResult result = ocr.ReadLicensePlate(input); 調用 ReadLicensePlate 方法,該方法分析輸入圖像中的車牌字符,並返回一個 OcrLicensePlateResult 對象,其中包含提取的文本。
  • 輸出: Console.WriteLine(result.Text); 將識別出的車牌文字打印到控制台。

車牌自動識別

IronOCR 利用 OpenCV 來識別圖像中的文本區域,採用多種影像處理技術。 此功能使程式能夠通過定位影像中的文字區域來偵測車牌,然後利用 Tesseract 來讀取這些區域。

安裝

要啟用車牌檢測模型,請透過套件管理器主控台安裝所需的套件:

var ocr = new IronTesseract();

using (var input = new OcrImageInput("CarPlate.jpeg"))
{
    input.FindTextRegion();
    OcrLicensePlateResult result = ocr.ReadLicensePlate(input);
    Console.WriteLine(result.Text);
}
var ocr = new IronTesseract();

using (var input = new OcrImageInput("CarPlate.jpeg"))
{
    input.FindTextRegion();
    OcrLicensePlateResult result = ocr.ReadLicensePlate(input);
    Console.WriteLine(result.Text);
}
Dim ocr = New IronTesseract()

Using input = New OcrImageInput("CarPlate.jpeg")
	input.FindTextRegion()
	Dim result As OcrLicensePlateResult = ocr.ReadLicensePlate(input)
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

License Plate Ocr Csharp Tutorial 9 related to 安裝

程式碼解析:

  • 初始化:創建一個 IronTesseract 類的實例,該實例將與 Tesseract 引擎一起用於光學字符識別 (OCR)。
  • 圖像輸入:使用指定的圖像檔案("CarPlate.jpeg")實例化了一個新的 OcrImageInput 對象。 此物件將作為 OCR 處理的輸入,並封裝在 using 語句中以確保正確的資源管理。
  • 文字區域檢測:在輸入對象上調用了FindTextRegion方法。 這個方法使用電腦視覺技術自動識別圖像中可能包含文本的區域,特別是針對車牌。
  • 車牌識別:調用 ReadLicensePlate 方法來分析偵測到的文本區域並提取車牌號碼。 結果存儲在 OcrLicensePlateResult 對象中,其中包含識別出的文本和任何相關的元數據。
  • 輸出:偵測到的車牌文本將被打印到控制台,讓用戶查看擷取的車牌號碼。

IronOCR 授權

License Plate Ocr Csharp Tutorial 10 related to IronOCR 授權

對於那些想親自試用IronOCR的人,IronOCR提供了免費試用,這讓您可以訪問其提供的所有工具,這意味著您可以在購買許可證之前,在您自己的專案中嘗試使用它們。 一旦您的免費試用期結束,IronOCR 授權起價僅為 $749,即可獲得 lite 授權。 它還提供可選的附加元件,需額外付費,例如免版稅的重新分發覆蓋以及不間斷的支持和持續的產品更新。

除此之外,如果您發現自己需要使用更多的 IronSoftware 產品,不僅僅是 IronOCR,例如,IronPDF 用於處理 PDF 任務或 IronWord 用於處理 Word 文件,那麼 IronSoftware 還提供 Iron Suite,這是一個以優惠價格獲取完整工具組的好方法。

結論

在本指南中,我們探討了如何使用IronOCR在 C# 中構建可靠的車牌識別系統。 IronOCR 擁有強大的文字提取功能,並整合了 OpenCV,為需要從車輛圖像中準確識別文字的應用程序提供了一個高效且易於使用的解決方案。 從圖像預處理到設置特定檢測區域,IronOCR 使用專為噪音大或複雜圖像(如交通和監控錄像中的車牌)設計的工具來簡化 OCR 過程。

無論您是在開發交通監控、停車執法,或是任何需要自動車牌識別的應用程式,IronOCR 提供了一個全面的程式庫,可以無縫整合到 .NET 環境中。 通過遵循這些步驟,您能夠部署搭載光學字符識別技術的解決方案,以提高各種現實場景的效率和準確性。 透過區域選擇和噪點消除等附加功能,IronOCR 確保您的車牌識別任務優化以達到最佳可能結果。

Kannaopat Udonpant
坎納帕特·烏頓潘
軟體工程師
在成為軟體工程師之前,Kannapat 在日本北海道大學完成了環境資源博士學位。在攻讀學位期間,Kannapat 也成為了車輛機器人實驗室的成員,該實驗室隸屬於生物生產工程學系。2022 年,他利用自己的 C# 技能,加入了 Iron Software 的工程團隊,專注於 IronPDF 的開發。Kannapat 珍視這份工作,因為他可以直接向負責撰寫大部分 IronPDF 程式碼的開發人員學習。除了同儕學習外,Kannapat 還享受在 Iron Software 工作的社交方面。當他不在撰寫程式碼或文件時,Kannapat 通常會在 PS5 上玩遊戲或重看《最後生還者》。
< 上一頁
如何在C#中進行駕照的OCR
下一個 >
如何從 C# 教程中獲取發票文本