Hackernoon logo3 Image Processing Techniques For Building Barcode Scanning Software by@scalrconsulting

3 Image Processing Techniques For Building Barcode Scanning Software

Scalr.ai Hacker Noon profile picture

@scalrconsultingScalr.ai

Machine Learning and Data Science Consulting firm focused on increasing revenue for clients.

Barcode processing 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 reductions by using these same techniques to build automation systems.

Image Processing Techniques For Building Barcode Scanning Software

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, check out this case study where we used barcode reading software to completely automate a backend system for an online retailer (along with other ML tools).


1. Extracting Horizontal & Extreme Vertical Lines With Morphology

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 - Original Image

Horizontal Lines detected by OpenCV Library

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

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.

2. Removing Colors BEFORE Grayscaling Images

One of the early steps many add to their 1d & 2d barcode reading software is grayscaling the image: 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 a 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:

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.

3. Use OCR to Read any Barcode Numbers

Testing your barcode reading software against many different labels and angles is the best way to insure you reach results with a 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.

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

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.  

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.  

Work With Scalr

Are you a business that has a few warehouses or complex inventory (like e commerce) that's hard to keep track of?

Want an automated solution that’s completely customized for you, not some giant corporate solution that costs a fortune?

Do what other businesses do: https://www.scalr.ai/contact

Let’s talk about our custom inventory management systems using labels & barcodes.

A little bit about Scalr.ai:

We are a machine learning and data science consulting firm focused completely on building business tools to increase profitability for clients. We specialize in natural language and computer vision systems that allow us to build software solutions for many different industries

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.