C#ガイド: IronOCRの画像フィルターを使用してOCRの精度を向上させる
IronOCR は、フィルターの形で前処理が必要な可能性のある画像を読み取るために必要なツールを提供します。 画像を処理可能な状態にするための、さまざまなフィルターから選択できます。
クイックスタート: OCR画像をクリーンアップするためにフィルターを適用たった1つの簡単な呼び出しチェーンで、OCRの前にスキャンの明瞭度を向上させるためにDeNoise、Binarize、Deskewフィルターを適用できます。 この例は、IronOCRの組み込みフィルターを使用して画像を簡単に強化し、すぐに始める方法を示しています。
-
1IronOCR を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
using var input = new IronOcr.OcrInput(); input.LoadImage("scan.jpg"); input.DeNoise(true).Binarize().Deskew(45); var result = new IronOcr.IronTesseract().Read(input);Using input As New IronOcr.OcrInput() input.LoadImage("scan.jpg") input.DeNoise(True).Binarize().Deskew(45) Dim result = New IronOcr.IronTesseract().Read(input) End UsingUsing input As New IronOcr.OcrInput() input.LoadImage("scan.jpg") input.DeNoise(True).Binarize().Deskew(45) Dim result = New IronOcr.IronTesseract().Read(input) End Using -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronOCRを使い始めるには、無料トライアルをお試しください。
OCR画像フィルターのリスト
次の画像フィルターを使用すると、パフォーマンスを大幅に向上できます。
Filters to change the Image OrientationRotate- 画像を時計回りに特定の角度だけ回転させます。反時計回りの場合は負の数を使用します。Deskew- 画像を正しい上下方向に直交して回転させます。 Tesseract の斜めスキャンの許容度は 5 度まで低いため、これは OCR に非常に役立ちます。Scale- OCR入力ページを比例的に拡大縮小します。
Filters to manipulate Image ColorsBinarize- このイメージフィルターは、すべてのピクセルを白黒に変え、中間の色をなくします。 これにより、テキストと背景のコントラストが非常に低い場合に OCR のパフォーマンスが向上する可能性があります。ToGrayScale- このイメージフィルターは、すべてのピクセルを灰色の階調に変えます。 OCR の精度が向上する可能性は低いですが、速度は向上する可能性があります。Invert- すべての色を反転させます。 たとえば、白は黒になり、黒は白になります。ReplaceColor- 画像内の色を特定のしきい値内で他の色に置き換えます。
Filters to improve Contrast in an ImageContrast- コントラストを自動で上げます。 このフィルターにより、低コントラストのスキャンでの OCR 速度と精度が向上することがよくあります。Dilate- 高度なモルフォロジー。 膨張 は、画像内のオブジェクトの境界にピクセルを追加します。 Erode の反対。Erode- 高度なモルフォロジー。 Erosion はオブジェクトの境界からピクセルを削除します。 Dilate の反対。
Filters to reduce Image NoiseSharpen- ぼやけたOCRドキュメントを鮮明にし、アルファチャンネルを白に平坦化します。DeNoise- デジタルノイズを除去します。このフィルターはノイズが予想されるシナリオでのみ使用するべきです。EnhanceResolution- 低品質の画像の解像度を向上させます。 このフィルターはあまり必要がありません。OcrInput.TargetDPIが自動的に低解像度の入力をキャッチして解決します。
フィルターの例と使用方法
次の例では、コード内でフィルターを適用する方法を示します。
using IronOcr;
using System;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("my_image.png");
input.Deskew();
var result = ocr.Read(input);
Console.WriteLine(result.Text);using IronOcr;
using System;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("my_image.png");
input.Deskew();
var result = ocr.Read(input);
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("my_image.png")
input.Deskew()
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)Imports IronOcr
Imports System
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("my_image.png")
input.Deskew()
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)デバッグ フィルター / フィルターは何をしているのですか?
プログラム内で画像やバーコードを読み取るのが難しい場合は、フィルタリングされた結果の画像を保存する方法があります。 この方法により、各フィルターが何を実行し、どのように画像を操作しているかを正確にデバッグして確認できます。
using IronOcr;
using System;
var file = "skewed_image.tiff";
var ocr = new IronTesseract();
using var input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(file, pageindices);
// Here we apply the filter: Deskew
input.Deskew();
// Save the input with filter(s) applied
input.SaveAsImages("my_deskewed");
// We read, then print the text to the console
var result = ocr.Read(input);
Console.WriteLine(result.Text);using IronOcr;
using System;
var file = "skewed_image.tiff";
var ocr = new IronTesseract();
using var input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(file, pageindices);
// Here we apply the filter: Deskew
input.Deskew();
// Save the input with filter(s) applied
input.SaveAsImages("my_deskewed");
// We read, then print the text to the console
var result = ocr.Read(input);
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private file = "skewed_image.tiff"
Private ocr = New IronTesseract()
Private input = New OcrInput()
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames(file, pageindices)
' Here we apply the filter: Deskew
input.Deskew()
' Save the input with filter(s) applied
input.SaveAsImages("my_deskewed")
' We read, then print the text to the console
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)Imports IronOcr
Imports System
Private file = "skewed_image.tiff"
Private ocr = New IronTesseract()
Private input = New OcrInput()
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames(file, pageindices)
' Here we apply the filter: Deskew
input.Deskew()
' Save the input with filter(s) applied
input.SaveAsImages("my_deskewed")
' We read, then print the text to the console
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)フィルターの使用例
回転
フィルターの説明
回転は、画像に既知の回転を手動で設定して、画像を真っ直ぐに近づけるために使用されるフィルターです。 IronOCRには Deskew() を実行する機能がありますが、このための許容範囲はかなり狭く、ほぼ完璧にまっすぐな画像(約15度以内)の場合に最適です。 入力画像が90度ずれている場合や逆さまの場合は、Rotate() を呼び出すべきです。
ユースケースコード例
これは逆さまの画像を修正するための Rotate() 呼び出しの例です。
using IronOcr;
using System;
var image = "screenshot.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Rotate 180 degrees because image is upside-down
input.Rotate(180);
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);using IronOcr;
using System;
var image = "screenshot.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Rotate 180 degrees because image is upside-down
input.Rotate(180);
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private image = "screenshot.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Rotate 180 degrees because image is upside-down
input.Rotate(180)
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)Imports IronOcr
Imports System
Private image = "screenshot.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Rotate 180 degrees because image is upside-down
input.Rotate(180)
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)Before Input.Rotate(180) | After Input.Rotate(180) |
|---|---|
![]() | ![]() |
デスキュー
フィルターの説明
ハフ変換を使用して、一定の許容範囲内で画像をまっすぐにしようとします。 傾いた文書は読み間違える可能性があるため、画像が完全にまっすぐでない場合はこれが重要です。
ユースケースコード例
これは歪んだ画像を修正するための Deskew() 呼び出しの例です。
using IronOcr;
using System;
var image = @"paragraph_skewed.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply deskew with 15 degree snap
bool didDeskew = input.Deskew(15);
if (didDeskew)
{
// Read image into variable: result
var result = ocr.Read(input);
Console.WriteLine(result.Text);
}
else
{
Console.WriteLine("Deskew not applied because Image Orientation could not be determined.");
}using IronOcr;
using System;
var image = @"paragraph_skewed.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply deskew with 15 degree snap
bool didDeskew = input.Deskew(15);
if (didDeskew)
{
// Read image into variable: result
var result = ocr.Read(input);
Console.WriteLine(result.Text);
}
else
{
Console.WriteLine("Deskew not applied because Image Orientation could not be determined.");
}Imports IronOcr
Imports System
Private image = "paragraph_skewed.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply deskew with 15 degree snap
Dim didDeskew As Boolean = input.Deskew(15)
If didDeskew Then
' Read image into variable: result
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)
Else
Console.WriteLine("Deskew not applied because Image Orientation could not be determined.")
End IfImports IronOcr
Imports System
Private image = "paragraph_skewed.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply deskew with 15 degree snap
Dim didDeskew As Boolean = input.Deskew(15)
If didDeskew Then
' Read image into variable: result
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)
Else
Console.WriteLine("Deskew not applied because Image Orientation could not be determined.")
End IfScale
フィルターの説明
スケールは、既存のピクセルを使用して画像のサイズを変更するのに役立つ便利な画像操作フィルターです。 これは、画像の幅が数十ピクセルしかなく、各バーが 1 ピクセルであるためバーコードがスキャンされない場合、またはテキストが小さすぎてアンチエイリアシングがない場合に使用できます。
1000px x 1000pxのスイートスポットがあり、それによりバーコードがうまく読み取れるようになります。バーコードが見つからない場合にはこれを考慮する必要があります。ユースケースコード例
これはスキャン用にバーコードのバー間の間隔を広げる Scale() 呼び出しの例です。
using IronOcr;
using System;
var image = @"small_barcode.png";
var ocr = new IronTesseract();
// Optional: This example uses a barcode
ocr.Configuration.ReadBarCodes = true;
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply scale
input.Scale(400); // 400% is 4 times larger
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);using IronOcr;
using System;
var image = @"small_barcode.png";
var ocr = new IronTesseract();
// Optional: This example uses a barcode
ocr.Configuration.ReadBarCodes = true;
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply scale
input.Scale(400); // 400% is 4 times larger
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private image = "small_barcode.png"
Private ocr = New IronTesseract()
' Optional: This example uses a barcode
ocr.Configuration.ReadBarCodes = True
Dim input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply scale
input.Scale(400) ' 400% is 4 times larger
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)Imports IronOcr
Imports System
Private image = "small_barcode.png"
Private ocr = New IronTesseract()
' Optional: This example uses a barcode
ocr.Configuration.ReadBarCodes = True
Dim input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply scale
input.Scale(400) ' 400% is 4 times larger
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)ビナライズ
フィルターの説明
2 値化フィルターは、適応アルゴリズムに応じて、画像内のすべてのピクセルを黒または白に分類します。 これにより、すべての色が削除され、背景がフラットな白に分離され、テキストとして認識されたものはすべて読みやすいように完全な黒になります。
ユースケースコード例
これは、有色のテキストを整列し、背景色とノイズを除去するための Binarize() 呼び出しの例です。
using IronOcr;
using System;
var image = @"no-binarize.jpg";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply Binarize
input.Binarize();
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);using IronOcr;
using System;
var image = @"no-binarize.jpg";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply Binarize
input.Binarize();
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private image = "no-binarize.jpg"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply Binarize
input.Binarize()
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)Imports IronOcr
Imports System
Private image = "no-binarize.jpg"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply Binarize
input.Binarize()
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)Before Binarize() | After Binarize() |
|---|---|
![]() | ![]() |
反転
フィルターの説明
IronOCRは画像が black text on a white background のときに最もよく読めます。 このために Invert フィルターが使用され、画像上のすべての色を反転させることで達成します。
ユースケースコード例
これは白地に黒を黒地に白に変えるための Invert() 呼び出しの例です。
using IronOcr;
using System;
var image = @"before-invert.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply Invert
input.Invert(true);
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);using IronOcr;
using System;
var image = @"before-invert.png";
var ocr = new IronTesseract();
using var input = new OcrInput();
// Load at least one image
input.LoadImage(image);
// Apply Invert
input.Invert(true);
// Read image into variable: result
var result = ocr.Read(input);
// Example print to console
Console.WriteLine(result.Text);Imports IronOcr
Imports System
Private image = "before-invert.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply Invert
input.Invert(True)
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)Imports IronOcr
Imports System
Private image = "before-invert.png"
Private ocr = New IronTesseract()
Private input = New OcrInput()
' Load at least one image
input.LoadImage(image)
' Apply Invert
input.Invert(True)
' Read image into variable: result
Dim result = ocr.Read(input)
' Example print to console
Console.WriteLine(result.Text)Before | After |
|---|---|
![]() | ![]() |
よくある質問
IronOCR for C#で画像フィルターを使用する目的は何ですか?
IronOCRの画像フィルターは、画像を前処理して視認性を高め、OCR精度を向上させるために使用されます。デノイズ、バイナライズ、デスキューなどのフィルターは、画像品質を大幅に向上させ、OCRがテキストを抽出しやすくします。
バイナライズフィルターはどのようにOCRの性能を向上させますか?
バイナライズフィルターは、画像内のすべてのピクセルを黒か白に変換することで、色の変動を排除し、テキストと背景のコントラストを強化します。これにより、OCR処理でテキストがより判別しやすくなります。
デスキューフィルターはすべての方向の傾斜画像を処理できますか?
IronOCRのデスキューフィルターは、ハフ変換を使用して傾きを修正しますが、わずかに傾いた画像に最適です。著しい回転がある画像には、デスキューを適用する前に回転フィルターが必要になる場合があります。
IronOCRでインバートフィルターを適用する利点は何ですか?
インバートフィルターは、黒地に白いテキストがある画像を白地に黒いテキストに変換するために使用されます。この形式は、OCR処理の効率を高めます。
IronOCRでスケールフィルターを使用する理由は何ですか?
スケールフィルターは画像サイズを変更するために使用され、特に小さなテキストやバーコード画像で、ピクセル密度が低いことで細部が失われる可能性がある場合に役立ちます。これらの画像を拡大することで、OCRやバーコードスキャンの精度が向上する可能性があります。
デノイズフィルターはOCR結果の改善にどのように貢献しますか?
デノイズフィルターは画像から視覚的ノイズやアーティファクトを除去し、テキストをより鮮明にし、OCRの精度を向上させます。これは、信号対雑音比が低い画像で役立ちます。
デスキューフィルターが画像の向きを判断できない場合はどうなりますか?
デスキューフィルターが画像の向きを判断できない場合は、フィルターは適用されません。これは通常、文書に明確な境界や向きを検出するのに十分な内容がない場合に発生します。
トゥーグレースケールフィルターはOCR精度の向上に効果的ですか?
トゥーグレースケールフィルターは、画像をグレースケールに変換し、色情報を簡素化します。OCR精度を大幅に向上させるわけではありませんが、色の複雑さを減らし、処理速度を向上させることができます。
OCR画像の前処理におけるコントラストフィルターの役割は何ですか?
コントラストフィルターは画像のコントラストを自動的に向上させます。これは特に、テキストと背景のコントラストが低い画像のOCR速度と精度を向上させることができます。
IronOCRでは、拡張形態フィルターとしてダイレートおよびエロードがどのように機能しますか?
ダイレートとエロードは高度な形態フィルターです。ダイレートは物体の境界にピクセルを追加し、エロードはその逆です。これらのフィルターは、特定の画像特性に応じて画像の細部を洗練し、OCR結果を向上させるために使用されます。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。





