使用 C# 圖像校正過濾器來改善條碼解碼
IronBarcode 提供內建的影像校正濾鏡,如 SharpenFilter 和 ContrastFilter,這些濾鏡可程式化地增強模糊或不完整的條碼圖像,無需外部影像編輯軟體或重新拍攝圖像即可改善讀取準確性。
不是每張圖像都是完美的,圖像品質差是妨礙 IronBarcode 成功讀取條碼的主要因素之一。 與其重新擷取影像或使用外部影像增強軟體,IronBarcode 提供內建濾鏡,可程式化地改善影像品質。 這些過濾器有助於 IronBarcode 讀取困難的圖像,並提高整體精確度。
繼續閱讀,了解 IronBarcode 中可用的影像校正濾鏡、它們對影像的影響以及如何應用它們。 有關更全面的條碼讀取技術,請查看我們的 閱讀條碼教程。
快速開始:應用銳化和對比過濾來改善條碼讀取只需一步,在 BarcodeReaderOptions 中使用 ImageFilterCollection 應用 IronBarcode 的 SharpenFilter 和 ContrastFilter。 這可改善 BarCode 掃描功能,且只需最少的設定,對外部工具的需求為零。
-
1使用 IronBarcode 安裝 NuGet Package Manager
-
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) } });Dim results As BarcodeResults = IronBarCode.BarcodeReader.Read("input.png", New IronBarCode.BarcodeReaderOptions With {.ImageFilters = New IronBarCode.ImageFilterCollection() {New IronBarCode.SharpenFilter(3.5F), New IronBarCode.ContrastFilter(2.0F)}})Dim results As BarcodeResults = IronBarCode.BarcodeReader.Read("input.png", New IronBarCode.BarcodeReaderOptions With {.ImageFilters = New IronBarCode.ImageFilterCollection() {New IronBarCode.SharpenFilter(3.5F), New IronBarCode.ContrastFilter(2.0F)}}) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronBarcode,並使用 免費試用
最小工作流程(5 個步驟)
- 下載C#程式庫以使用影像校正濾鏡
- 探索所有可用的圖像校正濾鏡
- 使用自訂值設定每個篩選器
- 對不完美的影像樣本套用濾鏡
- 從增強影像中擷取 BarCode 值
如何應用影像濾鏡以改善條碼讀取?
要應用濾鏡,請實例化 ImageFilterCollection 類別,並單獨創建每個濾鏡的實例。 然後將物件分配給 BarcodeReaderOptions 物件的 ImageFilters 屬性。 將選項物件連同範例圖像傳遞給 Read 方法。 有關高級安裝選項,請訪問我們的 NuGet 套件指南。
使用以下圖片作為我們的範例圖片。

範例影像
圖片看起來相當模糊。 不過,亮度尚可接受,白色和黑色也能區分。 因此,至少應用 SharpenFilter 和 ContrastFilter 以改善條碼的可讀性。 請參考以下的程式碼片段,將濾鏡套用至影像、讀取影像,並將結果顯示在控制台。
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);
}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 resultImports 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上面的程式碼片段會套用篩選器、讀取條碼,並將篩選過的影像匯出到磁碟。 樣本與篩選圖片的比較如下所示。

範例影像

過濾後的樣品
IronBarcode 提供哪些影像校正濾鏡?
IronBarcode 提供多個專為影像校正設計的 影像濾鏡。 這些篩選器可協助讀取不完美的 BarCode 影像,並提高讀取精確度。 然而,了解這些篩選器的運作方式,才能選擇合適的篩選器,並避免因使用太多篩選器或使用錯誤的篩選器而造成效能問題。 可用的篩選條件包括
AdaptiveThresholdFilterBinaryThresholdFilterBrightnessFilterContrastFilterInvertFilterSharpenFilterErodeFilterDilateFilterHistogramEqualizationFilter- 模糊濾鏡
GaussianBlurFilterBilateralFilterMedianBlurFilter
濾鏡的應用順序基於它們在 ImageFilterCollection 中的位置。 有關這些濾鏡的詳細API文件,請訪問我們的 API 參考。
自適應閾值濾鏡如何工作?
AdaptiveThresholdFilter 是 IronBarcode 中提供的一種濾鏡,應用 Bradley 自適應閾值 技術到影像,自動確定二值化圖像的閾值。 此濾鏡非常適合用於光照不均勻、背景強度變化較大的影像。
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");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")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")以下是使用不同值套用篩選器後的輸出結果。

預設值

0.9 值
建構器會接受額外的設定參數:
Upper:用於閾值的上限(白色)顏色。Lower:用於閾值的下限(黑色)顏色。Threshold:二值化的閾值限制(0.0-1.0)。Rectangle:應用處理器的矩形區域。
如上面的輸出影像所示,影像經過二值化處理,只有 ** 黑** 和 ** 白** 兩種顏色。 雖然對於 BarCode 讀取來說似乎仍不理想,但濾鏡需要組合使用。 請嘗試使用參數靈敏度以達到最佳效果。
二值閾值濾鏡如何工作?
BinaryThresholdFilter 通過在給定的閾值點分割像素,並比較顏色成分的亮度來濾過圖像。 與 AdaptiveThresholdFilter 類似,如果使用不當,此濾鏡可能會引入新的或不需要的噪聲。 然而,IronBarcode 已為篩選器屬性設定預設值。
與 AdaptiveThresholdFilter 類似,BinaryThresholdFilter 接受相同的附加參數以進行配置:
Upper:用於閾值的上限(白色)顏色。Lower:用於閾值的下限(黑色)顏色。Threshold:二值化的閾值限制(0.0-1.0)。Rectangle:應用處理器的矩形區域。
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");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")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")以下是對範例影像套用濾鏡後的範例輸出結果。

預設值

0.9 值
觀察上面的輸出影像,樣本已二值化為黑白顏色。 然而,由於條碼條被消除,並引進新的雜訊,此濾鏡顯然不適合此影像。 要處理困難的條碼情形,請參閱我們的 未識別條碼故障排除指南。
如何調整圖像亮度以提高條碼讀取效果?
BrightnessFilter 是 IronBarcode 影像濾鏡集合中的另一個重要過濾器。 顧名思義,此濾鏡可調整條碼影像的亮度。 此構成器的輸入會改變輸出影像中的 Amount 亮度。 預設值為 1,這表示影像保持不變。 值為 0 會產生完全黑色的影像,而值高於 1 則會使影像更亮。
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");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")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")下面這張圖是將此濾鏡套用到範例輸入後的輸出影像。

預設值

1.5 值
如何使用對比度濾鏡增強條碼圖像?
ContrastFilter 調整圖像的對比度等級。 影像對比度是指影像中不同元素之間顏色強度的差異。 提高對比度可以增強細節的可見性,使影像看起來生動鮮明;而降低對比度則會使影像看起來柔和低調。 有關條碼自定義的更多詳細信息,請參閱我們的 自定義條碼樣式指南。
預設值為 1,這表示影像保持不變。 值為 0 會產生完全灰色的影像,而值高於 1 則會增加影像的對比度。
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");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")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")將此濾鏡套用至範例輸入,即可產生下圖。

預設值

1.5 值
什麼時候應該使用反轉濾鏡?
此濾鏡可將影像內部的顏色反轉,變成相反的顏色,例如白色變成黑色,黑色變成白色。在讀取有背景顏色的 BarCode 影像時特別有用。 與 BinaryThresholdFilter 不同,此濾鏡直接反轉顏色,而不需要指定敏感性。 此外,此濾鏡可與 CropRectangle 搭配使用,以指定影像中需要反轉顏色的位置,而不是反轉整個影像的顏色。 了解更多有關指定裁剪區域的信息,請參閱我們的 裁剪區域教程。
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");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")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")下面的輸出影像是將此濾鏡套用至範例輸入影像的結果。

原圖

倒
如何使用銳化濾鏡修正模糊的條碼圖像?
IronBarcode 提供銳化篩選器。 此濾鏡可以增強影像的清晰度,在處理模糊影像時非常有用。 在實體化濾鏡物件時,透過調整 Sigma 值來操控此濾鏡,以調整影像的銳利度。 預設值為 3。增加 sigma 值可以提高影像清晰度。 有關其他性能優化選項,請查看我們的 讀取速度選項指南。
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");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")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")下面的圖片是範例輸入圖片的 sharpened 版本。

預設值

0.5 值
將上面的圖片與原始圖片比較,它顯得更清晰,並有助於使用 IronBarcode 讀取條碼。 在大多數情況下,SharpenFilter 總是與 ImageFilterCollection 類別中的其他濾鏡一起應用。
腐蝕濾鏡有什麼用途?
ErodeFilter 通過移除形狀邊界附近的像素來去除微小的白色噪聲並使條碼更粗。此濾鏡最好在背景有許多白色斑點或條碼圖像過低解析度或模糊而導致條碼條形融合的場景中使用。 ErodeFilter 使條形更粗且移除背景中的白色斑點。 有關處理不完美圖像的更多信息,請參閱我們的 不完美條碼示例。
通過輸入代表濾鏡的整數 kernelSize 增強腐蝕效果。 卷積核尺寸越大,對輸入影像的影響就越強。 請注意,kernelSize 是一個正方形,在此範例中將是一個 5x5 核心。
舉例來說,使用具有更大核心尺寸的 ErodeFilter 來展示濾鏡效果。
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");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");Imports IronBarCode
Dim options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {
New ErodeFilter(5)
}
}
' Apply options and read the barcode
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Export file to disk
results.ExportFilterImagesToDisk("erodeFilter.jpg")Imports IronBarCode
Dim options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {
New ErodeFilter(5)
}
}
' Apply options and read the barcode
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Export file to disk
results.ExportFilterImagesToDisk("erodeFilter.jpg")
原圖

侵蝕過濾器應用
比較上面的輸入和輸出影像,由於輸入較大的核心尺寸進行篩選時較為激進,因此有些條形會明顯變厚。 然而,整體畫面中的白色斑點減少了。 根據侵蝕濾鏡的性質,內核尺寸越大,如果過於激烈地應用,可能會擦除細條,如上圖所示。 通過更改輸入到 ErodeFilter 的核心尺寸值,測試並完善效果。
擴張濾鏡如何有助於條碼讀取?
DilateFilter 功能類似於 ErodeFilter 的反向操作,通過將像素添加到物體邊緣來擴展亮區域,通常是背景。 雖然此濾鏡可藉由填補細小縫隙或增強低對比度區域來修復損壞或模糊的 BarCode,但請注意其對 BarCode 條碼的效果與直覺不同。 由於擴張會放大明亮的空間,因此會間接變薄暗色元素,例如黑色 BarCode 條碼(假設背景為白色)。 這使得該過濾器在條碼條看起來過粗或合併的情況下特別有效,但過度使用可能會因條碼條過窄而降低掃描精度。
與上述類似,通過輸入代表濾鏡的整數 kernelSize 來增強濾鏡效果。
在下面的範例中,使用較大核心尺寸來展示 DilateFilter 的效果。
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");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");Imports IronBarCode
Dim options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {
New DilateFilter(5)
}
}
' Apply options and read the barcode
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Export file to disk
results.ExportFilterImagesToDisk("dilateFilter.jpg")Imports IronBarCode
Dim options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {
New DilateFilter(5)
}
}
' Apply options and read the barcode
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Export file to disk
results.ExportFilterImagesToDisk("dilateFilter.jpg")
原圖

應用擴張濾波器
如上圖所示,過度使用 DilateFilter 可能會破壞條碼結構,融合緊密排列的條形並在條碼中創建安靜區域。 根據輸入的影像,將核心大小值變得更大或更小,以測試並改善對影像的影響。
什麼時候應該使用直方圖均衡化濾鏡?
HistogramEqualizationFilter 通過重新分配像素強度來增強圖像對比度,從而改善清晰度。 它最常用於條碼對比度較低的情況,例如褪色或褪色的影像,或是光線不均勻的影像,例如暗影或強光。 透過分析影像直方圖(像素亮度分佈),它透過拉伸強度範圍來增強對比度,從而重新分配像素值,使暗像素變得更暗,亮像素變得更亮。
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");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");Imports IronBarCode
Dim options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {
New HistogramEqualizationFilter()
}
}
' Apply options and read the barcode
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Export file to disk
results.ExportFilterImagesToDisk("histogramEqualizationFilter.jpg")Imports IronBarCode
Dim options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection(True) From {
New HistogramEqualizationFilter()
}
}
' Apply options and read the barcode
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)
' Export file to disk
results.ExportFilterImagesToDisk("histogramEqualizationFilter.jpg")
原圖

已應用直方圖均衡化濾波器
如上圖所示,與原始圖片相比,黑條明顯變暗,空格明顯變亮。
哪些模糊濾鏡可以幫助減少條碼噪聲?
高斯模糊濾鏡如何減少圖像噪聲?
GaussianBlurFilter 對圖像應用高斯模糊。 此濾鏡通常可降低影像中的雜訊。 有關處理不完美條碼的指南,請參閱我們的 影像方向校正教程。
此濾波器的工作原理是使用高斯函數對影像中相鄰像素值進行平均。 此方法依賴兩個可調因素:
- 核心: 用於平均像素的矩陣。
- 西格瑪: 控制模糊強度的值。
預設 kernel 大小為 3x3 像素,預設 Sigma 值為 3.0,產生適度的模糊。 增加 Sigma 值會帶來更強的模糊效果。 您還可以自定義 kernel 以控制模糊濾鏡平均的鄰域大小。
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");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")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")將此濾鏡套用至範例輸入,即可產生下圖。

銳化影像

高斯模糊影像
什麼時候應該使用雙邊濾鏡?
BilateralFilter 在保留邊緣的同時平滑圖像。 不同於簡單的模糊技術會均勻地影響所有像素,Bilateral Filter 會同時考慮顏色差異和像素距離,使其能有效地進行邊緣保留平滑處理。
此方法依賴三個可調因素:
NeighborhoodDiameter:像素鄰域直徑(默認:5)。SigmaColor:顏色影響,決定顏色差異所帶來的影響(默認:75.0)。SigmaSpace:空間影響,決定距離對影響(默認:75.0)。
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");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")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")將此濾鏡套用至範例輸入,即可產生下圖。

銳化影像

雙側影像
中值模糊濾鏡在噪聲降低方面有何不同?
MedianBlurFilter 通過用周圍像素的中值代替每個像素的值來降低圖像的噪聲。 此濾鏡特別擅長在去除噪聲的同時保留邊緣。要探索更多條碼讀取設置,請訪問我們的 條碼讀取器設置指南。
KernelSize:用於中值計算的鄰域大小(必須為奇數,默認:5)。
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");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")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")將此濾鏡套用至範例輸入,即可產生下圖。

銳化影像

中位數模糊影像
如何在每個處理步驟中保存已過濾的影像?
在對 BarCode 應用多個篩選器時,檢視每個篩選方法之後的輸出可能會有困難。 此功能允許在套用每個濾鏡後,按處理順序儲存濾鏡後的影像。 要啟用此功能,首先將 true 傳遞給 ImageFilterCollection 構造函數。 然後使用 ExportFilterImagesToDisk 方法提供輸出影像的路徑和名稱。 有關保存條碼的更多範例,請參閱我們的 條碼轉換為影像範例。
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");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")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")過濾器按照程式碼的順序應用,輸出圖像反映了每次迭代的結果:
Sharpen-> AfterSharpenSharpen+AdaptiveThreshold-> AfterAdaptiveThresholdSharpen+AdaptiveThreshold+Contrast-> AfterContrast

範例影像

磨利後

自適應閾值後

對比後
除了 ImageFilters 屬性外,還可以將其他屬性添加到 BarcodeReaderOptions 以提高讀取準確性; 有關更多資訊,請參閱這篇 文章。
常見問題
IronBarcode中有哪些影像校正濾鏡可用?
IronBarcode提供多種影像校正濾鏡,例如SharpenFilter、ContrastFilter、AdaptiveThresholdFilter、BinaryThresholdFilter、BrightnessFilter、InvertFilter、ErodeFilter、DilateFilter、HistogramEqualizationFilter,以及幾種模糊濾鏡,包括GaussianBlurFilter、BilateralFilter和MedianBlurFilter。這些濾鏡透過改善影像品質提高條碼閱讀的準確性。
如何在IronBarcode中應用Sharpen和Contrast濾鏡?
要在IronBarcode中應用Sharpen和Contrast濾鏡,您需要用這些濾鏡例項化ImageFilterCollection類,並將其指定給BarcodeReaderOptions物件的ImageFilters屬性。將此選項物件和圖片一起傳遞給Read方法,以增強條碼可讀性。
Adaptive Threshold Filter的作用是什麼?
Adaptive Threshold Filter使用Bradley Adaptive Thresholding技術,自動確定影像二值化的閾值,對於光照不均且背景亮度變化的影像特別有用。
Invert Filter如何工作?
Invert Filter反轉影像的顏色,將白色變為黑色,反之亦然,這對於背景顏色的條碼影像非常有幫助。它可以應用於整個影像或使用CropRectangle指定的區域。
Erode Filter對條碼影像有什麼效果?
Erode Filter通過去除邊緣附近的像素來加粗條碼條紋,從而減少背景雜訊。它對於低分辨率或模糊的影像有幫助,但需要仔細調整以避免擦掉細條紋。
什麼時候應該使用HistogramEqualization Filter?
HistogramEqualization Filter透過重新分配像素強度來增強對比,使其適合低對比或光照不均的條碼。此濾鏡拉伸強度範圍以提高可讀性。
GaussianBlur Filter的作用是什麼?
GaussianBlur Filter通過使用高斯函數平平均像素值來降低影像噪聲。它可以透過內核大小和sigma值進行配置,並應用中度模糊以幫助降低影像噪聲。
Bilateral Filter與其他模糊濾鏡有何不同?
Bilateral Filter在保護邊緣的同時降低噪聲,考慮顏色差異和像素距離。它透過考慮鄰域直徑、sigma色彩和sigma空間參數來平滑影像。
MedianBlur Filter的主要用途是什麼?
MedianBlur Filter透過將每個像素的值替換為其鄰居的中位數來在保持邊緣的同時最小化噪聲,使其在噪聲減少和邊緣保護方面效果良好。
如何在IronBarcode中應用每個濾鏡後保存影像?
在IronBarcode中,通過向ImageFilterCollection建構函式傳遞true來啟用每個濾鏡步驟之後的影像導出,然後使用ExportFilterImagesToDisk方法保存每個處理步驟的輸出影像。