Before you go, check out these stories!

0
Hackernoon logoBarcode image segmentation by@Pcc

Barcode image segmentation

Author profile picture

@PccLuca Piccinelli

I am a programmer. I love programming. Any language, any paradigm

Questo articolo è dispobinile anche in italiano

In this article i’m going to explain a simple technique for barcode segmentation from images. For the example code, i’m going to adopt Python 2.7 in its Anaconda incarnation and OpenCV as image processing library. I choose Python as it is a very practical choice in order to quickly write examples, however the technique can be adopted in any language of your choice. Anaconda is an interesting Python distribution that bundles a series of usefull tools for scientific programming. It’s like a matlab for “poor people” :).

Segmentation is inteded as the process of identifying the position of one or more objects inside an image.

The technique that i’m going to present is very simple; it makes use of morphological operators dilation and erosion and combinations as opening, closing and black-hat operators. The exploited barcode feature is than the close distance between barcode bars.

Once Anaconda is installed, let’s move to OpenCv installation with the following command:

conda install -c https://conda.anaconda.org/menpo opencv

from Anaconda prompt.

And now let’s launch the Spyder IDE from the Anaconda launcher

Anaconda launcher

Once you have Spyder running, i suggest to verify that opencv installation have succeded: from the bottom right IPython console, let’s test with

import cv2

Code and technique

I have created a startup github repository; you can clone it with:

git clone --branch step1 https://github.com/lucapiccinelli/BarcodesTutorial.git

with this, you will download the test image and the starting code that reads and shows the image

Test image

From now on, it will start the real processing. At first we are going to binarize the image, possibly enhancing the more interesting features. With blackhat operator we can enhance the darkest image elements.

Now we can safely binarize the image with a simple global threshold: blackhat operator let us use a very low threshold value, without giving to much emphasis to the noise.

When applying blackhat, we use a kernel that gives more importance to vertical image elements. The kernel has a fixed size, so the image is scaled achieving also performance improvement (and favoring some kind of input normalization).

blackhat + thresholding

It follows the adoption of the others morphological operators, sequentially combining them in order to obtain connected components in barcode positions.

This combination of dilation and closing works very well with the test image, however it could happen that it doesn’t achieve the same effectiveness on other images; no problem, you can try to variate the parameters and combination of operators until you are satisfied of the results.

dilation + closing

Last preprocessing step is to apply an opening operator with a very big kernel, in order to remove the elements that are to little to fit a barcode shape.

And here you are the final result

opening with a 35x21 kernel

Now we can run the connected components detection algorithm and retrieve barcodes rectangles, with coordinates and dimensions. As you can see in the previous image, some noise has not been filtered from the last morphological step; however in this case it is quite simple to filter them out, thresholding on rectangle area value.

Finally, in the above code i use extracted rectangles to draw them overlayed on the original image.

Risultato finale con i codici a barre evidenziati da riquadri verdi

Conclusions

The presented technique is very simple and effective, however it presents some annoying drawbacks:

  • it is very sensitive on barcode skewness; it works well until about 45 degrees, then you have to permorm a second pass, modifying kernels direction;
  • it can find barcodes only inside a fixed range of dimensions;
  • despite the filtering imposed on rectangles areas, it is not unusual that some non barcode is not cleaned out;

It could be that the first and the second are not real problems, depending on application context, however the last one is quite more an annoying problem, as you can spend very much cpu time trying to decode something that is not a barcode.

A good solution is to instruct an artificial neural network (or some other classifier that you like) on barcode features (image gradient, fourier transform) and filter out the noise in a second moment. Maybe the next article will be on this topic :).

Material

Here it follows the complete example code.

Here it is the github repository. In the “img” directory i put all the images of the intermediate steps.

That’s all folks. I hope to have succeeded in the explanation of the technique: if not, please let me know.

Questions, reviews, comments or any kind of feedback will be very appreciated.

Thank you for reading :).

Bye!

Author profile picture

@PccLuca Piccinelli

Read my stories

I am a programmer. I love programming. Any language, any paradigm

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.