画像補正フィルターの使用方法

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

によって ハイリル ハシミ ビン オマル

正直に言いましょう。 すべての画像が完璧というわけではなく、IronBarcodeによってバーコード画像が読み取れない主な要因の一つでもあります。 これは完全にユーザーの責任ではありません。 再キャプチャーや他の画像強調ソフトウェアを使用する手間をかける代わりに、IronBarcodeにはユーザーがプログラムによって画像にフィルタを適用する機能が搭載されています。 これは、IronBarcodeが画像を読み取り、精度を向上させるのに役立ちます。

続きをお読みいただき、IronBarcodeで利用可能な画像修正フィルター、その画像への効果、およびそれらを適用する方法について学びましょう。

IronBarcodeを始める

今日から無料トライアルでIronBarcodeをあなたのプロジェクトで使い始めましょう。

最初のステップ:
green arrow pointer


画像フィルターを使用して読み取り例を改善。

フィルターを適用するには、ImageFilterCollectionクラスのインスタンスを作成し、それぞれのフィルターのインスタンスを個別に作成します。 その後、オブジェクトをBarcodeReaderOptionsオブジェクトのImageFiltersプロパティに割り当てます。 サンプルイメージと共にオプションオブジェクトを Read メソッドに渡します。

次の画像をサンプル画像として使用しましょう。

サンプル画像

画像を最初に見たところ、かなりぼやけているようです。 しかし、明るさは許容範囲であり、白と黒の色は区別できます。 したがって、バーコードの読み取り精度を向上させるためには、少なくとも SharpenFilterContrastFilter を適用する必要があります。 以下のコードスニペットを参照して、画像にフィルターを適用し、それを読み取って、コンソールに表示します。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-apply-filter.cs
using IronBarCode;
using System;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection()
    {
        new SharpenFilter(3.5f),
        new ContrastFilter(2)
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Write the result value to console
foreach (BarcodeResult result in results)
{
    Console.WriteLine(result.Text);
}
Imports IronBarCode
Imports System

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection() From {
		New SharpenFilter(3.5F),
		New ContrastFilter(2)
	}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Write the result value to console
For Each result As BarcodeResult In results
	Console.WriteLine(result.Text)
Next result
VB   C#

上記のコードスニペットから、フィルタを適用してバーコードを読み取ることに加えて、フィルタ処理された画像をディスクにエクスポートしました。 サンプル画像とフィルター処理された画像の比較は以下でご覧いただけます。

サンプル画像
フィルタリングされたサンプル

画像補正フィルターを探る

IronBarcodeは、画像補正のために特別に設計された複数の画像フィルターを提供します。 これらのフィルターは、不完全なバーコード画像の読み取りを補助し、読み取り精度を向上させることができます。 ただし、ユーザーはこれらのフィルターの動作を理解する必要があります。

適切なフィルターを選択し、フィルターの過剰使用や不適切なフィルターの使用によるパフォーマンスの問題を回避してください。 以下は利用可能なすべてのフィルターです:

  • 適応型しきい値フィルター
  • バイナリしきい値フィルター
  • ブライトネスフィルター
  • コントラストフィルター
  • 反転フィルター
  • シャープンフィルター
  • ブラー フィルター

    • ガウシアンブラー フィルター
  • バイラテラルフィルター
  • メディアンブラー方式フィルター

    これらのフィルターが適用される順序は、ImageFilterCollection 内の配置に基づいています。

適応型閾値フィルター

AdaptiveThresholdFilterは、IronBarcodeで利用可能なフィルターの一つであり、ブラッドリー適応しきい値技術を使用して画像の二値化のためのしきい値を自動的に決定します。 このフィルターは、不均一な照明および異なる背景輝度レベルの画像に最適です。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-adaptive-threshold.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new AdaptiveThresholdFilter(0.9f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("adaptiveThreshold_0.9.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New AdaptiveThresholdFilter(0.9F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("adaptiveThreshold_0.9.png")
VB   C#

以下は、さまざまな値を使用してフィルタを適用した結果です。

デフォルトアダプティブ閾値
0.9適応的しきい値

コンストラクタは、構成のための追加パラメータも受け付けます。

  • 上部: 上部(白)しきい値処理のための色。
  • 下位: Lower(黒)しきい値処理のための色。
  • しきい値: しきい値の制限(0.0-1.0)二値化のために考慮する。
  • 長方形: プロセッサーを適用する領域。

    上記の出力画像に示されているように、画像はの色のみになるように二値化されています。 フィルターを組み合わせて使用する必要があるため、バーコード読み取りには最適ではないようです。 ユーザーは最適な結果を得るためにパラメータの感度を調整する必要があります。

バイナリしきい値フィルター

BinaryThresholdFilterは、指定されたしきい値で画素を分割して画像をフィルタリングします。このフィルタは、色成分の輝度を比較するために使用されます。 AdaptiveThresholdFilterと同様に、このフィルターは正しく使用されない場合、新しいノイズや不要なノイズを導入する可能性があります。 ただし、IronBarcode はフィルタのプロパティのデフォルト値を設定しています。

AdaptiveThresholdFilterと同様に、BinaryThresholdFilterも構成のために同じ追加パラメーターを受け入れます。

  • 上部: 上部(白)しきい値処理のための色。
  • 下位: Lower(黒)しきい値処理のための色。
  • しきい値: しきい値の制限(0.0-1.0)二値化のために考慮する。
  • 長方形: プロセッサーを適用する領域。
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-binary-threshold.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new BinaryThresholdFilter(0.9f)
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("binaryThreshold_0.9.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New BinaryThresholdFilter(0.9F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("binaryThreshold_0.9.png")
VB   C#

以下は、サンプル画像にフィルターを適用した際の出力例です。

デフォルトのバイナリ閾値
0.9 バイナリ閾値

上の出力画像を観察すると、サンプルが白黒の色に2値化されていることがわかります。 しかし、このフィルターはバーコードのバーが消去され、新しいノイズが追加されたため、この画像には明らかに適していないことがわかります。

明るさフィルター

BrightnessFilter は、IronBarcode の画像フィルターコレクションにおけるもう一つの重要なフィルターです。 名前が示すように、このフィルターはバーコード画像の明るさを調整します。 このコンストラクタへの入力によって、出力画像の明るさの量を変えることができます。 デフォルト値は1であり、画像は変更されません。 値を0にすると完全に黒い画像が生成され、1以上の値にすると画像が明るくなります。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-brightness.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new BrightnessFilter(1.5f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("brightness_1.5.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New BrightnessFilter(1.5F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("brightness_1.5.png")
VB   C#

以下は、サンプル入力にこのフィルターを適用した後の出力画像です。

デフォルトの明るさ
1.5 明るさ

コントラストフィルター

ContrastFilterは、画像のコントラストレベルを調整するために使用されます。 画像のコントラストとは、画像内のさまざまな要素の色の強度の違いを指します。 コントラストのレベルを上げることで、詳細の視認性が向上し、画像が鮮明で際立ったものになります。一方、コントラストを下げると、画像はより柔らかく控えめな印象になります。

デフォルト値は1であり、画像は変更されません。 0の値は完全に灰色の画像を作成し、1以上の値は画像のコントラストを高めます。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-contrast.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new ContrastFilter(1.5f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("contrast_1.5.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New ContrastFilter(1.5F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("contrast_1.5.png")
VB   C#

このフィルターをサンプル入力に適用すると、以下の画像が生成されます。

デフォルトコントラスト
1.5 コントラスト

反転フィルター

このフィルターは画像内の色を反転させるのに使用されます。白は黒に、黒は白になるなど、逆の色になります。特に、背景色のあるバーコード画像を読み取ろうとする際に役立ちます。 BinaryThresholdFilterとは異なり、このフィルターは感度を指定する必要なく直接色を反転します。 さらに、このフィルターはCropRectangleと共に使用することで、画像全体の色を反転するのではなく、色を反転する必要がある画像内の位置を指定できます。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-invert.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new InvertFilter(),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("invert.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New InvertFilter()}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("invert.png")
VB   C#

以下の画像は、サンプル入力画像にこのフィルターを適用した結果です。

元の画像
反転

シャープ化フィルター

IronBarcode の最後の画像修正フィルターは SharpenFilter です。 このフィルターは画像のシャープネスを強化し、ぼやけた画像に非常に有用です。 ユーザーはフィルターオブジェクトをインスタンス化する際に、シグマ値を調整することで画像のシャープネスを調整するためのこのフィルターを操作できます。 デフォルト値は3です。シグマ値を増やすことで画像の鮮明さを向上させることができます。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-sharpen.cs
using IronBarCode;
using System;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new SharpenFilter(0.5f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("sharpen_0.5.png");
Imports IronBarCode
Imports System

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New SharpenFilter(0.5F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("sharpen_0.5.png")
VB   C#

以下の画像は シャープネッド サンプル入力画像のバージョン。

デフォルトのシャープネス
0.5 シャープ化

上記の画像と元の画像を比較すると、こちらの方がより鮮明であり、IronBarcodeを使用してのバーコード読み取りに確実に役立つと考えられます。 ほとんどの場合、SharpenFilter は常に ImageFilterCollection クラスの他のフィルターと一緒に適用されます。

ブラー フィルター

ガウシアンブラー フィルター

GaussianBlurFilterは、画像にガウスぼかしを適用するために使用されます。 このフィルターは、画像のノイズを低減するためによく使用されます。

フィルターはガウス関数を使用して画像内の隣接するピクセル値を平均化することで機能します。 この方法は、2つの調整可能な要因に依存しています。

  • カーネル: ピクセルを平均化するために使用される行列。
  • Sigma: ぼかしの強度を制御する値です。

    デフォルトのカーネルサイズは3x3ピクセルで、デフォルトのSigma値は3.0です。これにより、中程度のぼかしが生成されます。 シグマ値を増やすと、より強いぼかし効果が得られます。 また、ぼかしフィルターが平均化する近傍のサイズを制御するためにカーネルをカスタマイズすることもできます。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-gaussianblur.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new GaussianBlurFilter(3, 3, 3.0f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("gaussianBlur.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New GaussianBlurFilter(3, 3, 3.0F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("gaussianBlur.png")
VB   C#

このフィルターをサンプル入力に適用すると、以下の画像が生成されます。

デフォルトのシャープネス
ガウシアンブラー画像

バイラテラルフィルター

BilateralFilterは、エッジを保ちながら画像を滑らかにするために使用されます。 単純なぼかし技術とは異なり、バイラテラルフィルタは色の違いとピクセル距離の両方を考慮に入れるため、エッジを保持したスムージングに効果的です。

その方法は、3つの調整可能な要因に依存しています。

  • NeighborhoodDiameter: フィルタリングに使用されるピクセル近傍の直径を指定します。 より大きな直径はフィルター内の周囲のピクセルを多く含みます。 デフォルト値は5です。
  • SigmaColor: これはカラーの影響を表します。 隣接するピクセル間の色差がフィルタリングにどの程度影響を与えるかを決定します。 より高い値は、異なる色のピクセル同士がより相互に影響を与えることを意味します。 デフォルト値は 75.0 です。
  • SigmaSpace: これは空間的影響を表します。 ピクセル間の距離がフィルタリングにどの程度影響を与えるかを決定します。 より高い値は、離れたピクセル同士が互いにより影響を与えることを意味します。 デフォルト値は 75.0 です。
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-bilateral.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new BilateralFilter(5, 75, 75),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("bilateral.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New BilateralFilter(5, 75, 75)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("bilateral.png")
VB   C#

このフィルターをサンプル入力に適用すると、以下の画像が生成されます。

デフォルトのシャープネス
両側画像

メディアンブラー フィルター

MedianBlurFilter は、周囲のピクセルの中央値で各ピクセルの値を置き換えることにより、画像のノイズを低減するために使用されるフィルターです。 このフィルターは、ノイズを除去しながらエッジを保持するのに特に効果的です。

  • KernelSize: 各ピクセルの周囲で中央値を計算するために使用される近傍のサイズを定義します。 値は0より大きい奇数でなければなりません。デフォルト値は5です。
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-medianblur.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new MedianBlurFilter(5),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("medianBlur.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New MedianBlurFilter(5)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("medianBlur.png")
VB   C#

このフィルターをサンプル入力に適用すると、以下の画像が生成されます。

デフォルトのシャープネス
メディアンブラー画像

イテレーションを保存

バーコードに複数のフィルターを適用する際、各フィルターメソッド後の出力を視覚化するのは難しい場合があります。 この機能は、各フィルターが適用された後にフィルター処理された画像を順次保存することを可能にします。 この機能を有効にするには、最初に ImageFilterCollection コンストラクタに true を渡します。 次に、ExportFilterImagesToDisk メソッドを使用して、出力画像のパスと名前を指定します。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-save-iterations.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new SharpenFilter(3.5f),
        new AdaptiveThresholdFilter(0.5f),
        new ContrastFilter(2)
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.webp", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("filteredImage.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {
		New SharpenFilter(3.5F),
		New AdaptiveThresholdFilter(0.5F),
		New ContrastFilter(2)
	}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.webp", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("filteredImage.png")
VB   C#

フィルターはコードの順序で適用され、出力画像は各イテレーションの結果を反映します。

  • シャープ -> シャープ後
  • シャープ + アダプティブしきい値 -> アダプティブしきい値後
  • シャープ化 + 適応的しきい値 + コントラスト -> コントラスト後
    サンプル画像
    1.5 コントラスト
    サンプル画像
    1.5 コントラスト

    以外 イメージフィルターズ プロパティ、ユーザーは他のプロパティも追加できます。 バーコードリーダーオプション より正確な読み取りのためには、これをご覧ください 記事 詳細については、こちらをご覧ください。

Hairil related to イテレーションを保存

ハイリル ハシミ ビン オマル

ソフトウェアエンジニア

すべての優れたエンジニアと同じように、Hairilは熱心な学習者です。C#、Python、およびJavaの知識を洗練させ、その知識を活かしてIron Softwareのチームメンバーに価値を提供しています。Hairilはマレーシアのマラ工科大学(Universiti Teknologi MARA)で化学およびプロセス工学の学士号を取得し、Iron Softwareチームに加わりました。