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

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

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

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

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

 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package BarCode
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package BarCode
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

今日からプロジェクトでIronPDFを使い始めましょう。無料のトライアルをお試しください。

最初のステップ:
green arrow pointer

チェックアウト IronBarcode オン Nuget 迅速なインストールと展開のために。8百万以上のダウンロード数により、をC#で変革しています。

 用 C# NuGet ライブラリ nuget.org/packages/BarCode/
Install-Package BarCode

インストールを検討してください IronBarcode DLL 直接。ダウンロードして、プロジェクトまたはGACの形式で手動でインストールしてください。 IronBarCode.zip

プロジェクトに手動でインストールする

DLLをダウンロード

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

フィルターを適用するには、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);

// Export file to disk
results.ExportFilterImagesToDisk("filteredSample.png");

// 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)

' Export file to disk
results.ExportFilterImagesToDisk("filteredSample.png")

' 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 myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection() {
    new AdaptiveThresholdFilter(0.9f),
    },
};

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

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

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

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

' 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 myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection() {
        new BinaryThresholdFilter(0.9f)
    },
};

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

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

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

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

' 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 myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection() {
    new BrightnessFilter(1.5f),
    },
};

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

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

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

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

' 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 myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection() {
    new ContrastFilter(1.5f),
    },
};

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

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

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

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

' 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 myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection() {
    new InvertFilter(),
    },
};

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

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

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

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

' 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()
    {
        new SharpenFilter((float)3.5),
        new ContrastFilter(2)
    },
};
// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("filteredSample.png");

// 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(CSng(3.5)),
		New ContrastFilter(2)
	}
}
' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("filteredSample.png")

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

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

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

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

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

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

ソフトウェアエンジニア

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