如何使用C#影像校正濾波器改善條碼

使用C#影像校正濾鏡來提高條碼解碼

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

IronBarcode提供內建的影像校正濾鏡如ContrastFilter,可程式化地提升模糊或不完美的條碼影像,改善讀取精確度,而不需要外部影像編輯軟體或重新捕捉影像。

不是每張影像都是完美的,影像品質不佳是阻礙成功條碼讀取的主要因素之一。 與其重新捕捉影像或使用外部影像增強軟體,IronBarcode 提供內建濾鏡,可以程式化地提高影像品質。 這些濾鏡幫助IronBarcode讀取困難的影像並提高整體準確性。

繼續閱讀以了解IronBarcode中的可用的影像校正濾鏡、它們對影像的影響及如何應用它們。 欲了解更多綜合的條碼讀取技術,請查看我們的條碼讀取教學

快速開始:應用銳化和對比度濾鏡以改善條碼讀取

在一步驟中,使用BarcodeReaderOptions,應用IronBarcode的濾鏡。 這種方法可提高條碼掃描,同時設定最小且不需要外部工具。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/BarCode

    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類別並獨立建立每個濾鏡的實例。 然後將物件分配給ImageFilters物件的屬性。 將選項物件與範例影像一起傳入Read方法。 如需高級安裝選項,請存取我們的NuGet套件指南

使用下方的影像作為我們的範例影像。

帶有數字 4900203187590 的模糊條碼,顯示濾鏡強化前不佳的影像品質

影像看起來相當模糊。 然而,亮度是可以接受的,白色和黑色是可區分的。 因此,至少應用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
$vbLabelText   $csharpLabel

上面的程式碼片段應用濾鏡,讀取條碼,並將過濾的影像匯出到磁碟。 下方顯示了樣本和過濾影像之間的比較。

帶有數字 4902030187590 的模糊條碼影像,呈現不佳的影像品質
套用影像濾鏡後可讀性提升的條碼,顯示清晰的垂直線條與數字 4902030187590

IronBarcode中有哪些影像校正濾鏡可用?

IronBarcode提供多種影像濾鏡,專為影像校正而設計。 這些濾鏡有助於讀取不完美的條碼影像並提高讀取精確性。 然而,要理解這些濾鏡的運作,以選擇合適的濾鏡,同時避免因使用過多濾鏡或使用錯誤濾鏡而產生的性能問題。 可用的濾鏡包括:

  • AdaptiveThresholdFilter
  • BinaryThresholdFilter
  • BrightnessFilter
  • ContrastFilter
  • InvertFilter
  • SharpenFilter
  • ErodeFilter
  • DilateFilter
  • HistogramEqualizationFilter
  • 模糊濾鏡
    • GaussianBlurFilter
    • BilateralFilter
    • MedianBlurFilter

濾鏡的應用順序基於它們在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")
$vbLabelText   $csharpLabel

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

垂直線,顯示不同的自適應閾值濾鏡輸出,具有實線與虛線圖樣
低品質條碼影像,顯示 UPC 號碼 902030187590,並有明顯的視覺失真

構造函式接受額外的參數來進行配置:

  • Upper:用於閾值化的上限(白色)顏色。
  • Lower:用於閾值化的下限(黑色)顏色。
  • Threshold:二值化的閾值限度(0.0-1.0)。
  • Rectangle:將處理器應用於的矩形區域。

如上方輸出影像所示,影像被二值化為黑色白色顏色。 雖然似乎仍不理想於條形碼讀取,但濾鏡需要結合使用。 通過調整參數敏感性以實驗並達到最佳效果。

二值閾值濾鏡如何運作?

BinaryThresholdFilter透過在給定閾值處分割像素來過濾影像,並比較顏色分量的亮度。 類似於AdaptiveThresholdFilter,若使用不當,此濾鏡可能引入新的或不需要的噪音。 然而,IronBarcode已為濾鏡屬性設置了預設值。

類似於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")
$vbLabelText   $csharpLabel

下方為對樣本影像應用濾鏡的示範輸出。

二元閾值濾鏡輸出的三個範例,顯示稀疏、點狀與密集的垂直線圖樣
以 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")
$vbLabelText   $csharpLabel

下方是將此濾鏡應用於樣本輸入後的輸出影像。

Blurry UPC barcode sample showing default brightness level before filter enhancement
帶有產品編號 4902030187590 的模糊條碼,呈現低亮度或不佳的影像品質

如何使用對比度濾鏡增強條碼影像?

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

將此濾鏡應用於樣本輸入會產生以下影像。

帶有數字 4902030187590 的模糊條碼,呈現預設對比濾鏡設定
帶有數字 4902030187590 的模糊條碼,呈現低對比的影像品質

何時使用反轉濾鏡?

此濾鏡反轉影像中的顏色,使相對顏色如白色變為黑色、黑色變為白色。當讀取背景顏色的條碼影像時特別有用。 不同於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

下方的輸出影像是將此濾鏡應用於樣本輸入影像的結果。

顯示數字 480203187590 的模糊 UPC 條碼 - 套用反轉濾鏡前的原始影像
模糊的反轉條碼,在深色背景上顯示白色條紋與數字序列 4902030187590

我如何用銳化濾鏡修復模糊的條碼影像?

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

下圖為範例輸入影像的銳化版本。

模糊的條碼影像,呈現套用銳化濾鏡前未經銳化的品質
模糊的條碼範例,顯示影像品質劣化的效果

比較上圖與原始影像,相較而言,它顯得更清晰並有助於藉由IronBarcode進行條碼讀取。 在大多數情況下,ImageFilterCollection類中的其他濾鏡一起應用。

腐蝕濾鏡用於什麼?

ErodeFilter去除細小的白色噪音,並透過移除接近形狀邊緣的像素來加粗條碼線條。此濾鏡最佳用於條碼背景有很多白色斑點或條碼影像解析度太低或模糊,導致條形合併的情境。 ErodeFilter使條碼線條加粗,同時移除背景中的白色斑點。 如需處理不完美影像的更多資訊,請參閱我們的不完美條形碼例子

通過輸入代表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")
$vbLabelText   $csharpLabel
顯示數字 4002030187590 的模糊條碼 - 用於侵蝕濾鏡示範的範例
模糊的條碼,顯示黑白垂直條紋與數字程式碼

比較上方輸入與輸出影像,由於輸入較大核大小以過濾,若干條形顯得更粗。 然而,整體圖像中的白色斑點減少了。 由於腐蝕濾鏡的特性,核大小越大,若應用過於激進,可能會如上圖般擦除細條。 透過更改輸入給ErodeFilter的核大小值來測試並改善效果。

膨脹濾鏡如何幫助條碼讀取?

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");
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")
$vbLabelText   $csharpLabel
顯示數字 4902030187590 的模糊條碼影像 - 用於膨脹濾鏡處理的範例
垂直條下方帶有數字序列的模糊條碼

如上圖所示,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");
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")
$vbLabelText   $csharpLabel
帶有數字 4902030187590 的模糊條碼,用作直方圖均衡化濾鏡的測試影像
具有黑白垂直條紋並顯示數字 4902030187590 的條碼

如上圖所示,黑條顯得明顯更深,空格相較原始影像更明亮。

哪些模糊濾鏡可以幫助減少條碼噪聲?

高斯模糊濾鏡如何減少影像噪聲?

GaussianBlurFilter對影像應用高斯模糊。 此濾鏡通常減少影像中的噪聲。 如需處理不完美條形碼的全面指南,請參閱我們的影像方位校正教程

此濾鏡透過平均影像中相鄰像素值使用高斯函式運作。 該方法依賴於兩個可調因素:

  • :一個用於平均像素的矩陣。
  • Sigma:一個控制模糊強度的值。

預設3.0,產生中等模糊。 增加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")
$vbLabelText   $csharpLabel

將此濾鏡應用於樣本輸入會產生以下影像。

帶有數字 4902030187590 的模糊條碼,呈現不佳的影像品質
套用高斯模糊濾鏡的條碼影像,顯示模糊的垂直線條與扭曲的數字

何時應使用雙邊濾鏡?

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

將此濾鏡應用於樣本輸入會產生以下影像。

模糊的條碼,以垂直線條與數字 4902030187590 呈現不佳的影像品質
帶有數字 4902030187590 的模糊條碼,呈現不佳的影像品質

什麼使中值模糊濾鏡在噪聲減少方面不同?

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")
$vbLabelText   $csharpLabel

將此濾鏡應用於樣本輸入會產生以下影像。

模糊的條碼範例,顯示帶有數位雜訊且可讀性降低的不佳影像品質
套用中值模糊濾鏡的條碼,顯示模糊的垂直線條與數字 4902030187590

我如何在每個處理步驟中保存過濾影像?

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

濾鏡按照程式碼順序應用,輸出影像反映每次迭代的結果:

  • Sharpen -> 之後Sharpen
  • Sharpen + AdaptiveThreshold -> 之後AdaptiveThreshold
  • Sharpen + AdaptiveThreshold + Contrast -> 之後Contrast
帶有數字 4902030187590 的模糊條碼
Blurry UPC barcode showing number 4902030187590
劣化的條碼範例,以數字 9020301875905 呈現不佳的影像品質
帶有 UPC 編號 902030187590 的嚴重像素化條碼

除了BarcodeReaderOptions以獲得更準確的讀取; 請參閱此文章以獲得更多資訊。

常見問題

什麼是影像校正濾波器,為什麼條碼讀取需要它們?

IronBarcode的影像校正濾波器是內建工具,可程式化增強模糊或不完美的條碼影像。這些濾波器很重要,因為低畫質影像是阻礙成功條碼讀取的主要因素之一。IronBarcode提供如SharpenFilter和ContrastFilter的濾波器,能提高讀取精確性,不需外部影像編輯軟體或重拍影像。

如何應用影像校正濾波器來提高條碼掃描?

在IronBarcode中應用濾波器,首先建立ImageFilterCollection實例,並向其中加入個別濾波器實例。然後將此集合指派給BarcodeReaderOptions的ImageFilters屬性,再將其傳遞給Read方法。例如:new BarcodeReaderOptions { ImageFilters = new ImageFilterCollection() { new SharpenFilter(3.5f), new ContrastFilter(2.0f) } }。

建議使用哪些影像濾波器來處理模糊的條碼影像?

對於模糊的條碼影像,IronBarcode建議至少使用SharpenFilter和ContrastFilter。SharpenFilter增強模糊影像中的邊緣定義,而ContrastFilter提高明暗區域之間的區分度。這些濾波器協同工作,讓條碼更可讀,不需外部影像處理。

我可以自訂影像校正濾波器的強度嗎?

可以,IronBarcode允許您以自訂值配置每個濾波器。例如,SharpenFilter接受浮點參數(如3.5f)來控制銳化強度,而ContrastFilter接受參數(如2.0f)來調整對比度等級。這種自訂幫助優化濾波器在不同影像條件下的效果。

我需要外部影像編輯工具來增強條碼影像嗎?

不需要,IronBarcode提供內建的影像校正濾波器,去除了使用外部影像編輯工具的需求。這些程式化的濾波器如SharpenFilter和ContrastFilter可以直接在您的.NET應用程式中增強影像品質,節省時間並避免對第三方軟體的依賴。

使用IronBarcode進行條碼操作有什麼好處?

IronBarcode提供了如易於整合、支持多種條碼格式、高品質圖像生成和強大讀取能力等好處,使其成為C#中條碼操作的全面工具。

IronBarcode是否提供自定義條碼外觀的支持?

是的,IronBarcode提供了廣泛的條碼外觀自定義選項,包括顏色、大小和文字註釋,讓您可以根據具體設計需求定制條碼。

IronBarcode如何幫助改善業務流程效率?

IronBarcode通過使條碼生成和讀取快速且準確來提高業務流程效率,減少手動資料輸入錯誤,並改善庫存和資產追蹤。

將IronBarcode實現於專案中需要什麼程式設計技能?

基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文件來指導開發者。

IronBarcode適合於小型專案和大型企業應用嗎?

IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

Hairil Hasyimi Bin Omar
軟體工程師
和所有偉大的工程師一樣,Hairil是一位充滿熱情的學習者。他正在完善自己對C#、Python和Java的知識,並利用這些知識為Iron Software的團隊成員創造價值。Hairil從馬來西亞的瑪拉工藝大學畢業,擁有化學與流程工程學士學位,後加入Iron Software團隊。
準備好開始了嗎?
Nuget 下載 2,317,217 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想快速驗證嗎? PM > Install-Package BarCode
運行範例觀看您的字串成為條碼。