C#でバーコードの画像補正フィルターを使用する方法

バーコードのデコードを改善するための C# 画像補正フィルタの使用

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

IronBarcodeは、SharpenFilterContrastFilterのような組み込みの画像補正フィルタを提供し、プログラムによって不鮮明または不完全なバーコード画像を補正し、外部の画像編集ソフトウェアや画像の再キャプチャを必要とせずに読み取り精度を向上させます。

すべての画像が完璧というわけではなく、画質の悪さはIronBarcodeでのバーコード読み取りを成功させない主な要因の一つです。 IronBarcodeは、画像の再キャプチャや外部の画像補正ソフトウェアを使用する代わりに、プログラムによって画質を向上させる組み込みフィルタを提供します。 これらのフィルタは、IronBarcodeが難しい画像を読み取り、全体的な精度を向上させるのに役立ちます。

IronBarcodeで利用可能な画像補正フィルター、それらが画像に与える影響、およびそれらの適用方法についてさらにお読みください。 より包括的なバーコード読み取り技術については、バーコード読み取りチュートリアルをご覧ください。

クイックスタート: バーコード読み取りを改善するためのシャープネスとコントラストフィルターの適用

たった1ステップで、BarcodeReaderOptionsImageFilterCollectionを使って、IronBarcodeのSharpenFilterContrastFilterを適用することができます。 これは、最小限のセットアップと外部ツールの必要性ゼロでBarCodeスキャンを改善します。

Nuget Icon今すぐ NuGet で PDF を作成してみましょう:

  1. NuGet パッケージ マネージャーを使用して IronBarcode をインストールします

    PM > Install-Package BarCode

  2. このコード スニペットをコピーして実行します。

    BarcodeResults results = IronBarCode.BarcodeReader.Read("input.png", new IronBarCode.BarcodeReaderOptions { ImageFilters = new IronBarCode.ImageFilterCollection() { new IronBarCode.SharpenFilter(3.5f), new IronBarCode.ContrastFilter(2.0f) } });
  3. 実際の環境でテストするためにデプロイする

    今すぐ無料トライアルでプロジェクトに IronBarcode を使い始めましょう
    arrow pointer

バーコードの読み取りを向上させるために画像フィルタを適用するにはどうすればよいですか?

フィルタを適用するには、ImageFilterCollectionクラスをインスタンス化し、各フィルタのインスタンスを個別に作成します。 次に、オブジェクトをBarcodeReaderOptionsオブジェクトのImageFiltersプロパティに割り当てます。 オプションオブジェクトをサンプル画像と一緒にReadメソッドに渡します。 高度なインストールオプションについては、NuGetパッケージガイドをご覧ください。

以下の画像をサンプル画像として使用してください。

Blurred barcode with number 4900203187590 showing poor image quality before filtering enhancement

画像がかなりぼやけて見えます。 しかし、明るさは許容範囲で、白黒の色は区別できます。 したがって、バーコードの読みやすさを向上させるために、少なくとも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
$vbLabelText   $csharpLabel

上のコード・スニペットは、フィルタを適用し、BarCodeを読み取り、フィルタリングされた画像をディスクにエクスポートします。 サンプル画像とフィルター画像の比較を以下に示します。

Blurry barcode image with number 4902030187590 demonstrating poor image quality
Barcode with improved readability after applying image filters, showing clear vertical lines and number 4902030187590

IronBarcodeではどのような画像補正フィルタが利用できますか?

IronBarcodeは、特に画像補正のために設計された複数の画像フィルターを提供しています。 これらのフィルタは、不完全な BarCode 画像の読み取りを支援し、読み取り精度を向上させます。 しかし、適切なフィルタを選択し多すぎるフィルタの使用や間違ったフィルタの使用によるパフォーマンスの問題を避けるために、これらのフィルタがどのように機能するかを理解してください。 利用可能なフィルタは次のとおりです:

  • AdaptiveThresholdFilter (適応しきい値フィルタ)
  • BinaryThresholdFilter(バイナリしきい値フィルタ
  • BrightnessFilter(ブライトネスフィルター
  • <コード>コントラストフィルタ</コード
  • <コード>InvertFilter</コード
  • <コード>SharpenFilter</コード
  • <コード>ErodeFilter</コード
  • <コード>DilateFilter</コード
  • ヒストグラム等化フィルタ
  • Blur Filters
    • <コード>GaussianBlurFilter</コード
    • <コード>BilateralFilter</コード
    • <コード>MedianBlurFilter</コード

フィルターが適用される順序は、ImageFilterCollection内の配置に基づいています。 これらのフィルタの詳細なAPIドキュメントについては、API Referenceをご覧ください。

適応しきい値フィルターはどのように機能しますか?

AdaptiveThresholdFilterはIronBarcodeで利用可能なフィルターで、Bradley Adaptive Threshold テクニックを画像に適用し、画像を二値化するためのしきい値を自動的に決定します。 このフィルターは照明が不均一で背景強度が変化する画像に最適です。

: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")
$vbLabelText   $csharpLabel

以下は異なる値を使用してフィルターを適用した結果です。

Vertical lines showing different adaptive threshold filter outputs with solid and dashed patterns
Low-quality barcode image showing UPC number 902030187590 with significant visual distortion

コンストラクタは、設定のための追加パラメータを受け付けます:

  • Upper:閾値処理の上限色(白)。
  • :閾値の下側(黒)の色。
  • Threshold: 2値化のしきい値(0.0~1.0)。
  • 矩形:プロセッサを適用する矩形領域。

上の出力画像に示されているように、画像はの色だけを持つように2値化されています。 バーコードの読み取りにはまだ理想的ではないようですが、フィルターは組み合わせて使用する必要があります。 最良の結果を得るために、パラメータ感度を試してください。

バイナリしきい値フィルターはどのように機能しますか?

BinaryThresholdFilter は、与えられたしきい値でピクセルを分割し、色成分の輝度を比較することによって画像をフィルタリングします。 AdaptiveThresholdFilterと同様に、このフィルタは正しく使用しないと、新しいノイズや不要なノイズを引き起こす可能性があります。 ただし、IronBarcodeはフィルタのプロパティにデフォルト値を設定しています。

AdaptiveThresholdFilter と同様に、BinaryThresholdFilter は、設定のための同じ追加パラメータを受け入れます:

  • Upper:閾値処理の上限色(白)。
  • :閾値の下側(黒)の色。
  • Threshold: 2値化のしきい値(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")
$vbLabelText   $csharpLabel

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

Three examples of binary threshold filter outputs showing sparse, dotted, and dense vertical line patterns
Barcode image processed with 0.9 binary threshold filter showing black and white contrast

上の出力画像を見ると、サンプルは白黒に2値化されています。 しかし、バーコードバーが除去され、新たなノイズが混入しているため、このフィルタがこの画像に適していないことは明らかです。 難しい BarCode シナリオの処理については、troubleshooting guide for unrecognized barcodes を参照してください。

バーコードの読み取りを向上させるために画像の明るさを調整するにはどうすればよいですか?

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")
$vbLabelText   $csharpLabel

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

Blurry UPC barcode sample showing default brightness level before filter enhancement
Blurry barcode with product number 4902030187590, demonstrating low brightness or poor image quality

バーコード画像を強調するためにコントラスト フィルターを使用するには?

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")
$vbLabelText   $csharpLabel

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

Blurry barcode with number 4902030187590 demonstrating default contrast filter settings
Blurry barcode with number 4902030187590 demonstrating low contrast image quality

いつ反転フィルタを使用すべきですか?

このフィルタは、画像内の色を反転させ、白が黒に、黒が白になるように反対色にします。背景色のある BarCode 画像を読み取るときに特に便利です。 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")
$vbLabelText   $csharpLabel

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

Blurry UPC barcode showing number 480203187590 - original image before invert filter application
Blurry inverted barcode showing white bars on dark background with number sequence 4902030187590

鮮明化フィルタを使用して、ぼやけた BarCode 画像を修正するにはどうすればよいですか?

IronBarcodeはシャープフィルターを提供します。 このフィルターは画像のシャープネスを強化し、ぼやけた画像を処理する際に非常に有用です。 このフィルタを操作して、フィルタオブジェクトをインスタンス化するときにシグマ値を調整することで、画像のシャープネスを調整します。 デフォルト値は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")
$vbLabelText   $csharpLabel

The image below is the sharpened version of the sample input image.

Blurry barcode image demonstrating unsharpened quality before applying sharpen filter
Blurred barcode example showing effects of image quality degradation

上の画像と元の画像を比較すると、より鮮明に表示され、IronBarcodeを使用したバーコードの読み取りに役立ちます。 ほとんどの場合、SharpenFilterImageFilterCollectionクラスの他のフィルターと一緒に常に適用されます。

エロード フィルターは何に使用されますか?

ErodeFilterは、微小なホワイトノイズを除去し、図形のエッジ付近のピクセルを除去することでBarCodeバーを太くします。このフィルタは、バーコードの背景に白い斑点が多い場合や、バーコード画像の解像度が低すぎたり不鮮明で、バーが統合されてしまう場合に最適です。 ErodeFilterは、背景の白い斑点を取り除きながら、バーを太くします。 不完全な画像の処理については、不完全なバーコードの例を参照してください。

フィルターにkernelSizeを表す整数を入力することで、侵食の効果を高めます。 カーネルサイズが大きいほど、入力画像に対する効果が強くなります。 kernelSizeは正方形であり、この例では5x5カーネルであることに注意してください。

例として、ErodeFilterのカーネルサイズを大きくして、フィルターの効果を紹介してください。

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

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

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

// Export file to disk
results.ExportFilterImagesToDisk("erodeFilter.jpg");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
Blurry barcode showing number 4002030187590 - example for erode filter demonstration
Blurred barcode showing vertical black and white stripes with numerical code

上の入力画像と出力画像を比較すると、フィルタリングに大きなカーネル・サイズを入力したため、いくつかのバーが目に見えて太くなっています。 ただし、全体的な画像の白斑点は減少しました。 エロージョンフィルターの性質上、カーネルサイズが大きくなると、上の写真のように、強引に適用しすぎると細いバーを消してしまう可能性があります。 ErodeFilterに入力するカーネルサイズの値を変更して、効果をテストし、改良してください。

ディレート フィルターは BarCode 読み取りにどのように役立ちますか?

DilateFilterは、ErodeFilterの逆の機能を持ち、オブジェクトの境界にピクセルを追加することによって、明るい領域、通常は背景を拡張します。 このフィルタは、小さなギャップを埋めたり、低コントラスト領域を強調したりすることで、損傷したバーコードや淡いバーコードを修復しますが、バーコードバーへの効果は直感とは異なることに注意してください。 ディレーションは明るいスペースを拡大するため、(背景が白であると仮定した場合)黒いBarCodeバーのような暗い要素は間接的に薄くなります。 これは、非常に太いまたは合併したバーコードバーが現れる場面で特に効果的であり、過剰な使用はバーを細くしすぎてスキャン精度を低下させる可能性があります。

上記と同様に、フィルタのkernelSizeを表す整数を入力することで、フィルタの効果を増加させます。

以下の例では、DilateFilterの効果を紹介するために、より大きなカーネル・サイズを使用してください。

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

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

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

// Export file to disk
results.ExportFilterImagesToDisk("dilateFilter.jpg");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
Blurry barcode image showing number 4902030187590 - example for dilate filter processing
Blurred barcode with numerical sequence below vertical bars

上の画像に示されているように、DilateFilterを積極的に使用すると、バーコード構造が破壊され、間隔の狭いバーがマージされ、バーコードにクワイエット ゾーンができる可能性があります。 入力画像に応じてカーネル・サイズの値を大きくしたり小さくしたりして、画像への効果をテストし、改良してください。

どのような場合にヒストグラム等化フィルタを使用すべきですか?

HistogramEqualizationFilterは、ピクセルの強度を再分配することによって画像のコントラストを強調し、明瞭さを向上させます。 バーコードのコントラストが低い場合(色あせた画像や洗いざらしの画像など)、または照明にムラがある場合(暗い影や明るいまぶしさなど)によく使用されます。 ピクセルの明るさの分布である画像ヒストグラムを分析することによって、暗いピクセルを暗くし、明るいピクセルを明るくするように、強度範囲をストレッチしてコントラストを高めることでピクセル値を再配布します。

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

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

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

// Export file to disk
results.ExportFilterImagesToDisk("histogramEqualizationFilter.jpg");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
Blurry barcode with number 4902030187590 used as test image for histogram equalization filter
Barcode with vertical black and white stripes showing number 4902030187590

上の画像に示されているように、元の画像と比較して、黒いバーが目に見えて暗くなり、スペースが目に見えて明るくなっています。

バーコードのノイズ除去に役立つぼかしフィルタはどれですか?

ガウスぼかしフィルタはどのように画像のノイズを減らしますか?

GaussianBlurFilter は画像にガウスぼかしを適用します。 このフィルタは、一般的に画像のノイズを低減します。 不完全な BarCode を扱うための包括的なガイドについては、image orientation correction tutorial を参照してください。

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

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

デフォルトのkernelサイズは3x3ピクセルで、デフォルトのSigma値は3.0で、中程度のぼかしを生成します。 シグマの値を大きくすると、ぼかし効果が強くなります。 また、kernelをカスタマイズして、ぼかしフィルタが平均化する近傍領域のサイズを制御することもできます。

: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")
$vbLabelText   $csharpLabel

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

Blurry barcode with number 4902030187590 demonstrating poor image quality
Barcode image with Gaussian blur filter applied, showing blurred vertical lines and distorted numbers

バイラテラル・フィルターはいつ使うべきですか?

BilateralFilterは、エッジを保持しながら画像を滑らかにします。 すべてのピクセルに一様に影響を与える単純なぼかし技術とは異なり、バイラテラル・フィルターは色の違いとピクセルの距離の両方を考慮するため、エッジを維持したスムージングに効果的です。

このメソッドは3つの調整可能な要素に依存しています:

  • NeighborhoodDiameter:ピクセル近傍領域の直径 (デフォルト: 5)。
  • シグマカラー:色差の影響を決定する色の影響 (デフォルト: 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")
$vbLabelText   $csharpLabel

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

Blurred barcode demonstrating poor image quality with vertical lines and numbers 4902030187590
Blurred barcode with numbers 4902030187590 demonstrating poor image quality

ノイズ除去におけるMedianBlurフィルターの違いは何ですか?

MedianBlurFilter は、各ピクセルの値を周囲のピクセルの中央値で置き換えることによって、画像のノイズを低減します。 このフィルタは、ノイズを除去しながらエッジを保持することに特に優れています。BarCode 読み取り設定の詳細については、バーコードリーダー設定ガイドをご覧ください。

  • KernelSize: 中央値計算のための近傍値のサイズ(奇数でなければならない、デフォルト: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")
$vbLabelText   $csharpLabel

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

Blurry barcode example showing poor image quality with digital artifacts and reduced readability
Barcode with median blur filter applied showing blurred vertical lines and number 4902030187590

処理ステップごとにフィルタリングされた画像を保存するにはどうすればよいですか?

バーコードに複数のフィルタを適用する場合、各フィルタメソッド後の出力を表示することは困難です。 この機能は各フィルターが適用される順序でフィルターを適用した後、フィルターされた画像を保存します。 この機能を有効にするには、まずtrueImageFilterCollectionコンストラクタに渡します。 次に、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")
$vbLabelText   $csharpLabel

フィルターはコードの順序で適用され、出力画像は各反復の結果を反映します:

  • Sharpen -> Sharpen の後。
  • Sharpen + AdaptiveThreshold -> AdaptiveThreshold の後。
  • Sharpen + AdaptiveThreshold + Contrast -> Contrast の後。
Blurry barcode with number 4902030187590
Blurry UPC barcode showing number 4902030187590
Degraded barcode example showing poor image quality with number 9020301875905
Heavily pixelated barcode with UPC number 902030187590

ImageFiltersプロパティとは別に、より正確に読み取るために、BarcodeReaderOptionsに他のプロパティを追加してください; 詳細については、この記事を参照してください。

よくある質問

画像補正フィルタとは何ですか?また、なぜバーコードの読み取りに必要なのですか?

IronBarcodeの画像補正フィルターは、不鮮明または不完全なバーコード画像をプログラムで補正する組み込みツールです。画質の悪さはバーコードの読み取りを成功させない主な要因の一つであるため、必要不可欠なものです。IronBarcodeはSharpenFilterやContrastFilterなどのフィルターを提供し、外部の画像編集ソフトウェアや画像の再キャプチャを必要とせずに読み取り精度を向上させます。

バーコードスキャニングを向上させるために画像補正フィルタを適用する方法を教えてください。

IronBarcodeでフィルタを適用するには、ImageFilterCollectionインスタンスを作成し、そこに個々のフィルタインスタンスを追加します。次に、このコレクションをBarcodeReaderOptionsのImageFiltersプロパティに割り当て、Readメソッドに渡します。例えば: new BarcodeReaderOptions { ImageFilters = new ImageFilterCollection() { new SharpenFilter(3.5f), new ContrastFilter(2.0f) }.}.

不鮮明なバーコード画像にはどの画像フィルタが推奨されますか?

ぼやけたバーコード画像には、IronBarcodeは少なくともSharpenFilterとContrastFilterを使用することをお勧めします。SharpenFilterはぼやけた画像のエッジを強調し、ContrastFilterは明るい部分と暗い部分の区別を改善します。これらのフィルタを併用することで、外部で画像処理を行わなくてもバーコードの可読性を高めることができます。

画像補正フィルターの強さをカスタマイズできますか?

はい、IronBarcodeでは各フィルターにカスタム値を設定することができます。例えば、SharpenFilterはシャープネス強度を制御するためのfloatパラメータ(3.5fなど)を受け付け、ContrastFilterはコントラストレベルを調整するためのパラメータ(2.0fなど)を受け付けます。このようにカスタマイズすることで、さまざまな画像条件に対してフィルタの効果を最適化することができます。

バーコード画像を向上させるには、外部の画像編集ツールが必要ですか?

いいえ、IronBarcodeは組み込みの画像補正フィルターを提供することで、外部の画像編集ツールの必要性を排除します。SharpenFilterやContrastFilterのようなこれらのプログラムフィルタは、.NETアプリケーション内で直接画質を向上させることができ、時間を節約し、サードパーティのソフトウェアへの依存を避けることができます。

Hairil Hasyimi Bin Omar
ソフトウェアエンジニア
すべての優れたエンジニアのように、ハイリルは熱心な学習者です。彼はC#、Python、Javaの知識を磨き、その知識を活用してIron Softwareのチームメンバーに価値を追加しています。ハイリルはマレーシアのマラ工科大学からIron Softwareのチームに参加し、化学およびプロセス工学の学士号を取得しました。
準備はできましたか?
Nuget ダウンロード 2,002,059 | バージョン: 2025.12 リリース