IronOCRを使用してC#でページの回転を検出する方法
IronOCRのDetectPageOrientationメソッドは、PDFドキュメントと画像内のページ回転角度(0°、90°、180°、270°)を自動的に識別します。 各ページのRotationAngleプロパティを返し、プログラムによる正確なテキスト抽出を可能にする信頼スコア付きの向き補正を行います。
ページ回転検出は、文書ページが時計回りか反時計回りに0度、90度、180度、270度回転したかを識別します。 この情報は、正確なレンダリングとテキスト抽出のために、ページが正しい向きで表示または処理されることを保証します。
クイックスタート: ページの回転を識別するためにDetectPageOrientationを使用
この例では、IronOCR の DetectPageOrientation を PDF に使用して RotationAngle プロパティにアクセスする方法を示しています。 最小限のコードで、高速なページ回転の検出と修正を提供します。
-
1IronOCR を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
var rotationResults = new IronOcr.OcrInput().LoadPdf("doc.pdf").DetectPageOrientation(); Console.WriteLine(rotationResults.First().RotationAngle);Dim rotationResults = New IronOcr.OcrInput().LoadPdf("doc.pdf").DetectPageOrientation() Console.WriteLine(rotationResults.First().RotationAngle)Dim rotationResults = New IronOcr.OcrInput().LoadPdf("doc.pdf").DetectPageOrientation() Console.WriteLine(rotationResults.First().RotationAngle) -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronOCRを使い始めるには、無料トライアルをお試しください。
最小限のワークフロー(5ステップ)
- ページ回転を検出するためのC#ライブラリをダウンロードする
- 読み取り用にPDFドキュメントと画像をインポートする
DetectPageOrientationメソッドを使用して、すべてのページの回転を検出します。- ページの回転を修正するには、 RotationAngleプロパティにアクセスします。
- エッジケースを処理するためにHighConfidenceプロパティにアクセスする
自分の文書でページの回転を検出する方法は?
ドキュメントを読み込んだ後、DetectPageOrientationメソッドを使用して各ページの回転を識別します。 このメソッドは、0度、90度、180度、270度に対応しています。 これらの標準回転を超えるスキュー画像には、IronOCR の画像補正フィルターからDeskew メソッドを使用してください。 次に、検出された角度を使用して画像を元の向きに回転させます。 サンプルPDFを使用して作業を始めましょう。
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);
}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 resultImports 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検出結果は何を意味しますか?
PageNumber: ページのゼロベースのインデックス。RotationAngle: 度単位の回転角度。Rotateメソッドと組み合わせて、向きを修正します。HighConfidence: 境界事例を扱うための方向結果の信頼レベル。
高い信頼度の値はいつ使用すべきですか?
HighConfidenceプロパティは、回転検出が不確かな不明瞭または低品質のドキュメントで重要です。 テキストがまばらであったり、レイアウトが特殊であったり、スキャンの質が悪かったりする文書は、信頼度のスコアが低くなることがよくあります。 このような場合、検出前に追加の検証を行うか、画像品質補正フィルターを適用します。
この値を使用して、信頼性の低いページのフォールバック戦略または手動レビューを実装します。 たとえば、信頼度が80%を下回った場合は、ページを複数の方向で処理してOCR結果を比較するか、手動レビューのフラグを立てます。 IronOCRのコンピュータビジョン機能は、困難なドキュメントでのテキスト領域をより正確に識別するのに役立ちます。
検出された回転をどのように修正しますか?
回転角度を特定した後、OCR の前に方位を修正するために Rotate メソッドを OcrInput オブジェクトで使用します。 これにより、最適なテキスト認識精度が保証されます。 包括的な向きの修正については、画像向き修正ガイドを参照してください。 修正プロセスは以下の通りです:
// 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' Apply rotation correction based on detection results
If result.RotationAngle <> 0 Then
input.Rotate(360 - result.RotationAngle) ' Rotate back to 0°
End If追加の前処理が必要なドキュメントには、OCR処理前に広範囲なドキュメント準備メソッドを提供するOcrInputクラスを考慮してください。
検出の速度と精度をどのようにカスタマイズできますか?
DetectPageOrientationメソッドは、検出の詳細とリソース使用を制御するためのオプション引数を受け入れます。 OrientationDetectionMode 列挙型を提供することで、要件に応じて検出速度と精度を調整できます。2 番目の maxDegreeOfParallelism 引数は同時に分析されるページ数を制限します。
実装方法は以下の通りです:
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);
}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 UsingImports 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どの検出モードを選ぶべきですか?
OrientationDetectionModeのために4つの速度オプションが利用可能です。
IronOcr.Extensions.AdvancedScanパッケージが必要です。 これらのオプションはWindows x86およびMac ARMでは利用できません。- 高速: 高速検出で精度は低下します。 スピードが重要なドラフトや一括処理に最適です。
DetectPageOrientationのデフォルトです。 マルチスレッドサポートにより、数千ページを効率的に処理し、各スレッドに1つのエンジンを再利用することで、長いドキュメントでのメモリ使用を低く抑えます。 - バランス: 速度と精度のバランス。 本番作業に適しています。 AdvancedScan拡張機能を使用して、パフォーマンスを維持しながら精度を向上させます。
- 詳細: 速度は低いが精度は高い。 特に、複雑なレイアウトや内容が混在する文書など、正確で重要な作業に最適です。
- 極端な詳細: 最も遅いが精度は最高。 詳細が不十分な場合や、テキストが大きく歪んでいる場合にのみ使用してください。
向き検出時のメモリ制約の方法は?
複数ページ入力の場合、方位検出は複数のページを一度に分析し、各同時ページは独自のネイティブエンジンを使用します。最大同時実行数を制限し、メモリピークを抑えるためには、maxDegreeOfParallelism 値を渡します。これにより、メモリ制約のあるサーバーやコンテナ、およびドキュメントを並列に読み込む際に利用可能な MaxDegreeOfParallelism 制御と一致します。
using IronOcr;
using System.Collections.Generic;
using var input = new OcrInput();
input.LoadPdf("scanned.pdf");
// Cap orientation detection to 2 pages at a time. The default is one per CPU core.
IEnumerable<OcrPageOrientationResult> orientation =
input.DetectPageOrientation(OrientationDetectionMode.Fast, maxDegreeOfParallelism: 2);using IronOcr;
using System.Collections.Generic;
using var input = new OcrInput();
input.LoadPdf("scanned.pdf");
// Cap orientation detection to 2 pages at a time. The default is one per CPU core.
IEnumerable<OcrPageOrientationResult> orientation =
input.DetectPageOrientation(OrientationDetectionMode.Fast, maxDegreeOfParallelism: 2);Imports IronOcr
Imports System.Collections.Generic
Dim input As New OcrInput()
input.LoadPdf("scanned.pdf")
' Cap orientation detection to 2 pages at a time. The default is one per CPU core.
Dim orientation As IEnumerable(Of OcrPageOrientationResult) =
input.DetectPageOrientation(OrientationDetectionMode.Fast, maxDegreeOfParallelism:=2)Imports IronOcr
Imports System.Collections.Generic
Dim input As New OcrInput()
input.LoadPdf("scanned.pdf")
' Cap orientation detection to 2 pages at a time. The default is one per CPU core.
Dim orientation As IEnumerable(Of OcrPageOrientationResult) =
input.DetectPageOrientation(OrientationDetectionMode.Fast, maxDegreeOfParallelism:=2)Environment.ProcessorCountにフォールバックします。 単一引数のDetectPageOrientationオーバーロードは変更されておらず、完全にサポートされ続けるため、既存のコードは変更なしで動作し続けます。一般的なパフォーマンスの考慮事項は何ですか?
パフォーマンスはモードによって大きく異なります。 高速モードは高スループット処理に最適化されており、極端な詳細は速度を犠牲にして精度を優先します。 正確さの要件と時間的制約に基づいて選択してください。 最適なパフォーマンスのために
- 画像解像度: 高いDPI設定は精度を向上させますが、処理時間が増加します。回転検出には通常150~300 DPIで十分です。
- ドキュメントタイプ: テキストが密集しているドキュメントは、まばらなレイアウトよりも迅速かつ正確に処理されます。 フィルタウィザードを使用して、検出前に画像の品質を最適化してください。
- リソース使用: 大量バッチを処理する際のメモリ使用を監視し、
maxDegreeOfParallelism値を渡して一度に分析されるページ数を制限します。 進行追跡を実装して、フィードバックを提供し、システムリソースを管理します。 - 並列処理: 大量操作には、IronOCRのマルチスレッド機能を使用して、精度を維持しながら複数のドキュメントを同時に処理します。 メモリが制約されている場合は、
MaxDegreeOfParallelismで制限してください。
混在した向きの文書をどのように処理しますか?
混合向きのドキュメントを処理する場合、各ページを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)
{
// Load the specific page as its own input and correct it
using var pageInput = new OcrInput();
pageInput.LoadPdfPage("doc.pdf", i);
pageInput.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)
{
// Load the specific page as its own input and correct it
using var pageInput = new OcrInput();
pageInput.LoadPdfPage("doc.pdf", i);
pageInput.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
' Load the specific page as its own input and correct it
Using pageInput = New OcrInput()
pageInput.LoadPdfPage("doc.pdf", i)
pageInput.Rotate(360 - pageResult.RotationAngle)
End Using
End If
Next i' 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
' Load the specific page as its own input and correct it
Using pageInput = New OcrInput()
pageInput.LoadPdfPage("doc.pdf", i)
pageInput.Rotate(360 - pageResult.RotationAngle)
End Using
End If
Next iスキャンされたドキュメントの品質の変化やマルチページTIFFを含む複雑なシナリオの場合、最良の結果を得るために各ページを個別に前処理します。
混合フォーマット入力を処理する際、OcrResultクラスは詳細なページ情報を提供し、精密なエラーハンドリングと品質管理ワークフローを可能にします。 高スループットの生産環境には、速度と精度のバランスをとるために高速OCR設定オプションを検討してください。
テキストとバーコードを含むドキュメントを処理する場合、IronOCRのOCRとバーコード&QR読み取り機能を使用して、効率を向上させながらすべての情報を一度に抽出します。
よくある質問
IronOCRはページの回転をどのように検出しますか?
IronOCRではDetectPageOrientationメソッドを使用して、PDFや画像のページの回転角度を自動的に特定し、0°、90°、180°、270°の方向を検出して正確なテキスト抽出を行います。
IronOCRにおけるRotationAngleプロパティの目的は何ですか?
IronOCRのRotationAngleプロパティは、各ページに対して検出された回転の度合いを示すために使用されます。これにより、開発者はプログラム的にページの向きを正確に修正し、正確なテキストのレンダリングと抽出が可能になります。
IronOCRは標準以外のページの回転に対応できますか?
はい、標準の0°、90°、180°、270°の回転を超える歪んだ画像に対して、IronOCRはその画像補正フィルタからDeskewメソッドを使用することをお勧めし、画像を正しく向けることが可能です。
IronOCRにおけるHighConfidenceプロパティとは何ですか?
IronOCRのHighConfidenceプロパティは、検出された向きの信頼レベルを反映します。曖昧または低品質のスキャンでは、このプロパティは特に便利です。より多くの検証が必要な場合があります。
IronOCRは検出の速度と精度をどのように向上させますか?
IronOCRは、DetectPageOrientation用のオプションのOrientationDetectionModeパラメータを提供しており、ユーザーのニーズに基づいて高速から極度詳細モードまで検出速度と精度を調整することができます。
この機能は、特定のタイプのドキュメントに最適ですか?
IronOCRのDetectPageOrientation機能は、テキストが密集した文書に最適です。テキストが少ない文書や複雑なレイアウトの文書では、最適な結果を得るために検出の前に画質補正フィルタを適用することを検討してください。
大きなPDFの向きを検出する際にメモリの使用を制限するにはどうすればよいですか?
DetectPageOrientationのオーバーロードにmaxDegreeOfParallelism値を渡します。例えば、input.DetectPageOrientation(OrientationDetectionMode.Fast, maxDegreeOfParallelism: 2)です。これにより、同時に分析されるページの数が制限されます。各並行ページがそれぞれのネイティブエンジンを使用するので、ピークメモリが直接制限されます。非正値の場合はEnvironment.ProcessorCountに戻ります。
maxDegreeOfParallelismパラメータを追加すると既存のコードが壊れるか?
いいえ。単一の引数を持つDetectPageOrientationのオーバーロードは変更されておらず、引き続き完全にサポートされているため、既存のコードは変更なしで動作し続けます。高速モードでは、1つのエンジンをワーカースレッドごとに再利用するため、メモリ効率も向上します。結果は変更されません。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。