Introduction
The main conditions for the accurate recognition of the barcode in the image are as follows: after the image is converted to a black-and-white binary image, the boundaries between the bars and spaces in the barcode must be clear, the shape (boundaries) of the bars must not be distorted (e.g., enlarged, reduced, warped, etc.), and the image must be free of noise.
When the quality of the barcode in the image is poor, the algorithms provided in the SDK can only support certain fixed processing methods to improve the recognition rate. To recognize barcodes from low-quality images, more complex and targeted processing is required.
Image processing is generally carried out in the following ways:
When the quality of the barcode in the image is poor, the algorithms provided in the SDK can only support certain fixed processing methods to improve the recognition rate. To recognize barcodes from low-quality images, more complex and targeted processing is required.
Image processing is generally carried out in the following ways:
- Converting color images/grayscale images to black-and-white images
Different methods used to convert images to black-and-white may result in different widths of the bars and spaces in the barcode. Trying various conversion methods may help find more accurate boundaries between bars and spaces. -
Adjusting brightness/contrast
Enhance boundaries in the image (e.g., the junction between bars and spaces), so that the bar areas and space areas can be distinguished more correctly when the image is converted to black-and-white. -
Using filters to remove noise points
The less noise there is, the less interference there will be to barcode recognition. -
Applying operations such as blurring/sharpening
Modify boundary characteristics to increase the chance of locating accurate boundaries.
How to process barcode images in C#?
Install OnBarcode Imaging library
You need install OnBarcode C# imaging library to process images in your .NET projects.
- Search OnBarcode.Image package from "Manage NuGet Packages..S" in Visual Studio
- Install the latest version
Read, save processing images
Here we will explain how to apply the image processing in your C# applications.
Using OnBarcode image library, you can read, load an image from file path,
- Create a
ImageProcessorobject with an image loaded. - Apply one or multiple imaging process functions on the image
- Call method
GetResultto get the processed image inBitmapobject
Bitmap bitmap = new Bitmap("C:\\input\\sample-image.png"); using (ImageProcessor obj = ImageProcessor.FromBitmap(bitmap)) { // To apply one or more imaging processes here. obj.Bilevel(128); Bitmap result = obj.GetResult(); }
Using OnBarcode image library, you can read, load an image from file path,
Stream or Bitmap object.
And you will get the processed image data from Bitmap, Stream object or file path on disc also.
// Read an image public static ImageProcessor FromFile(String imageFilePath) public static ImageProcessor FromStream(Stream imageStream) public static ImageProcessor FromBitmap(Bitmap bitmap) // Get the processed image public Bitmap GetResult() public void SaveToStreamAsPNG(Stream stream) public void SaveToFileAsPNG(String filePath)
Explore the image processing details
OnBarcode C# image processing library includes the following categories
- Image Enhancement
- Image Segmentation
- Image Filtering
- Morphological Processing
Image Enhancement
Image enhancement includes the following functions
- Grayscale
- Negate
- Gamma
- Brightness and Contrast
- Equalize
Grayscale
Convert the color image to a grayscale image using the specified conversion method.
Why Grayscale will improve barcode reading performance?
This image filter applies to images that are overall too dark or too bright. Manually selecting different thresholds for black and white pixels allows for more accurate definition of the boundaries between bars and spaces. Generally, a smaller threshold should be used for darker images, and a larger threshold for brighter ones.
This function works with color images and internally converts them to grayscale using the default method.
- GrayscaleMethod
The difference between the various conversion methods lies in the weights used for the different components (Red, Green, Blue) during conversion.
Average: Uses equal weights for R, G, and B.
Rec601: Based on the color standard Rec.601 (or BT.601).
Rec709: Based on the color standard Rec.709 (or BT.709).
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { obj.Grayscale(GrayscaleMethod.Average); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | GrayscaleMethod.Average |
|
|
| GrayscaleMethod.Rec601 | GrayscaleMethod.Rec709 |
|
|
Why Grayscale will improve barcode reading performance?
This image filter applies to images that are overall too dark or too bright. Manually selecting different thresholds for black and white pixels allows for more accurate definition of the boundaries between bars and spaces. Generally, a smaller threshold should be used for darker images, and a larger threshold for brighter ones.
This function works with color images and internally converts them to grayscale using the default method.
Negate
Inverts the bits of all pixels in the image. It is mainly applicable to grayscale images and can also be applied to color images.
Suitable for images with inverted black and white.
In some cases, the bars in the image are white (or bright), while the spaces are black (or dark). In such situations, the image must be color-inverted before recognition.
Why Negate image filter will improve barcode reading performance?
Suitable for images with inverted black and white. In some cases, the bars in the image are white (or brighter colors) and the spaces are black (or darker colors). In such situations, the image must be color-inverted before barcode recognition.
Suitable for images with inverted black and white.
In some cases, the bars in the image are white (or bright), while the spaces are black (or dark). In such situations, the image must be color-inverted before recognition.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { obj.Negate(); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Result |
|
|
Why Negate image filter will improve barcode reading performance?
Suitable for images with inverted black and white. In some cases, the bars in the image are white (or brighter colors) and the spaces are black (or darker colors). In such situations, the image must be color-inverted before barcode recognition.
Gamma
Applies Gamma correction (grayscale correction) to the image.
Supports using independent Gamma values for each color component (Red, Green, and Blue).
Applies Gamma correction for each color component (Red, Green, and Blue).
Why Gamma image process will improve barcode scanning performance?
Gamma correction is mainly used to adjust the brightness and contrast of an image. When the barcode image is overall too bright or too dark, you can apply correction first to make the boundaries between bars and spaces in the image clearer, thereby improving recognition performance.
When the image is generally dark, select a Gamma value greater than 1; when bright overall, select a Gamma value less than 1.
Supports using different correction values for different color components separately.
- gamma
When the Gamma value is less than 1, the image becomes darker; when it is greater than 1, the image becomes brighter.
Common range: 0.8 ~ 2.3
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { float gamma = 0.8F; obj.Gamma(gamma); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Gamma: 0.8 |
|
|
| Gamma: 1.0 | Gamma: 2.3 |
|
|
Applies Gamma correction for each color component (Red, Green, and Blue).
- gammaRed Gamma for Red component in image.
- gammaGreen Gamma for Green component in image.
- gammaBlue Gamma for Blue component in image.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { float gammaRed = 1.0F; float gammaGreen = 2.0F; float gammaBlue = 1.0F; obj.Gamma(gammaRed, gammaGreen, gammaBlue); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Gamma Red: 1; Gamma Green: 2; Gamma Blue: 1 |
|
|
Why Gamma image process will improve barcode scanning performance?
Gamma correction is mainly used to adjust the brightness and contrast of an image. When the barcode image is overall too bright or too dark, you can apply correction first to make the boundaries between bars and spaces in the image clearer, thereby improving recognition performance.
When the image is generally dark, select a Gamma value greater than 1; when bright overall, select a Gamma value less than 1.
Supports using different correction values for different color components separately.
Brightness Contrast
Adjust the brightness and contrast of the image.
- brightness
Adjust the image brightness. The smaller the value, the lower the image brightness; the larger the value, the higher the image brightness.
Valid range: -100 ~ 100 (0 means no change in brightness). - contrast
Adjust the image contrast. The smaller the value, the lower the image contrast; the larger the value, the higher the image contrast.
Valid range: -100 ~ 100 (0 means no change in contrast).
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { float brightness = -50F; float contrast = 0F; obj.BrightnessContrast(brightness, contrast); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Brightness: -50; Contrast: 0 |
|
|
| Brightness: 50; Contrast: 0 | Brightness: 0; Contrast: -50 |
|
|
| Brightness: 0; Contrast: 50 | |
|
Why Brightness Contrast image process will improve barcode scanning performance?
Directly adjust the brightness/contrast of the image to make the boundaries between the Bar and Space more distinct, thereby improving the barcode recognition effect.
Equalize
Use Histogram Equalization to adjust the distribution of brightness values of image pixels, thereby enhancing the contrast of the image.
Why Equalize image filter will improve barcode scanning performance?
Enhancing the contrast can highlight the boundaries between Bar and Space, thereby improving the barcode recognition effect.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { obj.Equalize(); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Brightness: -50; Contrast: 0 |
|
|
Why Equalize image filter will improve barcode scanning performance?
Enhancing the contrast can highlight the boundaries between Bar and Space, thereby improving the barcode recognition effect.
Image Segmentation
Image Segmentation includes the following functions
- AdaptiveThreshold
- Bilevel
Adaptive Threshold
Take each pixel as the center, calculate the grayscale average of all pixels within a region of the specified size, and use this average as the threshold to define black and white pixels. This method is mainly applicable to grayscale images.
The C# source code below explains how to apply the Adaptive Threshold image process in C# program.
- width, height: Width and height of the specified region (in pixels). The larger the value, the more pixels are included in the mean calculation. In terms of processing effect, a smaller region can better highlight details.
- offset Define the difference between the mean value and the threshold. The larger the value, the higher the threshold, which will result in more pixels being defined as black.
Note: Setting the offset to a negative number has no effect.
The C# source code below explains how to apply the Adaptive Threshold image process in C# program.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { int width = 10; int height = 10; obj.AdaptiveThreshold(width, height, 0); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Width: 10; Height: 10; Offset: 0 |
|
|
| Width: 50; Height: 50; Offset: 0 | Width: 500; Height: 500; Offset: 0 |
|
|
Why Adaptive Threshold will improve barcode scanning performance?
This is applicable to situations where there is a significant difference in brightness between different areas of an image. If a uniform threshold is used, the bars in darker areas will appear wider, while those in brighter areas will appear narrower, resulting in incorrect width ratios of bars within the same barcode. Evaluating the threshold for black and white pixels based on adjacent pixels (rather than the entire image) can mitigate such issues.
This function is only applicable to grayscale images. Applying it directly to color images yields poor results. For optimal outcomes, you need to first convert the image to grayscale (using the Grayscale function) before invoking this function.
The selection of Width/Height values is primarily determined by the size of regions in the image where significant brightness variations occur. If such regions are small, smaller Width/Height values should be chosen.
Offset can be used to adjust the width of bars. A larger Offset value means more pixels will be classified as bars, resulting in wider bar widths.
Bilevel
Use the given grayscale value as the threshold to define black and white pixels.
This method is applicable to Color or Grayscale images, and Color images will be automatically converted to grayscale images internally.
Why Bilevel will improve barcode scanning performance?
Suitable for images that are overall too dark or too bright. By manually selecting different thresholds for black and white pixels, you can more accurately define the boundaries between Bars and Spaces. Generally, a smaller threshold should be used if the image is dark overall; conversely, a larger threshold should be used if bright.
This function supports color images and will internally convert the image to grayscale using the default method.
- threshold Define the threshold. A larger value results in more pixels being defined as black; conversely, a smaller value results in more pixels being defined as white.
Valid range: 0 ~ 255.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { int threshold = 128; obj.Bilevel(threshold); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Threshold: 96 |
|
|
Why Bilevel will improve barcode scanning performance?
Suitable for images that are overall too dark or too bright. By manually selecting different thresholds for black and white pixels, you can more accurately define the boundaries between Bars and Spaces. Generally, a smaller threshold should be used if the image is dark overall; conversely, a larger threshold should be used if bright.
This function supports color images and will internally convert the image to grayscale using the default method.
Image Filtering
Image Filtering includes the following functions
- Gaussian Blur
- Adaptive Blur
- Selective Blur
- Sharpen
- Unsharp Mask
- Despeckle
- Edge
Gaussian Blur
Apply blur to the image (convolution with a Gaussian distribution).
Why Gaussian Blur will improve barcode reading performance?
Blurring the image can reduce noise in the image (but also weakens the effective signal). Since the degree of reduction for the two may not be the same, it is beneficial for barcode recognition when noise is reduced more significantly than the signal.
- radius
Blur radius, in pixels.
The larger the value, the more blurred the image.
Recommended value: 1 ~ 10.
0 means the function will automatically select an appropriate value. - sigma
Standard deviation, in pixels. The larger the value, the more blurred the image.
Recommended value: 1/3 of the radius.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { float radius = 6; float sigma = 2; obj.GaussianBlur(radius, sigma); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Radius: 6; Sigma: 2 |
|
|
| Radius: 12; Sigma: 4 | Radius: 24; Sigma: 8 |
|
|
Why Gaussian Blur will improve barcode reading performance?
Blurring the image can reduce noise in the image (but also weakens the effective signal). Since the degree of reduction for the two may not be the same, it is beneficial for barcode recognition when noise is reduced more significantly than the signal.
Adaptive Blur
Based on Gaussian blur, reduce the blur intensity in edge regions of the image to preserve edge sharpness.
Why Adaptive Blur will improve barcode reading performance?
Similar to Gaussian blur, but reduces the blur intensity at edge regions (i.e., the boundaries between Bars and Spaces), making it more helpful for locating the boundaries between Bars and Spaces.
- radius
Blur radius, in pixels.
The larger the value, the more blurred the image.
Recommended value: 1 ~ 10.
0 means the function will automatically select an appropriate value. - sigma
Standard deviation, in pixels. The larger the value, the more blurred the image.
Recommended value: 1/3 of the radius.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { float radius = 6; float sigma = 2; obj.AdaptiveBlur(radius, sigma); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Radius: 6; Sigma: 2 |
|
|
| Radius: 12; Sigma: 4 | Radius: 24; Sigma: 8 |
|
|
Why Adaptive Blur will improve barcode reading performance?
Similar to Gaussian blur, but reduces the blur intensity at edge regions (i.e., the boundaries between Bars and Spaces), making it more helpful for locating the boundaries between Bars and Spaces.
Selective Blur
On the basis of applying Gaussian blur, set a contrast threshold and only blur pixels below the threshold.
Why Selective Blur will improve barcode reading performance?
Similar to Gaussian blur, but performs selective blurring on image pixels, blurring only regions with high contrast. This helps highlight the boundaries between Bars and Spaces and reduces the impact of the blurring operation on the barcode signal.
- radius
Blur radius, in pixels.
The larger the value, the more blurred the image.
Recommended value: 1 ~ 10.
0 means the function will automatically select an appropriate value. - sigma
Standard deviation, in pixels. The larger the value, the more blurred the image.
Recommended value: 1/3 of the radius. - threshold Pixels with contrast below the threshold are blurred. The higher the threshold, the larger the area that will be blurred.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { float radius = 12; float sigma = 4; float threshold = 80; obj.SelectiveBlur(radius, sigma, threshold); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Radius: 12; Sigma: 4; Threshold: 0 |
|
|
| Radius: 12; Sigma: 4; Threshold: 50 | Radius: 12; Sigma: 4; Threshold: 100 |
|
|
Why Selective Blur will improve barcode reading performance?
Similar to Gaussian blur, but performs selective blurring on image pixels, blurring only regions with high contrast. This helps highlight the boundaries between Bars and Spaces and reduces the impact of the blurring operation on the barcode signal.
Sharpen
Sharpen the image.
Why Sharpen will improve barcode reading performance?
Sharpening the edges can highlight the boundaries between Bars and Spaces, improving recognition performance.
Sharpening also increases noise, so an appropriate value should be chosen to balance noise and the effective signal (bar boundaries).
- radius
Gaussian radius, in pixels. The larger the value, the sharper the edges in the image.
0 means the function will automatically select an appropriate value. - sigma Gaussian standard deviation, in pixels. The larger the value, the sharper the edges in the image.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { float radius = 0; float sigma = 2; obj.Sharpen(radius, sigma); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Radius: 0; Sigma: 2 |
|
|
| Radius: 0; Sigma: 4 | Radius: 0; Sigma: 8 |
|
|
Why Sharpen will improve barcode reading performance?
Sharpening the edges can highlight the boundaries between Bars and Spaces, improving recognition performance.
Sharpening also increases noise, so an appropriate value should be chosen to balance noise and the effective signal (bar boundaries).
Unsharp Mask
Sharpen the image using the Unsharp Mask algorithm.
Why Unsharp Mask will improve barcode reading performance?
Sharpening the edges in the image can highlight the boundaries between Bars and Spaces, improving the recognition effect.
Sharpening also increases noise at the same time. An appropriate value should be selected to balance noise and the effective signal (Bar boundaries).
- radius Gaussian radius, in pixels. 0 means the function automatically selects an appropriate value.
- sigma Gaussian standard deviation, in pixels.
- amount The larger the sharpening amount value, the more obvious the edge areas in the image.
Recommended value: 0.5 ~ 1.5 - threshold Defines a threshold for pixel difference. Pixels below the threshold will be ignored. The smaller this value is, the more pixels will be sharpened.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { float radius = 0; float sigma = 1; float amount = 1; float threshold = 0; obj.UnsharpMask(radius, sigma, amount, threshold); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Radius: 0; Sigma: 1; Amount: 1; Threshold: 0 |
|
|
Why Unsharp Mask will improve barcode reading performance?
Sharpening the edges in the image can highlight the boundaries between Bars and Spaces, improving the recognition effect.
Sharpening also increases noise at the same time. An appropriate value should be selected to balance noise and the effective signal (Bar boundaries).
Despeckle
Reduce speckle noise in the image.
Why Despeckle will improve barcode reading performance?
Reduce speckle noise in the image.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { obj.Despeckle(); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Result |
|
|
Why Despeckle will improve barcode reading performance?
Reduce speckle noise in the image.
Edge
Highlight the edges of shapes in the image. Mainly suitable for grayscale images.
Why Edge will improve barcode reading performance?
Use different radius values to detect edges in the barcode image. Choosing an appropriate radius value can reduce noise while highlighting the boundaries between Bars and Spaces.
- radius Filter radius, in pixels. Valid values: greater than 0.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { int radius = 5; obj.Edge(radius); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Radius: 5 |
|
|
| Radius: 10 | Radius: 20 |
|
|
Why Edge will improve barcode reading performance?
Use different radius values to detect edges in the barcode image. Choosing an appropriate radius value can reduce noise while highlighting the boundaries between Bars and Spaces.
Morphological Processing
Morphological Processing includes the following functions
- Erode
- Dilate
Erode
Apply morphological erosion to the image.
Why Erode will improve barcode reading performance?
Mainly used to improve the situation where bars in the image are contaminated by surrounding spaces. Expanding the bar areas can correct the width ratio of bars and spaces, thereby improving barcode recognition performance.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { obj.Erode(); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Result |
|
|
Why Erode will improve barcode reading performance?
Mainly used to improve the situation where bars in the image are contaminated by surrounding spaces. Expanding the bar areas can correct the width ratio of bars and spaces, thereby improving barcode recognition performance.
Dilate
Apply morphological dilation to the image.
Why Dilate will improve barcode reading performance?
Mainly used to improve the situation where the Bars in the image have invaded the surrounding Spaces.
Shrinking the Bar areas can correct the width ratio between Bars and Spaces, thus improving the recognition effect.
using (ImageProcessor obj = ImageProcessor.FromFile(Path.Combine(Root, "Source.png"))) { obj.Dilate(); obj.SaveToFileAsPNG(Path.Combine(Root, "Result.png")); }
| Source image | Result |
|
|
Why Dilate will improve barcode reading performance?
Mainly used to improve the situation where the Bars in the image have invaded the surrounding Spaces.
Shrinking the Bar areas can correct the width ratio between Bars and Spaces, thus improving the recognition effect.
