使用 C# 影像校正濾鏡來改進條碼解碼
IronBarcode提供內建的影像校正濾鏡,如 SharpenFilter 和 ContrastFilter,可透過程式增強模糊或不完美的條碼影像,提高讀取精度,而無需外部影像編輯軟體或重新擷取影像。
並非每張圖片都是完美的,圖片品質不佳是IronBarcode無法成功讀取條碼的主要因素之一。 IronBarcode不採用重新擷取影像或使用外部影像增強軟體的方式,而是提供內建濾鏡,以程式方式提高影像品質。 這些濾鏡可以幫助IronBarcode讀取難以辨識的影像,並提高整體準確率。
繼續閱讀,了解IronBarcode中可用的影像校正濾鏡、它們對影像的影響以及如何應用它們。 如需更全面的條碼讀取技巧,請查看我們的條碼讀取教學。
快速入門:應用銳利化和對比濾鏡來改善條碼讀取效果
只需一步,即可套用 IronBarcode 的 SharpenFilter 和 ContrastFilter,使用 ImageFilterCollection 在 BarcodeReaderOptions 中。 這使得條碼掃描更加便捷,設定極少,無需任何外部工具。
-
使用NuGet套件管理器安裝https://www.nuget.org/packages/BarCode
PM > Install-Package BarCode -
複製並運行這段程式碼。
BarcodeResults results = IronBarCode.BarcodeReader.Read("input.png", new IronBarCode.BarcodeReaderOptions { ImageFilters = new IronBarCode.ImageFilterCollection() { new IronBarCode.SharpenFilter(3.5f), new IronBarCode.ContrastFilter(2.0f) } }); -
部署到您的生產環境進行測試
今天就在您的專案中開始使用免費試用IronBarcode
最簡工作流程(5個步驟)
- 下載 C# 庫以使用影像校正濾鏡
- 探索所有可用的影像校正濾鏡
- 使用自訂值配置每個過濾器
- 對不完美的影像樣本應用濾鏡
- 從增強影像中檢索條碼值
如何應用影像濾鏡來提高條碼讀取品質?
若要套用篩選器,請實例化 ImageFilterCollection 類,並分別建立每個篩選器的實例。 然後將該物件指派給 ImageFilters 物件的 BarcodeReaderOptions 屬性。 將選項物件連同範例圖像一起傳遞給 Read 方法。 有關進階安裝選項,請造訪我們的NuGet套件指南。
請使用下圖作為範例圖片。
範例影像
圖片看起來很模糊。 不過,亮度尚可接受,白色和黑色也能區分。 因此,至少應用銳利化濾鏡和對比濾鏡來提高條碼的可讀性。 請參考以下程式碼片段,對影像套用濾鏡,讀取影像,並在控制台上顯示結果。
: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
上面的程式碼片段套用過濾器,讀取條碼,並將過濾後的影像匯出到磁碟。 下面顯示了樣本影像和濾波後影像的對比。
範例影像
過濾後的樣品
IronBarcode有哪些影像校正濾鏡可用?
IronBarcode提供多種專為影像校正而設計的影像濾鏡。 這些濾鏡有助於讀取不完美的條碼影像,提高讀取準確率。 但是,要了解這些過濾器的工作原理,才能選擇合適的過濾器,避免因使用過多過濾器或使用錯誤的過濾器而導致效能問題。 可用的篩選條件包括:
AdaptiveThresholdFilterBinaryThresholdFilterBrightnessFilterContrastFilterInvertFilterSharpenFilterErodeFilterDilateFilterHistogramEqualizationFilter- 模糊濾鏡
GaussianBlurFilterBilateralFilterMedianBlurFilter
濾鏡的應用順序取決於它們在ImageFilterCollection中的位置。 有關這些過濾器的詳細 API 文檔,請造訪我們的API 參考。
自適應閾值濾波器的工作原理是什麼?
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")
以下是使用不同值套用篩選器後的輸出結果。
預設值
0.9 值
建構函式接受用於配置的附加參數:
Upper: 閾值處理的上部(白色)顏色。Lower: 閾值處理的較低(黑色)顏色。Threshold: 二值化閾值限制 (0.0-1.0)。Rectangle: 要套用處理器的矩形區域。
如上圖所示,影像被二值化,只保留了黑白**兩**色。 雖然它似乎仍然不是條碼讀取的理想選擇,但需要組合使用過濾器。 嘗試調整參數靈敏度,以獲得最佳結果。
二元閾值濾波器的工作原理是什麼?
二值閾值濾波器透過在給定閾值處分割像素來過濾影像,比較顏色分量的亮度。 與 AdaptiveThresholdFilter 類似,如果使用不當,此濾波器可能會引入新的或不需要的雜訊。 但是, IronBarcode為篩選屬性設定了預設值。
與 AdaptiveThresholdFilter 類似,BinaryThresholdFilter 也接受相同的附加配置參數:
Upper: 閾值處理的上部(白色)顏色。Lower: 閾值處理的較低(黑色)顏色。Threshold: 二值化閾值限制 (0.0-1.0)。Rectangle: 要套用處理器的矩形區域。
: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")
以下是對範例影像套用濾鏡後的範例輸出結果。
預設值
0.9 值
觀察上面的輸出影像,樣本已被二值化為黑白顏色。 然而,由於條碼條紋被消除,並且引入了新的噪聲,因此該濾鏡顯然不適用於此影像。 對於難以辨識的條碼情況,請參閱我們的條碼故障排除指南。
如何調整影像亮度以獲得更好的條碼讀取效果?
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")
下面這張圖是將此濾鏡套用到範例輸入後的輸出影像。
預設值
1.5 值
如何使用對比度濾鏡增強條碼影像?
對比度濾鏡用於調整影像的對比度等級。 影像對比度是指影像中不同元素之間顏色強度的差異。 提高對比度可以增強細節的可見性,使影像看起來生動鮮明;而降低對比度則會使影像看起來柔和低調。 有關條碼自訂的更多詳細信息,請參閱我們的條碼樣式自訂指南。
預設值為 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")
將此濾鏡套用至範例輸入,即可產生下圖。
預設值
1.5 值
何時應該使用反轉濾波器?
此濾鏡會反轉影像中的顏色,使影像呈現相反的顏色,例如白色變為黑色,黑色變為白色。它在讀取帶有背景色的條碼圖像時尤其有用。 與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")
下面的輸出影像是將此濾波器應用於範例輸入影像的結果。
原圖
倒
如何使用銳利化濾鏡修復模糊的條碼影像?
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")
The image below is the sharpened version of the sample input image.
預設值
0.5 值
將上圖與原始圖進行比較,可以發現它更清晰,有助於使用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");
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相反,它透過在物件邊界添加像素來擴展明亮區域(通常是背景)。 雖然此濾鏡可以透過填充小縫隙或增強低對比區域來修復損壞或模糊的條碼,但請注意,它對條碼條的影響與直覺不同。 由於膨脹會放大明亮的區域,因此它會間接地使黑色條碼條紋等暗部變薄(假設背景為白色)。 這使得該過濾器在條碼條看起來過粗或合併的情況下特別有效,但過度使用可能會因條碼條過窄而降低掃描精度。
與上述類似,透過輸入代表濾波器的整數來增強濾波器的效果。
對於下面的範例,使用更大的核心大小來展示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");
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可能會破壞條碼結構,合併緊密排列的條形,並在條碼中建立空白區域。 根據輸入影像的不同,透過增大或減少內核大小值來測試和改進對影像的影響。
何時應該使用直方圖均衡化濾波器?
直方圖均衡化濾波器透過重新分配像素強度來增強影像對比度,從而提高清晰度。 當條碼對比度低(例如影像褪色或發白)或影像光照不均勻(例如陰影過深或眩光過強)時,最常使用此方法。 透過分析影像直方圖(像素亮度分佈),它透過拉伸強度範圍來增強對比度,從而重新分配像素值,使暗像素變得更暗,亮像素變得更亮。
: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");
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對影像應用高斯模糊。 這種濾鏡通常用於減少影像中的雜訊。 有關處理不完美條碼的全面指南,請參閱我們的影像方向校正教學。
此濾波器的工作原理是使用高斯函數對影像中相鄰像素值進行平均。 此方法依賴兩個可調因素: -核:用於對像素進行平均的矩陣。
- Sigma :控制模糊強度的值。
預設大小為 kernel 像素,預設值為 3x3 像素,產生適度的模糊效果。 增加 Sigma 值會產生更強的模糊效果。 您也可以自訂 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")
將此濾鏡套用至範例輸入,即可產生下圖。
銳化影像
高斯模糊影像
何時應該使用雙邊濾波器?
雙邊濾波器在保持影像邊緣的同時,可以平滑影像。 與均勻影響所有像素的簡單模糊技術不同,雙邊濾波器同時考慮了顏色差異和像素距離,因此可以有效地保持邊緣平滑。
此方法依賴三個可調因素:
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")
將此濾鏡套用至範例輸入,即可產生下圖。
銳化影像
雙側影像
MedianBlur 濾波器在降噪方面有何獨特之處?
MedianBlurFilter透過將每個像素的值替換為周圍像素的中位數來減少影像中的雜訊。 此濾鏡尤其擅長在去除雜訊的同時保留邊緣。要了解更多關於條碼讀取設定的信息,請訪問我們的條碼讀取器設定指南。
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")
將此濾鏡套用至範例輸入,即可產生下圖。
銳化影像
中位數模糊影像
如何在每個處理步驟中保存過濾後的影像?
對條碼套用多個過濾器時,查看每種過濾器方法後的輸出結果可能會很困難。 此功能允許在套用每個濾鏡後,按處理順序儲存濾鏡後的影像。 若要啟用此功能,請先將 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")
過濾器按照程式碼的順序應用,輸出圖像反映了每次迭代的結果:
Sharpen-> 在Sharpen之後Sharpen+AdaptiveThreshold-> 在AdaptiveThreshold之後Sharpen+AdaptiveThreshold+Contrast-> 在Contrast之後
範例影像
磨利後
自適應閾值後
對比後
除了 ImageFilters 屬性之外,也要為 BarcodeReaderOptions 新增其他屬性,以便更準確地讀取; 更多資訊請參閱這篇文章。
常見問題解答
什麼是影像校正篩選器?為什麼條碼讀取需要這些篩選器?
IronBarcode 中的圖像校正過濾器是內置的工具,可程式化地增強模糊或不完美的條碼圖像。它們是必不可少的,因為低劣的圖像質量是妨礙成功讀取條碼的主要因素之一。IronBarcode 提供的濾鏡,如 SharpenFilter 和 ContrastFilter,可以提高讀取準確性,而不需要外部圖像編輯軟件或重新擷取圖像。
如何應用影像修正濾鏡來改善 BarCode 掃描?
要在 IronBarcode 中應用篩選器,請建立一個 ImageFilterCollection 實例,並將個別篩選器實例加入其中。然後将此集合分配給 BarcodeReaderOptions 的 ImageFilters 属性,并将其傳递給 Read 方法。例如:new BarcodeReaderOptions { ImageFilters = new ImageFilterCollection() { new SharpenFilter(3.5f), new ContrastFilter(2.0f) }。}.
建議使用哪些影像過濾器來處理模糊的 BarCode 影像?
對於模糊的條碼影像,IronBarcode 建議至少使用 SharpenFilter 和 ContrastFilter。SharpenFilter 可增強模糊圖像的邊緣清晰度,而 ContrastFilter 可改善明暗區域之間的區別。這些濾鏡共同作用,使 BarCode 更易於閱讀,而無需進行外部影像處理。
我可以自訂影像修正濾鏡的強度嗎?
是的,IronBarcode 允許您使用自訂值配置每個篩選器。例如,SharpenFilter 接受浮點參數(如 3.5f)來控制銳化強度,而 ContrastFilter 則接受參數(如 2.0f)來調整對比層級。這種自訂功能有助於針對不同的影像條件,最佳化濾鏡的效果。
我需要外部影像編輯工具來增強 BarCode 影像嗎?
不,IronBarcode 透過提供內建的影像修正篩選器,免除了外部影像編輯工具的需求。這些程式化的濾鏡,如 SharpenFilter 和 ContrastFilter,可直接在您的 .NET 應用程式中提升影像品質,節省時間並避免依賴第三方軟體。

