C#で画像の色を読みやすくする方法
C#でIronOCRの二値化、グレースケール、反転、色置換メソッドを使って画像の色を修正し、テキストの可読性とOCRの精度を向上させます。 ターゲット抽出のために特定のテキストの色をお読みください。
クイックスタート: 特定のテキストカラーを分離するIronOCRのSelectTextColorメソッドを使用して、特定のテキストカラーにフォーカスしたOCRを行います。複雑な画像操作は不要です。 画像を読み込み、テキストの色と許容範囲を選択し、そのテキストのみを抽出して正確なOCR結果を得ます。
-
1IronOCR を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
new IronTesseract().Read(new IronOcr.OcrImageInput("sample.jpg").SelectTextColor(new IronSoftware.Drawing.Color("#DB645C"), 60));Dim OcrEngine As New IronTesseract() OcrEngine.Read(New IronOcr.OcrImageInput("sample.jpg").SelectTextColor(New IronSoftware.Drawing.Color("#DB645C"), 60))Dim OcrEngine As New IronTesseract() OcrEngine.Read(New IronOcr.OcrImageInput("sample.jpg").SelectTextColor(New IronSoftware.Drawing.Color("#DB645C"), 60)) -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronOCRを使い始めるには、無料トライアルをお試しください。
C#で読み取り用に画像の色を修正する方法
- 画像カラーを修正するC#ライブラリをダウンロードする
- 読み取り用にPDFドキュメントと画像をインポートする
- カラー効果の適用:二値化、グレースケール、反転、色置換
- ビューイング用に修正された画像をエクスポート
SelectTextColorを使用して特定のテキスト色を読み取ります。
画像を二値化してOCRを向上させる方法
2値化は、画像を2色形式(通常は白黒)に変換します。これにより、テキストと背景が分離され、ノイズが減少するため、テキストがより明瞭になり、読みやすくなります。
2値化がOCRの精度を向上させる理由は?
Binarizeメソッドを使用して2値化を適用します。 OCRは、白地に黒文字の高コントラスト画像に最適です。 この方法では、背景と文字が明確に区別されます。
二値化は、照明ムラや背景ノイズのあるスキャン文書に適しています。このアルゴリズムは画像を分析し、前景テキストと背景を分離するための最適なしきい値を決定します。 高度な前処理技術については、OCR画像最適化フィルターガイドを参照してください。
2値化のコード実装とは?
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Apply binarize affect
imageInput.Binarize();
// Export the modified image
imageInput.SaveAsImages("binarize.jpg");using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Apply binarize affect
imageInput.Binarize();
// Export the modified image
imageInput.SaveAsImages("binarize.jpg");Imports IronOcr
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Apply binarize affect
imageInput.Binarize()
' Export the modified image
imageInput.SaveAsImages("binarize.jpg")Imports IronOcr
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Apply binarize affect
imageInput.Binarize()
' Export the modified image
imageInput.SaveAsImages("binarize.jpg")バッチ処理では、2値化と他のフィルタを組み合わせてください。 画像に最適なフィルターの組み合わせを決定するには、フィルタウィザードを利用してください。
結果をエクスポートして比較する方法
変更された画像をSaveAsImagesメソッドを使用してエクスポートします。 二値化前と二値化後の画像を比較してください。

ビフォー

アフター
読みやすくするために画像をグレースケールに変換する方法
画像をグレースケールに変換することで、視覚的な乱れを減らし、読みやすさを向上させます。 オリジナルカラーがコンテンツの邪魔になる場合にも役立ちます。
グレースケールを他の方法より優先して使用すべき場合
ToGrayScaleメソッドを使用してグレースケールを適用します。 プロセスでは、R、G、Bの値を平均化します。
グレースケールは、テキスト認識の妨げとなる色付きの背景や透かしに適しています。 2値化とは異なり、グレースケールは微妙に変化する画像のディテールを保持します。 テキストが埋め込まれた写真や複雑なレイアウトの写真にお使いください。 低品質のスキャンについては、低品質スキャンと画像の修正を参照してください。
グレースケール変換のシンプルなコードとは?
// Apply grayscale affect
imageInput.ToGrayScale();// Apply grayscale affect
imageInput.ToGrayScale();' Apply grayscale affect
imageInput.ToGrayScale()' Apply grayscale affect
imageInput.ToGrayScale()さまざまな画像品質のため、グレースケールと他の前処理ステップを組み合わせてください。 OcrInput クラスのドキュメントは、複数のフィルターを連鎖させる方法を説明しています。
グレースケールと元の画像の比較

ビフォー

アフター
画像の色を反転すべき場合
色を反転させるとコントラストが強調されます。黒い背景に白いテキストを白い背景に黒いテキストに変換すると、読みやすさが向上します。
色反転を実装する方法
色を反転させるには、Invertメソッドを使用してください。 カラーチャンネルを削除してグレースケールを返すには、ブール値を渡します。
色の反転は、ネガ画像や暗いテーマのスクリーンショットを扱います。 最近のアプリケーションは、従来のOCRでは難しかったダークモードのインターフェースを使用しています。 これらの画像を反転させることで、最適な認識を保証します。 様々な画像タイプについては、高速OCR設定ガイドを参照してください。
// Apply invert affect
imageInput.Invert();// Apply invert affect
imageInput.Invert();' Apply invert affect
imageInput.Invert()' Apply invert affect
imageInput.Invert()ドキュメントの種類が混在するバッチ処理では、自動的に暗い背景を検出する機能を実装します。 コンピュータビジョン機能は、IronOCRで反転が必要な時を識別します。
反転オプションの違い
グレースケールの有無でInvertメソッドを比較してください:

反転

反転&グレースケール
画像の特定の色を置き換える方法
特定の色に置き換えて、要素を強調したり、強調しなかったりします。 テキストを目立たせたり、問題のあるコントラストを修正したりする場合に使用します。
色の置換で許容度が重要な理由は?
ReplaceColorメソッドは、現在の色、新しい色、および許容値を必要とします。 より高い耐性は、ぼやけた画像をよりよく処理します。
許容誤差は、ピクセルがターゲットカラーとどの程度一致しなければならないかを決定します。 低い値(0~50)が均一な色に適しています。 高い値(100~200)は、アンチエイリアステキストや圧縮アーチファクトを処理します。 これは、インクのにじみや紙の質感によってばらつきが生じるスキャン文書に役立ちます。
色の置き換えを実装する方法
using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
IronSoftware.Drawing.Color currentColor = new IronSoftware.Drawing.Color("#DB645C");
IronSoftware.Drawing.Color newColor = IronSoftware.Drawing.Color.DarkCyan;
// Replace color
imageInput.ReplaceColor(currentColor, newColor, 80);
// Export the modified image
imageInput.SaveAsImages("replaceColor");using IronOcr;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
IronSoftware.Drawing.Color currentColor = new IronSoftware.Drawing.Color("#DB645C");
IronSoftware.Drawing.Color newColor = IronSoftware.Drawing.Color.DarkCyan;
// Replace color
imageInput.ReplaceColor(currentColor, newColor, 80);
// Export the modified image
imageInput.SaveAsImages("replaceColor");Imports IronOcr
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sample.jpg")
Dim currentColor As New IronSoftware.Drawing.Color("#DB645C")
Dim newColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.DarkCyan
' Replace color
imageInput.ReplaceColor(currentColor, newColor, 80)
' Export the modified image
imageInput.SaveAsImages("replaceColor")
End UsingImports IronOcr
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sample.jpg")
Dim currentColor As New IronSoftware.Drawing.Color("#DB645C")
Dim newColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.DarkCyan
' Replace color
imageInput.ReplaceColor(currentColor, newColor, 80)
' Export the modified image
imageInput.SaveAsImages("replaceColor")
End Using高度な色置換シナリオの対応方法
複雑なシナリオのために複数の置換を連鎖させます:
/* :path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-multiple-replacements.cs */
// Replace multiple colors in sequence
using var imageInput = new OcrImageInput("multi-color-document.jpg");
// Replace red text with black
imageInput.ReplaceColor(IronSoftware.Drawing.Color.Red, IronSoftware.Drawing.Color.Black, 70);
// Replace blue headers with dark gray
imageInput.ReplaceColor(IronSoftware.Drawing.Color.Blue, IronSoftware.Drawing.Color.DarkGray, 60);
// Replace light yellow background with white
imageInput.ReplaceColor(new IronSoftware.Drawing.Color("#FFFACD"), IronSoftware.Drawing.Color.White, 40);
// Perform OCR on the cleaned image
var result = ocrTesseract.Read(imageInput);/* :path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-multiple-replacements.cs */
// Replace multiple colors in sequence
using var imageInput = new OcrImageInput("multi-color-document.jpg");
// Replace red text with black
imageInput.ReplaceColor(IronSoftware.Drawing.Color.Red, IronSoftware.Drawing.Color.Black, 70);
// Replace blue headers with dark gray
imageInput.ReplaceColor(IronSoftware.Drawing.Color.Blue, IronSoftware.Drawing.Color.DarkGray, 60);
// Replace light yellow background with white
imageInput.ReplaceColor(new IronSoftware.Drawing.Color("#FFFACD"), IronSoftware.Drawing.Color.White, 40);
// Perform OCR on the cleaned image
var result = ocrTesseract.Read(imageInput);Imports IronSoftware.Drawing
' Replace multiple colors in sequence
Using imageInput As New OcrImageInput("multi-color-document.jpg")
' Replace red text with black
imageInput.ReplaceColor(Color.Red, Color.Black, 70)
' Replace blue headers with dark gray
imageInput.ReplaceColor(Color.Blue, Color.DarkGray, 60)
' Replace light yellow background with white
imageInput.ReplaceColor(New Color("#FFFACD"), Color.White, 40)
' Perform OCR on the cleaned image
Dim result = ocrTesseract.Read(imageInput)
End UsingImports IronSoftware.Drawing
' Replace multiple colors in sequence
Using imageInput As New OcrImageInput("multi-color-document.jpg")
' Replace red text with black
imageInput.ReplaceColor(Color.Red, Color.Black, 70)
' Replace blue headers with dark gray
imageInput.ReplaceColor(Color.Blue, Color.DarkGray, 60)
' Replace light yellow background with white
imageInput.ReplaceColor(New Color("#FFFACD"), Color.White, 40)
' Perform OCR on the cleaned image
Dim result = ocrTesseract.Read(imageInput)
End Using複雑な色置換については、追加の前処理技術を確認するために画像補正フィルターをご覧ください。
色置換の視覚的結果

ビフォー

アフター
特定のテキスト色だけを読む方法
特定のテキストカラーを読み取るには、SelectTextColorメソッドを使用してください。 ターゲットカラーと許容範囲(0~255)を指定してください。 許容誤差は、R、G、B値について、ピクセルと選択された色との間の許容可能な差異を表します。
色許容度が結果に与える影響
using IronOcr;
using System;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Text color to focus on
IronSoftware.Drawing.Color focusColor = new IronSoftware.Drawing.Color("#DB645C");
// Specify which text color to read
imageInput.SelectTextColor(focusColor, 60);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Output result to console
Console.WriteLine(ocrResult.Text);using IronOcr;
using System;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Text color to focus on
IronSoftware.Drawing.Color focusColor = new IronSoftware.Drawing.Color("#DB645C");
// Specify which text color to read
imageInput.SelectTextColor(focusColor, 60);
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Output result to console
Console.WriteLine(ocrResult.Text);Imports IronOcr
Imports System
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Text color to focus on
Private focusColor As New IronSoftware.Drawing.Color("#DB645C")
' Specify which text color to read
imageInput.SelectTextColor(focusColor, 60)
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output result to console
Console.WriteLine(ocrResult.Text)Imports IronOcr
Imports System
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Text color to focus on
Private focusColor As New IronSoftware.Drawing.Color("#DB645C")
' Specify which text color to read
imageInput.SelectTextColor(focusColor, 60)
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output result to console
Console.WriteLine(ocrResult.Text)色分けされたセクションのあるフォーム、ハイライトされたテキストの抽出、特定の警告メッセージの読み取りには、選択的なカラー読み取りを使用します。 構造化データ抽出については、OcrResultクラスの高度な処理を参照してください。
色選択から期待できる出力
OCRの結果は、オレンジ色のテキストのみを読み取ります:

フィルターを適用した修正済みPDFを保存する方法
IronOCRはフィルタの有無にかかわらず、変更されたPDFを保存します。 SaveAsSearchablePdfメソッドの第2パラメータは、フィルターが適用された状態で保存するかどうかを指定します。
検索可能なPDFの実装
using IronOcr;
// Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
var ocr = new IronTesseract();
var ocrInput = new OcrInput();
// Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf");
// Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale();
// Run OCR on the preprocessed input
OcrResult result = ocr.Read(ocrInput);
// Write the searchable PDF; true = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", true);using IronOcr;
// Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
var ocr = new IronTesseract();
var ocrInput = new OcrInput();
// Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf");
// Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale();
// Run OCR on the preprocessed input
OcrResult result = ocr.Read(ocrInput);
// Write the searchable PDF; true = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", true);Imports IronOcr
' Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
Dim ocr As New IronTesseract()
Dim ocrInput As New OcrInput()
' Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf")
' Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale()
' Run OCR on the preprocessed input
Dim result As OcrResult = ocr.Read(ocrInput)
' Write the searchable PDF; True = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", True)Imports IronOcr
' Create OCR engine: filters are applied at the OcrInput level, so no configuration changes are needed here
Dim ocr As New IronTesseract()
Dim ocrInput As New OcrInput()
' Load the scanned PDF as the OCR source
ocrInput.LoadPdf("invoice.pdf")
' Convert to grayscale: removes color noise that can reduce OCR accuracy on color-printed documents
ocrInput.ToGrayScale()
' Run OCR on the preprocessed input
Dim result As OcrResult = ocr.Read(ocrInput)
' Write the searchable PDF; True = embed the grayscale-filtered image rather than the original color scan
result.SaveAsSearchablePdf("outputGrayscale.pdf", True)保存されたPDFにフィルターを適用すべき場合
よりクリーンで読みやすいドキュメントを作成するには、フィルタを適用します。 これは、アーカイブ目的や異なるデバイス間でのドキュメント共有に役立ちます。 詳細については、OCRで検索可能なPDFを作成するをご覧ください。
表を含む複雑なドキュメントについては、ドキュメント内の表を読むで専門的な抽出を探ってください。 大量処理の場合、マルチスレッドTesseract OCRを参照して複数のドキュメントにわたるフィルター適用を最適化してください。
よくある質問
C# OCR における画像の二値化の目的は何ですか?
二値化は画像を通常白黒の二色形式に変換し、背景からテキストを分離してノイズを軽減します。これにより高コントラストの画像が作成され、テキストの可読性とOCRの精度が向上します。
SelectTextColor メソッドは OCR 結果をどのように改善しますか?
SelectTextColor メソッドは、特定のテキストカラーに OCR を集中させ、複雑な画像操作をせずにターゲットのテキストを抽出できるようにします。これにより、複数のテキストカラーを含む文書に対処する際の精度が向上します。
画像前処理でグレースケール変換をいつ使用すべきですか?
グレースケール変換は、分散した色を持つ画像の視覚的な混乱を減らすのに役立ちます。カラーの背景や透かしがテキスト認識の妨げとなる場合に適しており、複雑なレイアウトの詳細を維持します。
なぜ色の反転が OCR に有利なのですか?
色の反転は、黒背景の白文字を白背景の黒文字に変換することでコントラストを高め、可読性を向上させます。特にダークテーマのスクリーンショットやネガティブ画像に役立ちます。
ReplaceColor メソッドにおいて耐性はどのような役割を果たしますか?
ReplaceColor メソッドにおける耐性は、ピクセルが置換対象の色とどれほど近く一致する必要があるかを定義します。これは、ぼやけた画像やインクのにじみのある文書を取り扱う際に重要で、テキストの目立ちやコントラストを改善します。
IronOCRは OCR 処理の速度をどのように改善できますか?
IronOCR はマルチスレッド処理をサポートしており、大量の文書を扱う際に OCR を高速化します。これにより複数の画像にフィルターを適用する速度が上がり、全体的な効率が向上します。
OCRでの選択的な色読み取りから期待される出力は何ですか?
選択的な色読み取りによる OCR の結果は、ハイライトされたテキストや特定の警告メッセージなど、指定された色のみに焦点を当てて、色分けされた文書から正確なデータ抽出を可能にします。
IronOCR で検索可能な PDF をどのように作成できますか?
IronOCR は、SaveAsSearchablePdf メソッドを使用して変更されたPDFを保存することができます。グレースケールなどのフィルターを適用することで、結果の文書でのテキストの可読性とノイズの削減を向上させることができます。
低品質のスキャン画像に対するOCRの精度を向上させる前処理ステップは何ですか?
低品質のスキャン画像に対しては、ノイズを軽減しコントラストを高めることでテキストの可読性と OCR 精度を大幅に向上させるために、二値化、グレースケール変換、色反転などの前処理ステップが効果的です。
IronOCR は構造化データの抽出にどのように役立ちますか?
IronOCR は OcrResult クラスのような高度な処理能力を提供し、画像からの構造化データ抽出をサポートします。これにより、色分けされたセクションやハイライトされたテキストを含むフォームに最適です。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。