如何使用 C# 為 BarCode 使用影像校正篩選器

使用 C# 影像校正濾鏡來改進條碼解碼

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

讓我們面對現實吧。 並非每張圖片都是完美的,這也是IronBarcode無法讀取條碼圖片的主要原因之一。 這並非完全是用戶的錯。 IronBarcode 提供了一項功能,使用戶能夠以程式設計方式對影像套用濾鏡,而無需重新拍攝影像或使用其他影像增強軟體。 這將有助於IronBarcode讀取影像並提高準確率。

繼續閱讀,了解 IronBarcode 中可用的影像校正濾鏡、它們對影像的影響以及如何應用它們。

快速入門:應用銳利化和對比濾鏡來改善條碼讀取

只需一步,即可使用 BarcodeReaderOptions 中的 ImageFilterCollection 應用 IronBarcode 的 SharpenFilter 和 ContrastFilter。 這樣一來,只需極少的設置,無需任何外部工具,即可獲得更佳的條碼掃描效果。

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方法。

讓我們使用下圖作為範例圖片。

範例影像

從圖像的第一印象來看,它似乎很模糊。 不過,亮度尚可接受,白色和黑色也能區分。 因此,我們需要至少應用銳利化濾鏡對比濾鏡來提高條碼的可讀性。 請參考以下程式碼片段,將濾鏡套用到影像,讀取影像,並在控制台上顯示影像。

: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

從上面的程式碼片段可以看出,除了應用濾鏡和讀取條碼之外,我們還將過濾後的影像匯出到磁碟。 下面可以看到樣本影像和濾波後影像的對比。

範例影像
過濾後的樣品

探索影像校正濾鏡

IronBarcode 提供多種專為影像校正而設計的影像濾鏡。 這些濾鏡可以幫助讀取不完美的條碼影像,提高讀取準確率。 但是,使用者需要了解這些過濾器的工作原理,以便選擇合適的過濾器避免因使用過多過濾器或使用錯誤的過濾器而導致效能問題。 以下是所有可用的篩選條件:

  • 自適應閾值濾波器
  • 二元閾值濾波器
  • 亮度濾鏡
  • 對比濾鏡
  • 反轉濾波器
  • 銳利化濾鏡
  • 侵蝕過濾器
  • 擴張過濾器
  • 直方圖均衡化濾波器
  • 模糊濾鏡
    • 高斯模糊濾波器
    • 雙邊濾波器
    • 中值模糊濾波器

這些濾鏡的應用順序取決於它們在ImageFilterCollection中的位置。

自適應閾值濾波器

AdaptiveThresholdFilter是 IronBarcode 中可用的濾波器之一,它將Bradley 自適應閾值技術應用於影像,自動確定影像二值化的閾值。 此濾鏡非常適合用於光照不均勻、背景強度變化較大的影像。

: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

以下是使用不同值套用篩選器後的輸出結果。

Default Adaptive Threshold
0.9 Adaptive Threshold

建構函式也接受用於配置的附加參數:

  • 上部:閾值處理的上部(白色)顏色。
  • 下限:閾值處理的下限(黑色)顏色。
  • 閾值:二值化的閾值限制(0.0-1.0)。
  • 矩形:要套用處理器的矩形區域。

如上圖所示,影像被二值化,只保留了黑白**兩**色。 雖然它似乎仍然不是條碼讀取的理想選擇,因為需要組合使用過濾器。 使用者需要透過試驗來調整參數靈敏度,以獲得最佳結果。

二元閾值濾波器

二值閾值濾波器透過在給定閾值處分割像素來過濾影像,它用於比較顏色分量的亮度。 與自適應閾值濾波器類似,如果使用不當,此濾波器可能會引入新的或不必要的雜訊。 但是,IronBarcode 為篩選器的屬性設定了預設值。

與自適應閾值濾波器類似,二元閾值濾波器也接受相同的附加配置參數:

  • 上部:閾值處理的上部(白色)顏色。
  • 下限:閾值處理的下限(黑色)顏色。
  • 閾值:二值化的閾值限制(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

以下是對範例影像套用濾鏡後的範例輸出結果。

Default Binary Threshold
0.9 Binary Threshold

觀察上面的輸出影像,我們可以看到樣本已被二值化為黑白顏色。 但是可以看出,由於條碼條紋被消除,並且引入了一些新的噪聲,因此該濾鏡顯然不適合此圖像。

亮度濾鏡

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

下面這張圖是將此濾鏡套用到範例輸入後的輸出影像。

Default Brightness
1.5 Brightness

對比濾鏡

對比度濾鏡用於調整影像的對比度等級。 影像對比度是指影像中不同元素之間顏色強度的差異。 提高對比度可以增強細節的可見性,使影像看起來生動鮮明;而降低對比度則會使影像看起來柔和低調。

預設值為 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

將此濾鏡應用於範例輸入將產生下圖。

Default Contrast
1.5 Contrast

反濾波器

此濾鏡用於反轉影像中的顏色,使相反的顏色(例如白色變為黑色,黑色變為白色)互換。當使用者嘗試讀取帶有背景色的條碼影像時,此濾鏡尤其有用。 與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

下面的輸出影像是將此濾鏡套用至範例輸入影像的結果。

原圖
倒

銳利化濾鏡

IronBarcode 也提供了一個銳化濾鏡。 此濾鏡可以增強影像的清晰度,在處理模糊影像時非常有用。 使用者可以透過在實例化濾鏡物件時調整Sigma值來操控此濾鏡,從而調整影像的清晰度。 預設值為 3。增加 sigma 值可以提高影像清晰度。

: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

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

Default Sharpen
0.5 Sharpen

將上圖與原始圖進行比較,可以明顯看出影像更清晰,這肯定有助於使用 IronBarcode 來讀取條碼。 大多數情況下, SharpenFilter總是與ImageFilterCollection類別中的其他濾鏡一起應用。

侵蝕過濾器

ErodeFilter 濾鏡可透過去除條碼邊緣附近的像素來消除微小的白雜訊並加粗條碼條紋。此濾鏡最適用於條碼背景存在大量白色斑點,或條碼影像本身解析度過低或模糊不清,導致部分條紋重疊的情況。 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
原圖
倒

對比上面的輸入和輸出影像,我們可以看到,由於輸入更大的內核尺寸進行濾波的激進性,一些條形明顯更粗。 然而,整體畫面中的白色斑點減少了。 由於腐蝕濾波器的特性,內核尺寸越大,如果應用得過於激進,可能會遇到擦除細條的問題,如上圖所示。因此,開發人員會透過更改輸入到ErodeFilter 的內核尺寸值來測試和改進他們想要的效果。

擴張過濾器

DilateFilter 的功能與ErodeFilter相反,它透過在物件邊界添加像素來擴展明亮區域(通常是背景)。 雖然此濾鏡可用於修復損壞或模糊的條碼,透過填充小間隙或增強低對比度區域來實現,但需要注意的是,它對條碼條的影響與直覺不同。 由於膨脹效應會放大明亮的區域,因此它會間接地使黑色條碼條紋等暗部變薄(假設背景是白色)。 這使得該過濾器在條碼條看起來過粗或合併的情況下特別有效,但過度使用可能會因條碼條過窄而降低掃描精度。

與上述類似,使用者可以透過輸入表示濾波器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
原圖
倒

從上圖可以看出,過度使用DilateFilter可能會破壞條碼結構,合併緊密排列的條形,並在條碼中創建空白區域。 使用者應根據輸入影像,透過增大或減小核心大小值來測試和改進他們想要對影像產生的效果。

直方圖均衡化濾波器

直方圖均衡化濾波器是一種透過重新分配像素強度來增強影像對比度以提高清晰度的濾波器。 當條碼對比度較低時,例如褪色或發白的影像,或光線不均勻的影像,例如陰影過深或眩光過強時,最常使用此方法。 透過分析影像直方圖(像素亮度分佈),它透過拉伸強度範圍來增強對比度,從而重新分配像素值,使暗像素變得更暗,亮像素變得更亮。

: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
原圖
倒

從上圖可以看出,與原圖相比,黑色條紋明顯變暗,空白區域明顯變亮。

模糊濾鏡

高斯模糊濾波器

GaussianBlurFilter用於對影像應用高斯模糊。 這種濾鏡通常用於減少影像中的雜訊。

此濾波器的工作原理是使用高斯函數對影像中相鄰像素值進行平均。 此方法依賴兩個可調因素: -:用於對像素進行平均的矩陣。

  • Sigma :控制模糊強度的值。

預設內核大小為 3x3 像素,預設 Sigma 值為 3.0,可產生適度的模糊效果。 增加 Sigma 值會產生更強的模糊效果。 您也可以自訂核心來控制模糊過濾器平均的鄰域大小。

: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

將此濾鏡應用於範例輸入將產生下圖。

Default Sharpen
高斯模糊影像

雙邊濾波器

雙邊濾波器用於平滑影像,同時保留邊緣。 與對所有像素均勻影響的簡單模糊技術不同,雙邊濾波器同時考慮顏色差異和像素距離,因此可以有效地保持邊緣平滑。

此方法依賴三個可調因素:

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

將此濾鏡套用至範例輸入,即可產生下圖。

Default Sharpen
雙側影像

中值模糊濾波器

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

將此濾鏡套用至範例輸入,即可產生下圖。

Default Sharpen
中位數模糊影像

儲存迭代次數

對條碼套用多個濾鏡時,每次套用濾鏡後都很難查看輸出結果。 此功能允許在套用每個濾鏡後,按處理順序儲存濾鏡後的影像。 若要啟用此功能,請先將true傳遞給ImageFilterCollection建構子。 然後,使用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

過濾器按照程式碼的順序應用,輸出圖像反映了每次迭代的結果:

  • 銳利化 -> 銳化後
  • 銳利化 + 自適應閾值 -> 自適應閾值後
  • 銳利化 + 自適應閾值 + 對比 -> 對比調整後
範例影像
1.5 Contrast
範例影像
1.5 Contrast

除了ImageFilters屬性外,使用者還可以為BarcodeReaderOptions新增其他屬性,以實現更準確的讀取; 更多資訊請參閱這篇文章

常見問題解答

如何在 .NET C# 中提高 BarCode 的可讀性?

您可以使用 IronBarcode for .NET 應用影像修正篩選器來增強 .NET C# 中條碼的可讀性。這些濾鏡,例如 AdaptiveThresholdFilter 和 BrightnessFilter,可以處理影像瑕疵,並提高條碼掃描的精確度。

以程式化方式套用影像修正濾鏡的步驟為何?

要使用 IronBarcode 程式化地套用影像修正篩選器,您需要下載該函式庫,實體化 ImageFilterCollection class,配置您所需的篩選器,並在處理條碼影像之前,透過 BarcodeReaderOptions 套用這些篩選器。

在 IronBarcode 中,哪些濾鏡可用來增強影像品質?

IronBarcode 提供多種濾鏡來增強影像品質,包括 AdaptiveThresholdFilter, BinaryThresholdFilter, BrightnessFilterContrastFilter, InvertFilter, SharpenFilter, 和幾個模糊濾鏡如 GaussianBlurFilterBilateralFilter.

如何在 C# 中設定 Adaptive Threshold Filter?

在 IronBarcode 中,AdaptiveThresholdFilter 可以使用 Bradley Adaptive Threshold 技術進行配置。此濾鏡可自動決定影像二值化的閾值,對於光線不均勻的情況特別有用。

是否可以在篩選的每個步驟中儲存影像?

是的,IronBarcode 允許您在篩選的每一步驟儲存圖片,方法是在 ImageFilterCollection 中啟用迭代儲存,並使用 ExportFilterImagesToDisk 方法。

套用多重篩選條件時應注意哪些事項?

在 IronBarcode 中應用多個篩選器時,必須避免使用過多或不適當的篩選器,因為這可能會帶來雜訊或影響效能。瞭解每個篩選器的功能有助於只使用必要的篩選器,以獲得最佳的結果。

銳化濾鏡如何影響影像?

IronBarcode 中的 SharpenFilter 可透過調整銳利度來增強影像的清晰度。它可以使用 Sigma 值進行配置,對於改善 BarCode 圖像中的邊緣清晰度非常有用。

Invert Filter 在 BarCode 處理中扮演什麼角色?

IronBarcode 中的 InvertFilter 可以反轉圖片的顏色,使白色變成黑色,黑色變成白色。這對於具有非標準配色方案或背景的 BarCode 特別有用。

高斯濾波器和雙邊模糊濾波器如何改善影像處理?

在 IronBarcode 中,GaussianBlurFilter 可透過應用高斯模糊來減少影像雜訊,而 BilateralFilter 則可在保留邊緣的同時平滑影像,並同時考慮顏色差異和像素距離。

Hairil Hasyimi Bin Omar
軟體工程師
和所有优秀的工程师一样,Hairil 是个努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识应用于 Iron Software 各个团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 1,979,979 | Version: 2025.11 剛發表