DEV Community

loading...
Cover image for 3 Image Processing Techniques For Building Enterprise Level 1D & 2D Barcode Scanning Software

3 Image Processing Techniques For Building Enterprise Level 1D & 2D Barcode Scanning Software

Matt Payne
Machine learning and data science consultant @ Scalr.ai
・5 min read

Image processing and computer vision techniques we’ve used to build high end custom barcode scanning software along with label readers for small business automation.

Barcode scanning and reading is a sector of computer vision problems that rarely has a straightforward and direct solution. Lots of things including brightness, image clarity, image contrast, and barcode length relative to the image can cause your software to have issues that are tricky to solve. Many industries can see insane cost and labor hours reductions by using these same techniques to build automation systems for your business. We’ve compiled a few techniques we’ve seen great success with to build custom solutions for pretty noisy images and videos. If you want to see a real world business tool using these techniques and getting results, check out this case study where we used barcode scanning software and computer vision to completely automate a backend system for an online retailer (along with other ML tools).

Extracting Horizontal & Extreme Vertical Lines With Morphology

extracting horizontal lines

When reading noisy 1D barcodes off images, this might be the most important technique available. By using the erode and dilate morphology operations, we can simply remove any large horizontal lines from the image, which is very useful for lines around the barcode or lines that run into the barcode from the sides. You can see a nice example with images and code here.

Example Using Musical Notes - Orginal Image
Muscial Notes

Horizontal Lines detected by OpenCV Library
Alt Text

Lines removed, image does have gaps after removing (not shown), but we can use a vertical kernel function to repair the lines
Alt Text

On top of this, you can create a “vertical structuring element”, which defines the min or max size for a vertical line to be removed from an image as well. This is an important step, as it allows us to remove large vertical lines around the barcode, which in certain noisy images could be detected as part of the barcode. In the image above we can see a few examples of vertical space that could be confused as part of the barcode at certain sideways angles, especially the middle barcode with perforated space next to it. OpenCV has a great example of removing these lines using morphology operations here.

This image processing technique has worked wonders for us when building systems for automating warehouses and the supply chain industry. Labels on inventory, like boxes and building supplies that contain a barcode can be difficult to read because of the amount of information that supply labels can contain.

Removing Colors BEFORE Grayscaling Images

One of the early steps many add to their 1d & 2d barcode reading software is grayscaling the image, in the idea of removing colors to make the barcode easier to read. While the thought process for this isn’t that bad, grayscaling the image in the beginning is not a good idea. Barcodes are black, and sometimes a dark gray, which means they are already one of if not the darkest objects in images in videos. What can happen is colors like dark red or dark blue around the barcode can be grayscaled into the same color as the barcode, causing huge issues when trying to detect the bounds of the real barcode. What can happen is the dark colors will change to black or dark grey and now be impossible to tell apart from the barcode itself, especially if trying to draw bounding boxes around barcodes.

Instead of this, let's remove the colors before manipulating the image:

Alt Text

In this example from one of our projects, we are looking to remove the color red by converting it to white from a Pillow Image object that gets passed in. By comparing the values of the r,g,b to the range of values for the color red, it allows us to remove any shade that could be in the image. We’ve seen this concept work especially great when building barcode systems for inventory management, given you can remove the product color that surrounds the barcode.

Use OCR to Read any Numbered Barcode

Alt Text

Testing your barcode reading software against many different labels and angles is the best way to ensure you reach results with high correctness. One of the things we noticed during quality testing was we had more frequent errors with barcodes where the label exists in an outdoor or construction environment. Barcodes become faded, ripped, and torn which makes it impossible to read, even if we have a good process for isolating the barcode.
The strategy we’ve seen work to improve these testing results is using OCR to see if we can identify and read the barcode number off a label instead. Many times if we can identify that a barcode exists but we are just unable to read it, it is possible to identify the barcode number and process it through a barcode library.

Alt Text

While OCR algorithms will usually take longer than most barcode libraries, this can be a great last step alternative when you simply cannot read the barcode. The hidden bonus to this OCR implementation is it integrates nicely with any larger scale computer vision for automation systems, which is normally how barcode and QR code scanning systems are used as a piece of the whole puzzle. If you’re thinking about building a tool for complete automating with computer vision, both of these algorithms will probably be a part of your process.

Final Tips & Tricks For Better Barcode Software

Something useful to keep in mind if you or your team is struggling with getting A+ results when testing your new tools, understand the importance of normalizing the viewing of a barcode. The more noise you can remove without overfitting your reader the higher chance you’ll have of being successful. Sometimes you can crop closer around the barcode before reading it, using an object detection library before you read, allowing that barcode to be a higher percentage of the image and removing most of the noise. Sometimes you’ll just find your barcode is simply too small relative to the image and can cause problems differentiating the lines or squares of the barcode.

Using Your Barcode Reader In Other Spaces

Alt Text
One of the more interesting benefits to building a tool like this is the data you can capture alongside the software running. Creating systems to grab data from your barcode scanner can result in you collecting higher-level data, or data built that takes in multiple lower-level data points to be created. Normally this data is difficult to generate and find, which means it can produce pretty high ROIs for other systems. Given this data is generated from models your competition might not have, it can be used easily to gain an advantage. These data capture services are normally simple to build and can produce great returns down the line.

Hopefully, you find some of these techniques useful in your journey to building the best barcode scanning and detection software you can. We’ve used these same steps to build custom 1d & 2d barcode reading software for small businesses, everything from online retailers to warehousing and supply companies, so we know this can work for you too.

Discussion (0)