Image processing and manipulation techniques we’ve used to build high end custom barcode scanning software for small business automation. 

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 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 for use cases such as product matching, UPC code extraction, barcode scanning, and many more.

Extracting Horizontal & Extreme Vertical Lines With Morphology

When reading noisey 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.  

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 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.

Use OCR to Read any Numbered Barcode

Testing your barcode reading software against many different labels and angles is the best way to ensure 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. With Intelligent OCR, it’s making this process happen even quicker.

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

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. 

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.