如何使用圖像校正濾鏡
面對現實吧。 並非每張圖像都是完美的,這也是 IronBarcode 無法讀取條碼圖像的主要因素之一。 這不完全是使用者的錯。 為了避免重新捕捉影像或使用其他圖像增強軟體的麻煩,IronBarcode 推出了一項功能,使用者可以以程式設計的方式對影像應用過濾器。 這將有助於IronBarcode讀取圖像並提高準確性。
繼續閱讀以了解IronBarcode中可用的圖像校正濾鏡,它們對圖像的影響,以及如何應用它們。
如何使用圖像校正濾鏡
- 下載C# 庫以使用圖像修正濾鏡
- 探索所有可用的圖像修正濾鏡
- 使用自定值配置每個影像校正濾鏡
- 將濾鏡應用於不完美的圖像樣本
- 使用篩選器從圖像中檢索條碼值
開始使用 IronBarcode
立即在您的專案中使用IronBarcode,並享受免費試用。
使用影像篩選器來改善讀取範例
要應用篩選器,請實例化 ImageFilterCollection 類並分別創建每個篩選器的實例。 然後將對象指派給 BarcodeReaderOptions 對象的 ImageFilters 屬性。 將選項物件傳遞到 Read
方法中,並附帶範例圖像。
讓我們使用下面的圖片作為我們的範例圖片。
示例圖片
從圖像的初始外觀來看,它似乎相當模糊。 然而,亮度可以接受,且白色和黑色可以辨識。 因此,我們至少需要應用SharpenFilter和ContrastFilter來提升條碼的可讀性。 請參考下面的程式碼片段,對圖像進行過濾,讀取它,並在控制台上顯示。
: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提供多種專為圖像校正設計的影像過濾器。 這些過濾器可以幫助讀取不完美的條碼圖像並提高讀取精度。 然而,用戶將需要了解這些過濾器是如何運作的才能
選擇合適的過濾器並避免由於使用過多過濾器或使用錯誤過濾器而導致的性能問題。 以下是所有可用的過濾器:
- 自適應閾值過濾器
- 二進位閾值濾鏡
亮度濾鏡
ContrastFilter
反相濾鏡
SharpenFilter
模糊過濾器
高斯模糊過濾器
雙邊濾波器
- 中值模糊過濾器
這些過濾器的應用順序基於它們在ImageFilterCollection中的位置。
適應性閾值濾波器
AdaptiveThresholdFilter 是 IronBarcode 中可用的過濾器之一,它適用於布拉德利自适应阈值將技術應用於圖像,自動確定用於二值化圖像的閾值。 此過濾器非常適合用於具有非均勻照明和不同背景亮度級別的圖像。
: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")
以下是使用不同值應用過濾器的輸出。
預設值
.9 值
此建構函數還接受其他參數進行配置:
- 上方:上方(白色)用於閾值設定的顏色。
- 小寫(黑色)用於閾值設定的顏色。
- 閾值:閾值限制(0.0-1.0)考慮用於二值化。
矩形:應用處理器的矩形區域。
如上圖所示,圖像已被二值化,只有黑色和白色。 雖然它看起來仍然不是理想的條碼閱讀方式,因為需要組合使用過濾器。 用戶需要試驗參數的敏感性以獲得最佳結果。
二進位閾值過濾器
BinaryThresholdFilter 透過在給定閾值處分割像素來過濾圖像,用於比較顏色組件的亮度。 與AdaptiveThresholdFilter類似,如果使用不當,此過濾器可能會引入新的或不需要的噪音。 然而,IronBarcode 已為過濾器的屬性設置了預設值。
類似於AdaptiveThresholdFilter,BinaryThresholdFilter接受相同的額外參數進行配置。
- 上方:上方(白色)用於閾值設定的顏色。
- 小寫(黑色)用於閾值設定的顏色。
- 閾值:閾值限制(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")
以下是對樣本圖片應用濾鏡的樣本輸出。
預設值
.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")
以下是將此篩選器應用於範例輸入後的輸出影像。
預設值
.5 值
對比過濾器
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")
將此篩選器應用於示例輸入將生成下圖。
預設值
.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中的最後一個圖像校正過濾器是SharpenFilter。 此篩選器增強了圖像的銳利度,對於模糊圖像非常有用。 使用者可以在實例化過濾器對象時調整Sigma值,來操控此過濾器調整圖像的銳利度。 默認值為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")
下圖是 銳化 範例輸入圖像的版本。
預設值
.5 值
比較上面的圖像與原始圖像,它似乎更清晰,並且肯定會在使用 IronBarcode 進行條碼讀取時有所幫助。 在大多數情況下,SharpenFilter 總是與 ImageFilterCollection 類中的其他過濾器一起使用。
模糊過濾器
高斯模糊濾鏡
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")
將此篩選器應用於示例輸入將生成下圖。
銳化圖像
高斯模糊圖像
雙邊濾波器
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")
將此篩選器應用於示例輸入將生成下圖。
銳化圖像
雙側影像
中值模糊濾波器
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")
將此篩選器應用於示例輸入將生成下圖。
銳化圖像
中值模糊影像
保存迭代
當對條碼應用多個篩選器時,可能很難在每個篩選方法後查看輸出。 此功能允許儲存每個濾鏡應用後的過濾影像,按照處理順序進行。 若要啟用此功能,首先在 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")
篩選器按照程式碼的順序應用,輸出圖片反映每次迭代的結果:
- 銳化 -> 銳化後
- 銳化 + 自適應閾值 -> 自適應閾值後
- 銳化 + 自適應閾值 + 對比度 -> 對比後
示例圖片
銳化後
自適應閾值後
對比後
Apart from
圖像過濾器
屬性,使用者也可以加入其他屬性條碼讀取器選項
要獲得更準確的閱讀,請參見此內容 文章 了解更多資訊。