如何在C#中使用圖像校正濾波器以改善條碼

How to use Image Correction Filters

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 IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    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. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步)

  1. 下載 C# 程式庫以使用影像校正濾鏡
  2. 探索所有可用的影像校正濾鏡
  3. 使用自定義值配置每個影像校正濾鏡
  4. 將濾鏡應用於不完美的影像範例
  5. 在濾鏡的幫助下從圖像中提取條碼值

使用影像濾鏡改善讀取範例

要應用濾鏡,先實例化 ImageFilterCollection 類,並單獨創建每個濾鏡的實例。 然後將對象分配給BarcodeReaderOptions對象的 ImageFilters 屬性。 將選項對象與範例圖像一起傳遞到Read方法。

讓我們以下面的圖像作為範例圖像。

class="content-img-align-center">
class="center-image-wrapper"> Sample image

從圖像的初步觀察來看,它看起來相當模糊。 然而,亮度是可接受的,黑白顏色是可區分的。 因此,我們需要至少應用 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

從上面的程式碼片段可以看出,除了應用濾鏡和讀取條碼外,我們還將濾鏡應用過的圖像導出到磁碟。 下面可以看到範例與經過濾鏡處理的圖像之間的比較。

class="competitors-section__wrapper-even-1">
Sample image
Filtered sample

探索影像校正濾鏡

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

以下是使用不同值應用濾鏡的輸出。

class="competitors-section__wrapper-even-1">
Default Adaptive Threshold
0.9 Adaptive Threshold

構造函數還可以接受其他配置參數:

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

如上面輸出圖像所示,圖像已被二值化為只有黑色白色顏色。 儘管它似乎仍不適合條碼讀取,因為需要使用組合的濾鏡。 用戶需要通過參數敏感度來實驗以達到最佳效果。

二值閾值濾鏡

BinaryThresholdFilter 濾鏡會在給定的閾值處分割圖像中的像素,以此來比較色彩分量的亮度。 類似於自適應閾值濾鏡,如果使用不正確,此濾鏡可能會引入新的或不需要的噪聲。 然而,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

以下是應用濾鏡到範例圖像的輸出樣本。

class="competitors-section__wrapper-even-1">
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

以下是將此濾鏡應用於範例輸入後的輸出圖像。

class="competitors-section__wrapper-even-1">
Default Brightness
1.5 Brightness

對比濾鏡

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

將此濾鏡應用於範例輸入將生成下面的圖像。

class="competitors-section__wrapper-even-1">
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

下面的輸出圖像是將此濾鏡應用於範例輸入圖像的結果。

class="competitors-section__wrapper-even-1">
Original image
Inverted

銳化濾鏡

我們還在 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.

class="competitors-section__wrapper-even-1">
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
class="competitors-section__wrapper-even-1">
Original image
Inverted

比較上面的輸入和輸出圖像,我們可以看到,由於為過濾器輸入了更大的內核尺寸,一些條碼的條明顯更厚。 然而,整體畫面中的白色斑點減少了。 由於侵蝕濾鏡的作用,内核大小越大,在過於激烈地應用時,您可能會遇到刪除細條的問題,如上圖所示,因此開發者應通過更改他們輸入到 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
class="competitors-section__wrapper-even-1">
Original image
Inverted

從上面的圖像可以看出,過於激烈使用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
class="competitors-section__wrapper-even-1">
Original image
Inverted

從上面的圖像可以看出,黑色條形顯著變暗,而空間顯著變亮,與原始圖像相比。

模糊濾鏡

高斯模糊濾鏡

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

將此濾鏡應用於範例輸入將生成下面的圖像。

class="competitors-section__wrapper-even-1">
Default Sharpen
GaussianBlur image

雙邊濾鏡

BilateralFilter 用於在平滑圖像的同時保留邊緣。 與均勻影響所有像素的簡單模糊技術不同,雙邊濾鏡考慮顏色差異和像素距離,使其在保護邊緣的平滑方面非常有效。

該方法依賴於三個可調因素:

  • 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

將此濾鏡應用於範例輸入會生成以下圖像。

class="competitors-section__wrapper-even-1">
Default Sharpen
Bilateral image

中值模糊濾鏡

MedianBlurFilter 是一種濾鏡,用於通過用周圍像素的中值值替換每個像素的值來減少圖像中的噪音。 此濾鏡特別在去噪時有效。

  • KernelSize:定義每個像素周圍的鄰域大小以計算中值。 The value must be an odd number greater than 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

將此濾鏡應用於範例輸入會生成以下圖像。

class="competitors-section__wrapper-even-1">
Default Sharpen
MedianBlur image

保存迭代

當將多個濾鏡應用於條碼時,可能很難在每個濾鏡方法後查看輸出。 這個功能允許在每個濾鏡應用後按處理順序保存濾鏡後的圖像。 要啟用這個功能,首先將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")
$vbLabelText   $csharpLabel

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

  • 銳化 -> 銳化後
  • 銳化 + 自適應閾值 -> 自適應閾值後
  • 銳化 + 自適應閾值 + 對比 -> 對比後
Sample image
1.5 Contrast
class="competitors-section__wrapper-even-1">
Sample image
1.5 Contrast

除了ImageFilters屬性,用戶還可以為BarcodeReaderOptions添加其他屬性以提高讀取準確性; 請參閱此文章以獲取更多信息。

常見問題解答

如何在 .NET C# 中改善條碼可讀性?

您可以通過使用 IronBarcode 應用圖像校正濾鏡來提高 .NET C# 中的條碼可讀性。這些濾鏡,如 AdaptiveThresholdFilter 和 BrightnessFilter,可以解決圖像不完善問題,並提高條碼掃描的準確性。

如何以程式化方式應用圖像校正濾鏡?

要以程式化方式使用 IronBarcode 應用圖像校正濾鏡,您需要下載函式庫,實例化 ImageFilterCollection 類,配置所需的濾鏡,並在處理條碼圖像之前通過 BarcodeReaderOptions 應用它們。

在 IronBarcode 中有哪些濾鏡可用於改善圖像質量?

IronBarcode 提供多種濾鏡以改善圖像質量,包括 AdaptiveThresholdFilterBinaryThresholdFilterBrightnessFilterContrastFilterInvertFilterSharpenFilter,以及幾種模糊濾鏡,如 GaussianBlurFilterBilateralFilter

如何在 C# 中配置自適應閾值濾鏡?

在 IronBarcode 中,可以通過 Bradley 自適應閾值技術配置 AdaptiveThresholdFilter。該濾鏡會自動確定二值化圖像的閾值,特別適合光照不均的情況。

是否可以在每個濾波步驟中保存圖像?

是的,IronBarcode 允許您通過啟用 ImageFilterCollection 中的迭代保存,並使用 ExportFilterImagesToDisk 方法來保存每個濾波步驟的圖像。

在應用多個濾鏡時應考慮哪些因素?

在 IronBarcode 中應用多個濾鏡時,重要的是要避免使用過多或不當的濾鏡,因為這可能引入噪聲或影響性能。了解每個濾鏡的功能可幫助僅應用必要的濾鏡以獲得最佳效果。

銳化濾鏡如何影響圖像?

IronBarcode 中的 SharpenFilter 通過調整銳度來增強圖像清晰度。可用 Sigma 值進行配置,對改善條碼圖像邊緣清晰度非常有用。

反轉濾鏡在條碼處理中的作用是什麼?

IronBarcode 中的 InvertFilter 會反轉圖像的顏色,將白色變為黑色,黑色變為白色。這對於具有非標准顏色方案或背景的條碼特別有用。

高斯和雙邊模糊濾鏡如何改善圖像處理?

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

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