如何在C#中檢測OCR的頁面旋轉 | IronOCR

如何使用IronOCR在C#中檢測頁面旋轉

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

IronOCR的DetectPageOrientation方法會自動識別PDF文件和圖像中的頁面旋轉角度(0°、90°、180°、270°)。 它為每頁返回RotationAngle屬性,使程式能夠打擊方向矯正,並提供信心分數以準確提取文字。

頁面旋轉偵測識別文件頁面是順時針還是逆時針旋轉了0、90、180或270度。 此資訊確保頁面以正確的方向顯示或處理,以便準確呈現和提取文字。

DetectPageOrientation識別頁面旋轉@@--AH2EG--@@

此範例演示了在PDF上使用IronOCR的RotationAngle屬性。 它提供快速的頁面旋轉偵測和矯正,所需程式碼極少。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronOcr

    PM > Install-Package IronOcr
  2. 複製並運行這段程式碼片段。

    var rotationResults = new IronOcr.OcrInput().LoadPdf("doc.pdf").DetectPageOrientation();
    Console.WriteLine(rotationResults.First().RotationAngle);
  3. 部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronOCR,透過免費試用

    arrow pointer


如何在我的文件中檢測頁面旋轉?

載入文件後,使用DetectPageOrientation方法來識別每頁的旋轉。 此方法支持0、90、180和270度。 對於超出這些標準旋轉的偏斜圖像,請使用IronOCR的圖像矯正過濾器中的Deskew方法。 然後使用偵測到的角度將圖像旋轉回其原來的方向。 讓我們處理一個範例PDF

請注意此功能在文字密集的文件中效果最佳。

:path=/static-assets/ocr/content-code-examples/how-to/detect-page-rotation-detect-page-rotation.cs
using IronOcr;
using System;

using var input = new OcrInput();

// Load PDF document
input.LoadPdf("Clockwise90.pdf");

// Detect page rotation
var results = input.DetectPageOrientation();

// Ouput result
foreach(var result in results)
{
    Console.WriteLine(result.PageNumber);
    Console.WriteLine(result.HighConfidence);
    Console.WriteLine(result.RotationAngle);
}
Imports IronOcr
Imports System

Private input = New OcrInput()

' Load PDF document
input.LoadPdf("Clockwise90.pdf")

' Detect page rotation
Dim results = input.DetectPageOrientation()

' Ouput result
For Each result In results
	Console.WriteLine(result.PageNumber)
	Console.WriteLine(result.HighConfidence)
	Console.WriteLine(result.RotationAngle)
Next result
$vbLabelText   $csharpLabel

偵測結果的意義是什麼?

  • PageNumber: 頁面的零基索引。
  • RotationAngle: 旋轉角度(度數)。 使用Rotate方法進行方向矯正。
  • HighConfidence: 用於處理邊緣情況的方向結果的信心水平。

什麼時候應該使用高信心值?

HighConfidence屬性對於旋轉偵測不確定的模糊或低質量文件尤為重要。 包含稀疏文字、不尋常的佈局或掃描質量差的文件通常會返回較低的信心水平。 在這些情況下,實施額外的驗證或在偵測之前應用圖像質量矯正過濾器

使用此值為低信心頁面實施備用策略或人工審查。 例如,如果信心低於80%,則以多個方向處理該頁面並比較OCR結果,或標記進行人工審查。 IronOCR的計算機視覺功能有助於在挑戰性文件中更準確地識別文字區域。

如何矯正偵測到的旋轉?

識別旋轉角度後,請在您的Rotate方法,以便在OCR之前矯正方向。 這可確保最佳的文字識別準確性。 有關全面的方向修正,請參閱圖像方向矯正指南。 以下是糾正過程:

// Apply rotation correction based on detection results
if (result.RotationAngle != 0)
{
    input.Rotate(360 - result.RotationAngle); // Rotate back to 0°
}
// Apply rotation correction based on detection results
if (result.RotationAngle != 0)
{
    input.Rotate(360 - result.RotationAngle); // Rotate back to 0°
}
' Apply rotation correction based on detection results
If result.RotationAngle <> 0 Then
    input.Rotate(360 - result.RotationAngle) ' Rotate back to 0°
End If
$vbLabelText   $csharpLabel

對於需要額外預處理的文件,請考慮OcrInput類,其在OCR處理之前提供廣泛的文件準備方法。

我如何自訂偵測速度和準確性?

DetectPageOrientation方法接受可選參數來控制偵測細節。 通過提供OrientationDetectionMode枚舉,您可以根據需求調整偵測速度和準確性。

以下是如何實施:

:path=/static-assets/ocr/content-code-examples/how-to/detect-page-rotation-detect-page-rotation-advanced.cs
using IronOcr;
using System;

using var input = new OcrInput();

// Load PDF document
input.LoadPdf("Clockwise90.pdf");

// Detect page rotation with Fast mode
var results = input.DetectPageOrientation(OrientationDetectionMode.Fast);

// Ouput result
foreach(var result in results)
{
    Console.WriteLine(result.PageNumber);
    Console.WriteLine(result.HighConfidence);
    Console.WriteLine(result.RotationAngle);
}
Imports IronOcr
Imports System

Using input As New OcrInput()
    ' Load PDF document
    input.LoadPdf("Clockwise90.pdf")

    ' Detect page rotation with Fast mode
    Dim results = input.DetectPageOrientation(OrientationDetectionMode.Fast)

    ' Output result
    For Each result In results
        Console.WriteLine(result.PageNumber)
        Console.WriteLine(result.HighConfidence)
        Console.WriteLine(result.RotationAngle)
    Next
End Using
$vbLabelText   $csharpLabel

我應該選擇哪種偵測模式?

四種速度選項可供OrientationDetectionMode選用:

警告平衡詳細極其詳盡需要IronOcr.Extensions.AdvancedScan包。 這些選項在Windows x86和Mac ARM上不可用。
。 )}]

  • 快速: 高速偵測但準確性較低。 適用於行稿或批量處理,那裡速度至關重要。 預設為DetectPageOrientation。 使用多執行緒支持來有效處理數千頁。
  • 平衡: 平衡的速度和準確性。 適合生產任務。 使用AdvancedScan擴展功能來提高準確性,同時保持性能。
  • 詳細: 低速,高準確性。 最適合同樣準確或關鍵的任務,尤其是具有複雜佈局或混合內容的文件。
  • 極其詳盡: 速度最慢,但準確性最高。 僅當詳細不夠或文字高度傾斜和失真時使用。

常見的性能考量是什麼?

性能在不同模式之間有很大差異。 快速模式每分鐘處理數百頁; 極其詳盡的模式每頁可能需要秒數。 根據準確性要求和時間限制選擇。 為了獲得最佳性能:

  1. 圖像解析度: 更高的DPI設置提高了準確性,但會增加處理時間。150-300 DPI通常足以轉動偵測。
  2. 文件型別: 文字密集的文件比稀疏的佈局處理得更快更準確。 在偵測之前使用過濾嚮導優化圖像質量。
  3. 資源使用: 處理大型批次時監控記憶體使用。 實施進度跟蹤以提供反饋並管理系統資源。
  4. 並行處理: 對於批量操作,使用IronOCR的多執行緒處理,同時保持準確性。

如何處理混合方向文件?

對於混合方向的文件,逐頁使用DetectPageOrientation進行處理,然後在OCR之前應用逐頁旋轉修正。 這可確保正確的方向不管初始狀態如何。 這是一個有效的方法:

// Process each page with individual rotation detection
for (int i = 0; i < results.Count; i++)
{
    var pageResult = results[i];

    // Apply rotation only to pages that need it
    if (pageResult.RotationAngle != 0 && pageResult.HighConfidence)
    {
        // Correct the specific page
        input.Pages[i].Rotate(360 - pageResult.RotationAngle);
    }
}
// Process each page with individual rotation detection
for (int i = 0; i < results.Count; i++)
{
    var pageResult = results[i];

    // Apply rotation only to pages that need it
    if (pageResult.RotationAngle != 0 && pageResult.HighConfidence)
    {
        // Correct the specific page
        input.Pages[i].Rotate(360 - pageResult.RotationAngle);
    }
}
' Process each page with individual rotation detection
For i As Integer = 0 To results.Count - 1
    Dim pageResult = results(i)

    ' Apply rotation only to pages that need it
    If pageResult.RotationAngle <> 0 AndAlso pageResult.HighConfidence Then
        ' Correct the specific page
        input.Pages(i).Rotate(360 - pageResult.RotationAngle)
    End If
Next
$vbLabelText   $csharpLabel

對於涉及不同質量的掃描文件多頁TIFF的復雜場景,逐頁預處理以獲得最佳效果。

處理混合格式輸入時,OcrResult類提供詳細的頁面資訊,從而實現複雜的錯誤處理和質量控制工作流。 對於高吞吐量的生產環境,探索快速OCR配置選項以平衡速度和準確性。

如果處理含有文字和條形碼的文件,使用IronOCR的OCR與條形碼和QR閱讀功能在一次通行中提取所有資訊,提高效率。

常見問題

什麼是頁面旋轉檢測,為什麼它很重要?

頁面旋轉檢測識別文件頁面是否已旋轉0°、90°、180°或270°度。這一點對IronOCR至關重要,可以確保頁面以正確的方向處理,從而能從PDF和圖像中準確地提取和渲染文字。

如何使用C#快速檢測PDF中的頁面旋轉?

使用IronOCR的DetectPageOrientation方法,僅需最少的程式碼:var rotationResults = new IronOcr.OcrInput().LoadPdf("doc.pdf").DetectPageOrientation(); 這將返回所有頁面的旋轉資訊,透過RotationAngle屬性即可存取。

可以檢測哪些旋轉角度?

IronOCR的DetectPageOrientation方法可以檢測標準的0°、90°、180°和270°度旋轉。對於超出這些標準旋轉的歪斜圖像,請使用IronOCR的去歪斜方法來進行校正。

DetectPageOrientation返回什麼資訊?

此方法為每頁返回三個主要屬性:PageNumber(零基索引)、RotationAngle(用於搭配IronOCR的Rotate方法的旋轉角度)和HighConfidence(處理邊界案例的信心水平)。

什麼時候應該使用HighConfidence屬性?

當處理模糊或低質量的文件時使用HighConfidence屬性,因為這些文件的旋轉檢測結果可能不確定。文字稀疏、佈局不尋常或掃描質量差的文件經常在IronOCR中返回較低的信心分數,這需要進一步驗證或使用圖像質量校正過濾器。

該功能適合於某些型別的文件嗎?

IronOCR的DetectPageOrientation功能在文字密集的文件中表現最佳。對於文字少或佈局複雜的文件,建議在檢測前應用圖像質量校正過濾器,以獲得最佳結果。

IronOCR能整合到現有的應用程式中嗎?

IronOCR被設計成可以輕鬆地整合到現有應用程式中,使用C#允許開發人員以最小的努力為其軟體新增OCR功能。

使用IronOCR進行文件管理的好處是什麼?

使用IronOCR進行文件管理通過將掃描的文件轉換為可搜索和可編輯的文字來簡化工作流程,減少手動資料輸入的需求並提高文件的可存取性。

IronOCR如何提高資料精確性?

IronOCR通過其先進的識別算法和影像校正功能提高資料精確性,確保文字提取過程既可靠又精確。

IronOCR有免費試用版嗎?

有的,Iron Software提供IronOCR的免費試用版,允許使用者在做出購買決定前測試其功能和能力。

Curtis Chau
技術作家

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

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

由...審核
Jeff Fritz
Jeffrey T. Fritz
首席計劃經理 - .NET社區團隊
Jeff還是.NET和Visual Studio團隊的首席計劃經理。他是.NET Conf虛擬會議系列的執行製作人,並主持每週兩次的開發者直播節目'Fritz and Friends',在節目中討論技術並與觀眾一起撰寫程式碼。Jeff撰寫工作坊、演講和內容計劃,為微軟開發者的最大活動如Microsoft Build、Microsoft Ignite、.NET Conf和Microsoft MVP Summit提供內容支援。
準備開始了嗎?
Nuget 下載 6,136,090 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動?

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