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

使用 C# 影像校正篩選器來改善 BarCode 解碼。

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

IronBarcode提供內建的影像修正篩選器,如SharpenFilterContrastFilter,可程式化地強化模糊或不完美的 BarCode 影像,提高讀取精準度,而不需要外部影像編輯軟體或重新擷取影像。

不是每張圖像都是完美的,圖像品質差是妨礙 IronBarcode 成功讀取條碼的主要因素之一。 與其重新擷取影像或使用外部影像增強軟體,IronBarcode 提供內建濾鏡,可程式化地改善影像品質。 這些過濾器有助於 IronBarcode 讀取困難的圖像,並提高整體精確度。

繼續閱讀,了解 IronBarcode 中可用的影像校正濾鏡、它們對影像的影響以及如何應用它們。 如需更全面的條碼讀取技巧,請查看我們的 讀取 BarCode 教程

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

只需一個步驟,即可使用 BarcodeReaderOptions 中的 ImageFilterCollection 應用 IronBarcode 的 SharpenFilterContrastFilter 。 這可改善 BarCode 掃描功能,且只需最少的設定,對外部工具的需求為零。

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`方法。 如需進階安裝選項,請造訪我們的 [NuGet 套件指南](https://ironsoftware.com/csharp/barcode/get-started/advanced-installation-nuget/)。 使用以下圖片作為我們的範例圖片。
Blurred barcode with number 4900203187590 showing poor image quality before filtering enhancement
圖片看起來相當模糊。 不過,亮度尚可接受,白色和黑色也能區分。 因此,至少應用 **SharpenFilter** 和 **ContrastFilter** 來改善 BarCode 的可讀性。 請參考以下的程式碼片段,將濾鏡套用至影像、讀取影像,並將結果顯示在控制台。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-apply-filter.cs ``` 上面的程式碼片段會套用篩選器、讀取條碼,並將篩選過的影像匯出到磁碟。 樣本與篩選圖片的比較如下所示。
Blurry barcode image with number 4902030187590 demonstrating poor image quality
Barcode with improved readability after applying image filters, showing clear vertical lines and number 4902030187590

IronBarcode 提供哪些圖像校正濾鏡? IronBarcode 提供多種[影像濾鏡](https://ironsoftware.com/csharp/barcode/features/filters/),專門用於影像修正。 這些篩選器可協助讀取不完美的 BarCode 影像,並提高讀取精確度。 然而,了解這些篩選器的運作方式,才能**選擇合適的篩選器**,並**避免因使用太多篩選器或使用錯誤的篩選器而造成效能問題**。 可用的篩選條件包括 - `自適應閾值過濾器` - `二進位閾值過濾器` -<編碼>BrightnessFilterSharpenFilterErodeFilterDilateFilterHistogramEqualizationFilterBilateralFilterMedianBlurFilter自適應閾值篩選器如何運作? **AdaptiveThresholdFilter** 是 IronBarcode 中可用的篩選器,可將 [Bradley Adaptive Threshold](https://saturncloud.io/blog/bradley-adaptive-thresholding-algorithm-a-powerful-tool-for-image-segmentation/#:~:text=The%20Bradley%20adaptive%20thresholding%20algorithm%2C%20proposed%20by%20Derek%20Bradley%20and,illumination%20or%20varying%20contrast%20levels.) 技術套用至影像,自動決定影像二值化的閾值。 此濾鏡非常適合用於光照不均勻、背景強度變化較大的影像。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-adaptive-threshold.cs ``` 以下是使用不同值套用篩選器後的輸出結果。
Vertical lines showing different adaptive threshold filter outputs with solid and dashed patterns
Low-quality barcode image showing UPC number 902030187590 with significant visual distortion
建構器會接受額外的設定參數: - `Upper`:閾值的上色 (白色)。 - `Lower`:閾值的下層 (黑色) 顏色。 - `閾值`:二值化的閾值限制 (0.0-1.0)。 - `Rectangle`:要套用處理器的矩形區域。 如上面的輸出影像所示,影像經過二值化處理,只有 ** 黑** 和 ** 白** 兩種顏色。 雖然對於 BarCode 讀取來說似乎仍不理想,但濾鏡需要組合使用。 請嘗試使用參數靈敏度以達到最佳效果。

二進位閾值篩選器如何運作? **BinaryThresholdFilter** 透過在指定的臨界值分割像素,比較色彩元件的亮度,來過濾影像。 與 `AdaptiveThresholdFilter` 相似,如果使用不當,此過濾器可能會引入新的或不想要的雜訊。 然而,IronBarcode 已為篩選器屬性設定預設值。 與 `AdaptiveThresholdFilter` 相似,`BinaryThresholdFilter` 也接受相同的附加參數進行設定: - `Upper`:閾值的上色 (白色)。 - `Lower`:閾值的下層 (黑色) 顏色。 - `閾值`:二值化的閾值限制 (0.0-1.0)。 - `Rectangle`:要套用處理器的矩形區域。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-binary-threshold.cs ``` 以下是對範例影像套用濾鏡後的範例輸出結果。
Three examples of binary threshold filter outputs showing sparse, dotted, and dense vertical line patterns
Barcode image processed with 0.9 binary threshold filter showing black and white contrast
觀察上面的輸出影像,樣本已二值化為黑白顏色。 然而,由於條碼條被消除,並引進新的雜訊,此濾鏡顯然不適合此影像。 如需處理困難的 BarCode 情境,請參閱我們的 [無法辨識條碼的疑難排解指南](https://ironsoftware.com/csharp/barcode/troubleshooting/barcode-not-recognized/)。

如何調整影像亮度以獲得更好的條碼讀取效果? **BrightnessFilter**是 IronBarcode 影像濾鏡集合中的另一個重要濾鏡。 顧名思義,此濾鏡可調整條碼影像的亮度。 此構成器的輸入會改變輸出影像中的 **Amount** 亮度。 預設值為 1,這表示影像保持不變。 值為 0 會產生完全黑色的影像,而值高於 1 則會使影像更亮。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-brightness.cs ``` 下面這張圖是將此濾鏡套用到範例輸入後的輸出影像。
Blurry UPC barcode sample showing default brightness level before filter enhancement
Blurry barcode with product number 4902030187590, demonstrating low brightness or poor image quality

如何使用對比度篩選器來增強 BarCode 影像? **ContrastFilter** 可調整影像的對比度。 影像對比度是指影像中不同元素之間顏色強度的差異。 提高對比度可以增強細節的可見性,使影像看起來生動鮮明;而降低對比度則會使影像看起來柔和低調。 有關條碼自訂的詳細資訊,請參閱我們的 [ 自訂條碼樣式](https://ironsoftware.com/csharp/barcode/how-to/customize-barcode-style/)指南。 預設值為 1,這表示影像保持不變。 值為 0 會產生完全灰色的影像,而值高於 1 則會增加影像的對比度。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-contrast.cs ``` 將此濾鏡套用至範例輸入,即可產生下圖。
Blurry barcode with number 4902030187590 demonstrating default contrast filter settings
Blurry barcode with number 4902030187590 demonstrating low contrast image quality

何時應該使用反向篩選器? 此濾鏡可將影像內部的顏色反轉,變成相反的顏色,例如白色變成黑色,黑色變成白色。在讀取有背景顏色的 BarCode 影像時特別有用。 與 **BinaryThresholdFilter** 不同,此濾鏡可直接反轉顏色,而無需指定敏感度。 此外,此濾鏡可與 **CropRectangle** 搭配使用,以指定影像中需要反轉顏色的位置,而不是反轉整個影像的顏色。 在我們的[作物區域教程](https://ironsoftware.com/csharp/barcode/how-to/set-crop-region/)中瞭解有關指定作物區域的更多資訊。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-invert.cs ``` 下面的輸出影像是將此濾鏡套用至範例輸入影像的結果。
Blurry UPC barcode showing number 480203187590 - original image before invert filter application
Blurry inverted barcode showing white bars on dark background with number sequence 4902030187590

如何使用銳化篩選器修復模糊的 BarCode 影像? IronBarcode 提供銳化篩選器。 此濾鏡可以增強影像的清晰度,在處理模糊影像時非常有用。 在實體化濾鏡物件時,透過調整 **Sigma** 值來操控此濾鏡,以調整影像的銳利度。 預設值為 3。增加 sigma 值可以提高影像清晰度。 如需其他效能最佳化選項,請參閱我們的 [ 讀取速度選項指南](https://ironsoftware.com/csharp/barcode/how-to/reading-speed-options/)。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-sharpen.cs ``` The image below is the sharpened version of the sample input image.
Blurry barcode image demonstrating unsharpened quality before applying sharpen filter
Blurred barcode example showing effects of image quality degradation
將上面的圖片與原始圖片比較,它顯得更清晰,並有助於使用 IronBarcode 讀取條碼。 大多數情況下, **SharpenFilter**總是與`ImageFilterCollection`類別中的其他濾鏡一起應用。

Erode 過濾器有何用途? **ErodeFilter** 可移除微小的白噪音,並透過移除形狀邊緣附近的像素來加粗 BarCode 條碼。此濾鏡最適合用於條碼背景有大量白色斑點的情況,或是條碼影像解析度太低或模糊,導致條碼合併的情況。 **ErodeFilter**可使條形圖變得更粗,同時移除背景中的白色斑點。 如需處理不完美圖像的更多資訊,請參閱我們的 [ 不完美 BarCode 示例](https://ironsoftware.com/csharp/barcode/examples/imperfect-barcode-with-image-correction/)。 輸入代表篩選器 `kernelSize` 的整數,以增加侵蝕的效果。 卷積核尺寸越大,對輸入影像的影響就越強。 請注意,`kernelSize` 是一個正方形,在這個範例中是一個 `5x5` 核心。 以 **ErodeFilter** 為例,使用較大的核心大小來展示濾波器的效果。 ```cs :path=/static-assets/barcode/content-code-examples/how-to/image-correction-erode.cs ```
Blurry barcode showing number 4002030187590 - example for erode filter demonstration
Blurred barcode showing vertical black and white stripes with numerical code
比較上面的輸入和輸出影像,由於輸入較大的核心尺寸進行篩選時較為激進,因此有些條形會明顯變厚。 然而,整體畫面中的白色斑點減少了。 根據侵蝕濾鏡的性質,內核尺寸越大,如果過於激烈地應用,可能會擦除細條,如上圖所示。 透過變更 **ErodeFilter** 輸入的核心大小值,測試並改善效果。

Dilate 過濾器如何幫助條碼讀取? **DilateFilter 的**功能與**ErodeFilter**相反,它透過在物件邊界添加像素來擴展明亮區域(通常是背景)。 雖然此濾鏡可藉由填補細小縫隙或增強低對比度區域來修復損壞或模糊的 BarCode,但請注意其對 BarCode 條碼的效果與直覺不同。 由於擴張會放大明亮的空間,因此會間接變薄暗色元素,例如黑色 BarCode 條碼(假設背景為白色)。 這使得該過濾器在條碼條看起來過粗或合併的情況下特別有效,但過度使用可能會因條碼條過窄而降低掃描精度。 與上述類似,透過輸入代表篩選器的 `kernelSize` 的整數來增加篩選器的效果。 在下面的範例中,請使用較大的核心大小來展示 **DilateFilter**的效果。 ```cs :path=/static-assets/barcode/content-code-examples/how-to/image-correction-dilate.cs ```
Blurry barcode image showing number 4902030187590 - example for dilate filter processing
Blurred barcode with numerical sequence below vertical bars
如上圖所示,積極使用 **DilateFilter** 有可能會破壞條碼結構,合併間距較近的條碼,並在條碼中產生靜音區。 根據輸入的影像,將核心大小值變得更大或更小,以測試並改善對影像的影響。

何時應該使用 HistogramEqualization 篩選器? **HistogramEqualizationFilter** 可透過重新分配像素強度來增強影像對比,以改善清晰度。 它最常用於條碼對比度較低的情況,例如褪色或褪色的影像,或是光線不均勻的影像,例如暗影或強光。 透過分析影像直方圖(像素亮度分佈),它透過拉伸強度範圍來增強對比度,從而重新分配像素值,使暗像素變得更暗,亮像素變得更亮。 ```cs :path=/static-assets/barcode/content-code-examples/how-to/image-correction-histogram-equalization-filter.cs ```
Blurry barcode with number 4902030187590 used as test image for histogram equalization filter
Barcode with vertical black and white stripes showing number 4902030187590
如上圖所示,與原始圖片相比,黑條明顯變暗,空格明顯變亮。

哪些模糊過濾器可以幫助降低 BarCode 的雜訊?

GaussianBlur 過濾器如何降低影像雜訊? **GaussianBlurFilter** 會將高斯模糊套用至影像。 此濾鏡通常可降低影像中的雜訊。 如需處理不完美 BarCode 的全面指南,請參閱我們的 [影像方向修正教學](https://ironsoftware.com/csharp/barcode/how-to/image-orientation-correction/)。 此濾波器的工作原理是使用高斯函數對影像中相鄰像素值進行平均。 此方法依賴兩個可調因素: -**核**:用於對像素進行平均的矩陣。 - **Sigma**:控制模糊強度的數值。 預設的 ` 核心`大小為 `3x3` 像素,預設的 `Sigma` 值為 `3.0` ,會產生適度的模糊。 增加 `Sigma` 值會產生更強的模糊效果。 您也可以自訂 ` 核心`,以控制模糊濾鏡平均的鄰域大小。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-gaussianblur.cs ``` 將此濾鏡套用至範例輸入,即可產生下圖。
Blurry barcode with number 4902030187590 demonstrating poor image quality
Barcode image with Gaussian blur filter applied, showing blurred vertical lines and distorted numbers

何時應該使用雙邊篩選器? **BilateralFilter** 可在保留邊緣的同時平滑影像。 不同於簡單的模糊技術會均勻地影響所有像素,Bilateral Filter 會同時考慮顏色差異和像素距離,使其能有效地進行邊緣保留平滑處理。 此方法依賴三個可調因素: - `NeighborhoodDiameter`:像素鄰域直徑 (預設值:5)。 - `SigmaColor`:決定色差影響的顏色影響(預設:75.0)。 - `SigmaSpace`:決定距離影響的空間影響(預設:75.0)。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-bilateral.cs ``` 將此濾鏡套用至範例輸入,即可產生下圖。
Blurred barcode demonstrating poor image quality with vertical lines and numbers 4902030187590
Blurred barcode with numbers 4902030187590 demonstrating poor image quality

是什麼讓 MedianBlur 濾波器在降低雜訊方面與眾不同? **MedianBlurFilter**會以周圍像素的中值取代每個像素的值,以減少影像中的雜訊。 此濾鏡尤其擅長保留邊緣,同時去除雜訊。若要探索更多關於條碼讀取設定的資訊,請造訪我們的 [ 條碼讀取器設定指南](https://ironsoftware.com/csharp/barcode/examples/barcode-reader-settings-csharp/)。 - `KernelSize`:用於計算中位數的鄰區大小(必須是奇數,預設值:5)。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-medianblur.cs ``` 將此濾鏡套用至範例輸入,即可產生下圖。
Blurry barcode example showing poor image quality with digital artifacts and reduced readability
Barcode with median blur filter applied showing blurred vertical lines and number 4902030187590

如何在每個處理步驟中儲存篩選過的影像? 在對 BarCode 應用多個篩選器時,檢視每個篩選方法之後的輸出可能會有困難。 此功能允許在套用每個濾鏡後,按處理順序儲存濾鏡後的影像。 若要啟用此功能,請先將`true`傳遞給**ImageFilterCollection**建構子。 然後使用 `ExportFilterImagesToDisk` 方法提供輸出影像的路徑和名稱。 如需儲存條碼的更多範例,請參閱我們的 [ 將條碼轉換為影像範例](https://ironsoftware.com/csharp/barcode/examples/csharp-convert-barcode-to-image/)。 ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-correction-save-iterations.cs ``` 過濾器按照程式碼的順序應用,輸出圖像反映了每次迭代的結果: - `Sharpen` -> `Sharpen` 之後 - `Sharpen` + `AdaptiveThreshold` -> `AdaptiveThreshold` 之後 - `Sharpen` + `AdaptiveThreshold` + `Contrast` -> `Contrast` 之後
Blurry barcode with number 4902030187590
Blurry UPC barcode showing number 4902030187590
Degraded barcode example showing poor image quality with number 9020301875905
Heavily pixelated barcode with UPC number 902030187590
除了 `ImageFilters` 屬性之外,為了更精確地閱讀,請在 `BarcodeReaderOptions` 中加入其他屬性; 更多資訊請參閱這篇[文章](https://ironsoftware.com/csharp/barcode/how-to/read-barcodes-from-images/#setting-barcode-reader-options)。

常見問題解答

什麼是影像校正篩選器?為什麼條碼讀取需要這些篩選器?

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 應用程式中提升影像品質,節省時間並避免依賴第三方軟體。

哈伊里爾·哈希米·本·奧馬爾
軟體工程師
和所有優秀的工程師一樣,Hairil 是一位孜孜不倦的學習者。他不斷精進 C#、Python 和 Java 的知識,並運用這些知識為 Iron Software 的團隊成員創造價值。 Hairil 加入 Iron Software 團隊之前,畢業於馬來西亞瑪拉製程大學 (Universiti Teknologi MARA),並獲得了化學與製程工程學士學位。
準備好開始了嗎?
Nuget 下載 2,035,202 | 版本: 2025.12 剛剛發布