Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Barcode Recognition

by avo (Pilgrim)
on Aug 26, 2008 at 14:05 UTC ( [id://706909]=perlquestion: print w/replies, xml ) Need Help??

avo has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I am challenged with the following task and have being looking around for a solution preferably in Perl or via a command line utility and it must work on Windows and Linux. I have a large amount of bar code scans. They are all gray scale image from a scanner (8 bit). I need to write a program to recognize the bar code number in these scans. I have being thinking of implementing either an algorithm in Perl or using a free bar code command line tool which I call from my Perl program. Can anyone help please. Any input is highly appreciated.

UPDATE
I found this interesting demo of something very much similar to what I am looking for but done in Flash / Actionscript.

Replies are listed 'Best First'.
Re: Barcode Recognition
by themage (Friar) on Aug 26, 2008 at 15:07 UTC
    Se simpler way to go, probably, was to print the images you have and use a keyboard connectable scanner to parse the barcodes you have.

    But if you want an alternative to that, you will need to:
    1. Parse the Image and transform it in a representation you can easly parse, for example, 0 for a white bar, 1 for a black bar and L for a long bar.
    2. For most bar encodings (and specificly for EAN13) you can remove the initial and final "L0L" and the middle "0L0L0".
    3. Now you need to convert each group of 7 "0" and "1" to a number.
    4. You will need to guess which is the first digit, as it is not encoded in the barcode, but you can find out which it is by comparing the sequence of encondings used in the bar code.

    Note: You need to use the width of the long bar (not it's height) to know if a bar is a single binary digit or more than one. A slim bar is a digit, a large on is two, or even three or four digits. For example, 6 using L-Code have 4 consecutive "1" digits (black bars) and using G or R-Code have the same 4 but "0" digits (white bars).

    More info at wikipedia.

Re: Barcode Recognition
by jethro (Monsignor) on Aug 26, 2008 at 15:20 UTC

    Depends on the quality of the scans. If you have low-res scans with lots of artefacts and other graphic or text around the bar code and the bar code tilted, then you have a major project on your hand. If possible find a free bar code scan utility. Also there lots of different bar code standards, if you need to parse more than one it gets really difficult

    If you have non of the above problems, you might do something like this (this is theoretical, never done this in practice):

    1) Store lets say 20 or 40 horizontal lines from the vertical middle of the image bitmap to an array of arrays

    2) Find out the blackest and the whitest value from a block in the horizontal middle of these lines and subtract some error margin to get the thresholds or simple take the mean value as transition value

    3) Calculate for each of these lines black-to-white and white-to-black transitions based on the thresholds or the transition value

    4) Average those transitions that occur in more than 80% of the lines at the same horizontal place plus minus an error value

    5) Collect all the widths of black lines (difference of the white-to-black to the next black-to-white transition). You should get values the raster of the bar code out of this after some averaging.

    6) Take the first white-to-black transition as starting point. If you find the start code, good. If it doesn't work out, take the next white-to-black transition,...

Re: Barcode Recognition
by pjotrik (Friar) on Aug 26, 2008 at 15:58 UTC
Re: Barcode Recognition
by dHarry (Abbot) on Aug 26, 2008 at 21:10 UTC

    I am challenged with the following task and have being looking around for a solution preferably in Perl or via a command line utility and it must work on Windows and Linux.

    I don’t know about a Perl solution for this, only for the other way around, i.e. * creating * the bar code.

    Your best bet is to find a C/C++ program that you can run on both Windows and Linux. The suggestion of brother pjotrik: http://zebra.sourceforge.net/ is a good starting point. Otherwise you will have to spend some money on it. There are many commercial alternatives available.

    But first you must find out exactly what kind of barcode is being used. If it’s a 1D barcode there are at least 20 different ones!

    In general the barcode, in your case the quality of the scans, has to meet certain specifications otherwise it cannot be scanned at all. Normally logistic companies (like GLS for which I worked) have some sort of certification process put in place to accept barcodes from customers. How good are your scans? I don't know where you live but it might be simple to check by going to the supermarket and try to scan it there.

Re: Barcode Recognition
by Gavin (Archbishop) on Aug 26, 2008 at 16:04 UTC

    Don't all bar codes have numbers as well as horizontal lines, they seem to in the UK, can't you use (OCR) Optical reading software with a scanner.

      How is OCR easier than recognizing bar codes? This seems like transforming the problem into a more difficult problem in the same problem domain (image recognition). Bar codes were developed to avoid OCR!


      Perl reduces RSI - it saves typing
Re: Barcode Recognition
by johndageek (Hermit) on Aug 26, 2008 at 21:02 UTC
    A little more information would be helpful.

    Are all the barcodes the same encoding?
    Are all of the images the same direction?
    What is the file type used to store the grayscale image?
    Are all of the files of the same type (.jpg, tiff, png etc?)
    Are the images clear / consistant between light and dark?

    Are the original paper copies still in existance?
    How many (approx) of these bar codes do you have? (hundreds, thousands, millions?

    Enjoy!
    Dageek

      All barcodes are EAN-13, they are not the same direction, random angle some of them also upside down. They are all stored in PNG and TIFF (mostly 8bit) files. The images are not the same lightness, some of them are darker, also some of them are noisier. Some of them are not entirely focused - the scanning perhaps was not done right. I am talking of thousands of images.
Re: Barcode Recognition
by rpnoble419 (Pilgrim) on Aug 26, 2008 at 22:55 UTC
    I use clearImage to process my bar code data on Windows. I have used perl and ClearImage for about 4 years now and have had no problems. http://www.inliteresearch.com/ On Windows I use Win32::API to load the dll into my workspace.
      Hi, This is something of interest to me .Can you please provide little more details? Thanks N

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://706909]
Approved by Corion
Front-paged by psini
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-18 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found