C#で画像のOCR対象領域を指定する方法
C# で画像の特定領域からテキストを抽出するには、IronOCR の Rectangle オブジェクトに x/y 座標、width、height を指定します。作成したオブジェクトを LoadImage メソッドに渡すと、その領域だけに OCR を実行できます。
-
1IronOCR を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
using IronOcr; using IronSoftware.Drawing; // 1. Install IronOCR via NuGet: Install-Package IronOcr var ocr = new IronTesseract(); using var input = new OcrInput(); // 2. Create a Rectangle with coordinates var region = new Rectangle(x: 215, y: 1250, width: 1335, height: 280); // 3. Load image with region input.LoadImage("image.png", region); // 4. Extract text var result = ocr.Read(input); Console.WriteLine(result.Text);Imports IronOcr Imports IronSoftware.Drawing ' 1. Install IronOCR via NuGet: Install-Package IronOcr Dim ocr = New IronTesseract() Using input = New OcrInput() ' 2. Create a Rectangle with coordinates Dim region = New Rectangle(x:=215, y:=1250, width:=1335, height:=280) ' 3. Load image with region input.LoadImage("image.png", region) ' 4. Extract text Dim result = ocr.Read(input) Console.WriteLine(result.Text) End UsingImports IronOcr Imports IronSoftware.Drawing ' 1. Install IronOCR via NuGet: Install-Package IronOcr Dim ocr = New IronTesseract() Using input = New OcrInput() ' 2. Create a Rectangle with coordinates Dim region = New Rectangle(x:=215, y:=1250, width:=1335, height:=280) ' 3. Load image with region input.LoadImage("image.png", region) ' 4. Extract text Dim result = ocr.Read(input) Console.WriteLine(result.Text) End Using -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronOCRを使い始めるには、無料トライアルをお試しください。
多くの場合、請求書の合計金額やフォームの特定のフィールドなど、画像の小さな部分からテキストを抽出するだけで済みます。 ドキュメント全体をスキャンするのは非効率的であり、無関係なテキストをキャプチャすることでエラーが発生する可能性があります。
IronOCR を使用すると、スキャンする正確な長方形領域を指定することにより、精度、パフォーマンス、正確性を向上させることができます。 このガイドでは、特定の OCR 領域を定義し、そこからテキストを抽出し、OCR タスクに対して座標が正しいことを視覚的に確認する方法について、ステップバイステップで説明します。
IronOCRの利用を開始する
画像のOCR対象領域を指定する方法
- OCR対象領域を指定できるC#ライブラリをダウンロード
- OCRエンジンをインスタンス化します。
- 矩形でOCRの対象領域を指定します。
LoadImageに指定した矩形を渡して、画像を読み込みます。- OcrResultプロパティにアクセスして、抽出されたデータを表示および操作します。
特定の領域でOCRを実行するには?
IronSoftware.Drawing 名前空間の Rectangle オブジェクトを作成して、OCR の対象領域を指定します。x 座標、y 座標、width、height の4つの値をピクセル単位で指定します。(x、y)は対象領域の左上隅です。
画像を LoadImage で読み込む際、この Rectangle を第2パラメータとして渡します。 IronOCR は、OCR プロセスをその境界ボックス内のピクセルのみに制限します。
領域を指定したOCRは、請求書、スキャンしたフォーム、身分証明書など、特定の情報が決まった位置にある文書で特に有用です。必要な領域だけを処理すると、処理速度が大幅に向上し、無関係なテキストによる誤検出も減らせます。
Rectangle の座標は、MSペイントなどの画像編集ソフトで調べられます。入力画像を開き、対象領域の左上隅と右下隅にマウスポインターを合わせて、それぞれのピクセル座標(x、y)を控えます。矩形の値(x1、y1、width、height)のうち、幅と高さはそれぞれ width = x2 − x1、height = y2 − y1 で求めます。
テストにはどの画像を使用すれば良いですか?
3つの段落を含むサンプル画像を使用します。目標は、2番目の段落のみを抽出し、残りのテキストを無視することです。 これは、大きな文書から特定のフィールドやセクションを抽出する必要がある一般的なシナリオを示しています。

OCRの対象領域をコードで指定する方法
OcrInput オブジェクトを作成し、矩形領域を指定して画像を読み込みます。この方法は、JPG、PNG、GIF、TIFF、BMPなど、さまざまな画像形式に対応しています。
using IronOcr;
using IronSoftware.Drawing;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// Define the specific region as a Rectangle
// (x, y) is the top-left corner.
var ContentArea = new Rectangle(x: 215, y: 1250, width: 1335, height: 280);
ocrInput.LoadImage("region-input.png", ContentArea);
var ocrResult = ocrTesseract.Read(ocrInput);
// Print the extracted text
Console.WriteLine(ocrResult.Text);using IronOcr;
using IronSoftware.Drawing;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// Define the specific region as a Rectangle
// (x, y) is the top-left corner.
var ContentArea = new Rectangle(x: 215, y: 1250, width: 1335, height: 280);
ocrInput.LoadImage("region-input.png", ContentArea);
var ocrResult = ocrTesseract.Read(ocrInput);
// Print the extracted text
Console.WriteLine(ocrResult.Text);Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Dim ocrTesseract As New IronTesseract()
Using ocrInput As New OcrInput()
' Define the specific region as a Rectangle
' (x, y) is the top-left corner.
Dim ContentArea As New Rectangle(x:=215, y:=1250, width:=1335, height:=280)
ocrInput.LoadImage("region-input.png", ContentArea)
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Print the extracted text
Console.WriteLine(ocrResult.Text)
End UsingImports IronOcr
Imports IronSoftware.Drawing
Imports System
Dim ocrTesseract As New IronTesseract()
Using ocrInput As New OcrInput()
' Define the specific region as a Rectangle
' (x, y) is the top-left corner.
Dim ContentArea As New Rectangle(x:=215, y:=1250, width:=1335, height:=280)
ocrInput.LoadImage("region-input.png", ContentArea)
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Print the extracted text
Console.WriteLine(ocrResult.Text)
End Usingより複雑な処理では、同じ画像内に複数の領域を指定できます。複数のフィールドがあるフォームや文書内の表を処理する場合に便利です。
using IronOcr;
using IronSoftware.Drawing;
var ocr = new IronTesseract();
// Define multiple regions for different form fields
var nameField = new Rectangle(x: 100, y: 200, width: 300, height: 50);
var dateField = new Rectangle(x: 100, y: 300, width: 200, height: 50);
var amountField = new Rectangle(x: 400, y: 500, width: 150, height: 50);
// Load the same image with a separate OcrInput per region
OcrResult nameResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", nameField);
nameResult = ocr.Read(input);
}
OcrResult dateResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", dateField);
dateResult = ocr.Read(input);
}
OcrResult amountResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", amountField);
amountResult = ocr.Read(input);
}
// Process each field separately
Console.WriteLine($"Name: {nameResult.Text}");
Console.WriteLine($"Date: {dateResult.Text}");
Console.WriteLine($"Amount: {amountResult.Text}");using IronOcr;
using IronSoftware.Drawing;
var ocr = new IronTesseract();
// Define multiple regions for different form fields
var nameField = new Rectangle(x: 100, y: 200, width: 300, height: 50);
var dateField = new Rectangle(x: 100, y: 300, width: 200, height: 50);
var amountField = new Rectangle(x: 400, y: 500, width: 150, height: 50);
// Load the same image with a separate OcrInput per region
OcrResult nameResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", nameField);
nameResult = ocr.Read(input);
}
OcrResult dateResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", dateField);
dateResult = ocr.Read(input);
}
OcrResult amountResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", amountField);
amountResult = ocr.Read(input);
}
// Process each field separately
Console.WriteLine($"Name: {nameResult.Text}");
Console.WriteLine($"Date: {dateResult.Text}");
Console.WriteLine($"Amount: {amountResult.Text}");Imports IronOcr
Imports IronSoftware.Drawing
Dim ocr = New IronTesseract()
' Define multiple regions for different form fields
Dim nameField = New Rectangle(x:=100, y:=200, width:=300, height:=50)
Dim dateField = New Rectangle(x:=100, y:=300, width:=200, height:=50)
Dim amountField = New Rectangle(x:=400, y:=500, width:=150, height:=50)
' Load the same image with a separate OcrInput per region
Dim nameResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", nameField)
nameResult = ocr.Read(input)
End Using
Dim dateResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", dateField)
dateResult = ocr.Read(input)
End Using
Dim amountResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", amountField)
amountResult = ocr.Read(input)
End Using
' Process each field separately
Console.WriteLine($"Name: {nameResult.Text}")
Console.WriteLine($"Date: {dateResult.Text}")
Console.WriteLine($"Amount: {amountResult.Text}")Imports IronOcr
Imports IronSoftware.Drawing
Dim ocr = New IronTesseract()
' Define multiple regions for different form fields
Dim nameField = New Rectangle(x:=100, y:=200, width:=300, height:=50)
Dim dateField = New Rectangle(x:=100, y:=300, width:=200, height:=50)
Dim amountField = New Rectangle(x:=400, y:=500, width:=150, height:=50)
' Load the same image with a separate OcrInput per region
Dim nameResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", nameField)
nameResult = ocr.Read(input)
End Using
Dim dateResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", dateField)
dateResult = ocr.Read(input)
End Using
Dim amountResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", amountField)
amountResult = ocr.Read(input)
End Using
' Process each field separately
Console.WriteLine($"Name: {nameResult.Text}")
Console.WriteLine($"Date: {dateResult.Text}")
Console.WriteLine($"Amount: {amountResult.Text}")どのような結果が期待できますか?
コンソール出力から、OCRで処理されるのは2番目の段落だけだと分かります。対象領域を絞ることで、画像内の無関係なテキストが認識結果に混ざるのを防げます。

対象領域のOCR精度は、次の要因に左右されます。
- 画像品質: 高解像度画像は一般的により良い結果をもたらします。 DPI設定を利用して、画像を最適化することを検討してください。
- テキストの向き: テキストが適切に整列されていることを確認してください。 必要に応じてページ回転検出を使用してください。
- コントラストと鮮明さ: 画像補正フィルタを適用して、テキストを読み取りやすくします。
座標が正しいことを確認するにはどうすれば良いですか?
入力画像で指定した座標が正しいかどうかは、定義した ContentArea を可視化して確認できます。入力画像に矩形を描画し、StampCropRectangleAndSaveAs で別のファイルとして保存します。この方法で座標のずれを確認し、必要に応じて微調整できます。
この可視化技術は、特に複雑なレイアウトで作業する場合や、品質保証目的で特定のテキスト領域を強調する必要がある場合に役立ちます。
これは、上記の入力画像の例に指定された境界ボックスを描画した後の出力画像です。
選択した領域をどのように視覚化しますか?
using IronOcr;
using IronSoftware.Drawing;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// Define the specific rectangular area to scan within the image.
// The coordinates are in pixels: (x, y) is the top-left corner of the rectangle.
var ContentArea = new Rectangle(x: 4, y: 59, width: 365, height: 26);
ocrInput.LoadImage("region-input.png", ContentArea);
var ocrResult = ocrTesseract.Read(ocrInput);
// Draws the rectangle from above in a blue bounding box on the image for visualization.
ocrInput.StampCropRectangleAndSaveAs(ContentArea, Color.Aqua, "region-input.png");using IronOcr;
using IronSoftware.Drawing;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// Define the specific rectangular area to scan within the image.
// The coordinates are in pixels: (x, y) is the top-left corner of the rectangle.
var ContentArea = new Rectangle(x: 4, y: 59, width: 365, height: 26);
ocrInput.LoadImage("region-input.png", ContentArea);
var ocrResult = ocrTesseract.Read(ocrInput);
// Draws the rectangle from above in a blue bounding box on the image for visualization.
ocrInput.StampCropRectangleAndSaveAs(ContentArea, Color.Aqua, "region-input.png");Imports IronOcr
Imports IronSoftware.Drawing
Dim ocrTesseract = New IronTesseract()
Using ocrInput As New OcrInput()
' Define the specific rectangular area to scan within the image.
' The coordinates are in pixels: (x, y) is the top-left corner of the rectangle.
Dim ContentArea As New Rectangle(x:=4, y:=59, width:=365, height:=26)
ocrInput.LoadImage("region-input.png", ContentArea)
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Draws the rectangle from above in a blue bounding box on the image for visualization.
ocrInput.StampCropRectangleAndSaveAs(ContentArea, Color.Aqua, "region-input.png")
End UsingImports IronOcr
Imports IronSoftware.Drawing
Dim ocrTesseract = New IronTesseract()
Using ocrInput As New OcrInput()
' Define the specific rectangular area to scan within the image.
' The coordinates are in pixels: (x, y) is the top-left corner of the rectangle.
Dim ContentArea As New Rectangle(x:=4, y:=59, width:=365, height:=26)
ocrInput.LoadImage("region-input.png", ContentArea)
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Draws the rectangle from above in a blue bounding box on the image for visualization.
ocrInput.StampCropRectangleAndSaveAs(ContentArea, Color.Aqua, "region-input.png")
End Using対象領域を可視化した出力

水色の矩形から、2番目の段落だけが処理対象になっていることを確認できます。
OCRの対象領域を指定する場面
OCRの対象領域を指定すると、次のような場面で役立ちます。
- フォーム処理: データが一貫した場所に表示される標準化されたフォームから特定のフィールドを抽出する場合。
- 請求書処理: 文書全体を処理せずに合計、日付、請求書番号などの特定の値を抽出する場合。
- ナンバープレート: ナンバープレート認識で、プレート部分だけを読み取る場合。
- 身分証明書: パスポートやIDカードから特定のフィールドを抽出する場合。
- スクリーンショット: スクリーンショットで特定のUI要素からテキストをキャプチャする場合。
OCRの対象領域を指定する際のベストプラクティス
対象領域のOCRで良好な結果を得るには、以下の点に注意してください。
- 余白を追加: 領域の端で文字が切れないように、テキストの周囲に少し余白を設けます。
- サンプル画像でテスト: 大量の画像を処理する前に、必ず代表的なサンプルで座標を確認します。
- 位置のずれに対応: スキャンした文書で生じるわずかな位置のずれを見込んで、領域を少し大きめに設定します。
- パフォーマンスの最適化: マルチスレッド処理の場合、異なる領域を並列で処理します。
- 信頼度を確認: 認識精度を確かめるために、結果の信頼度スコアを確認します。
OCRの対象領域を絞ると、テキスト抽出の速度と精度を大幅に向上できます。.NET アプリケーションで効率的な文書処理ワークフローを構築するうえで重要な手法です。
よくある質問
C#を使用して、画像内の特定のOCR領域をどのように定義できますか?
特定のOCR領域をC#で定義するには、x/y座標、幅、高さを指定してIronOCRのRectangleオブジェクトを使用します。次に、この矩形をLoadImageメソッドに渡してターゲットOCR処理を行います。
特定のOCR領域を定義することの利点は何ですか?
特定のOCR領域を指定することで、画像の関連部分に集中し、不要なテキスト抽出を防ぎ、精度とパフォーマンスを向上させます。
C#で特定の画像領域でOCRを実行するには、どのライブラリが必要ですか?
IronOCRとIronSoftware.Drawingライブラリが必要です。IronOCRはNuGet経由でインストールされ、定義済みの画像領域でOCRを実行できます。
C#アプリケーションでOCRの矩形座標をどのように指定しますか?
矩形座標を指定するには、画像編集ソフトで左上隅のx/y座標を決定し、ピクセル単位での目的の領域の幅と高さを設定します。
IronOCRを使用して画像内の複数の領域からテキストを抽出できますか?
はい、IronOCRを使用すると、同じ画像内で複数の矩形領域を定義して処理できるため、複数のフィールドを持つフォームの処理に最適です。
入力画像にOCR領域を視覚化することは可能ですか?
はい、指定した矩形を入力画像上に描画して座標の確認を支援するためにStampCropRectangleAndSaveAsメソッドを使用してOCR領域を視覚化できます。
地域的なOCRはどのような用途に特に有用ですか?
地域的なOCRは、フォーム処理、請求書のスキャン、ナンバープレート認識、身分証明書の分析、スクリーンショット内のUI要素からのテキスト抽出に有用です。
文書処理で地域的なOCRを使用するためのベストプラクティスは何ですか?
ベストプラクティスには、テキスト周りにパディングを追加すること、サンプル画像でのテスト、わずかな文書のバリエーションに対応すること、マルチスレッドでのパフォーマンス最適化、結果の信頼度スコアの監視が含まれます。
IronOCRの地域的スキャンの精度をどのように向上できますか?
高解像度の画像を使用し、適切なテキストの向きを確認し、コントラストと明瞭度フィルターを適用し、対象の領域を視覚的に確認することで精度を向上させます。
IronOCRは異なる画像形式で使用できますか?
はい、IronOCRはJPG、PNG、GIF、TIFF、BMPなどのさまざまな画像形式をサポートしており、さまざまなスキャンや処理ニーズに対応できます。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。