# Identification of a given barcode as either 1 D or 2 D

I am working on an algorithm (in MATLAB) to identify/classify a given bar code (image) as 1 D or 2 D. I have considered using the contrast of THE IMAGE matrix as a parameter or using the FFT of the image.

I would be grateful for any ideas or suggestions on what you consider the best approach for this problem

• Why the FFT? What do you mean by image matrix? You need to derive the binary code from the image. White could be 1 and black could be 0.
– Moti
May 28 '15 at 5:15
• What do you mean by 1D or 2D ? Like normal bar code vs QR code (the only 2D bar code I could think of) ? May 28 '15 at 13:11
• @Moti I did derive the binary code from the image May 29 '15 at 15:49
• @Loufylouf Yes exactly. The names mentioned by you are a few examples in each category May 29 '15 at 15:50
• Does you mean to say you solved your challenge?
– Moti
May 30 '15 at 5:36

I think this is a fairly simple problem. Just compute the absolute gradient histogram (you, probably, don't even need to normalize). If it is concentrated around one angle, this should indicate a binary code, whereas more or less equally distributed histograms should be the opposite. Of course, this approach assumes that you always see barcode images, i.e. no negative class.

Ok, so I did a sample implementation for you, considering the above algorithm I described. Of course you could always make it better, but for the real-life images I tested, it gives reasonable results:

function [type] = classify_barcode_type(I, mingrad)

% convert to grayscale
if (size(I,3)>1)
I = rgb2gray(I);
end

% smooth the image
I = imgaussfilt(I, 2);

% upside down are the same (180 deg. range)
gdir = mod(Gdir+180,180);

% threshold the gradients: reduces noise

% 'relMag' percent of the gradients are pointing towards a single dir.

if (relMag<0.05)
type = 'qr';
else
type = 'linear';
end

end


Sample images for an Aztec code and Code 128 barcode are as follows:

You could use the MATLAB function as follows:

% read the image

% apply custom rotation for robustness test
imgr=imrotate(img,125);

% classify
[type] = classify_1d_barcode(imgr, 40)


Note that, even though rotation creates many artificial edges, the approach remains to be robust, even in presence of severe rotations. In real life, you won't have the synthetic edges appearing due to rotations.

The figures below are plots of normalized gradient histograms ($\frac{\mathbf{N}}{\lVert \mathbf{N} \rVert}$ - also in the code). Note the difference between the aztec (QR) code and the linear barcode:

Note that linear barcode is rotated by 90 deg and this is clearly visible in the normalized angle histogram plot. You can get the rotation angle this way. Even though this example was for 90 deg rotation, you will get the same / similar results with any other rotation - theoretically the same results actually. Threshold (or the decision boundary) is indicated by the green line.

• Thanks @tbirdal for the suggestion. Doesn't HOG essentially find the local gradients in an image according to the cell size? I am unsure as to what you mean by "concentrated around an angle". Also, will this method work if the barcodes are arbitrarily rotated by some angle? Jul 28 '15 at 20:44
• Dominant angle means the orientation, which is mostly observed over the image patch. Rotation will only shift the position (histogram bin) of this angle, and you would also be able to obtain the barcode rotation for free. Jul 28 '15 at 20:52
• Thanks again @tbirdal. So I am implementing this in MATLAB and here is a link to what I have done/ followed. ( Example 1 ) mathworks.com/help/vision/ref/extracthogfeatures.html#btypp_7 So, essentially now I need to find the peak or the maximally observed gradient angle and will that be the dominant angle? Jul 31 '15 at 18:31
• Updated my reply with a code. It will be easier for you to grasp the idea. Jul 31 '15 at 21:37
• Thanks a lot @tbirdal for the in depth implementation details!! Massively helpful and yes it was fairly accurate with my data set as well. The values of 40 for (thresholding Gmag) as well as 0.05 for classification are data set dependent? Thank you once again for your time and effort! Aug 3 '15 at 17:07

One dimensional barcodes have distinct "texture" differences than two dimensional ones.

In general, linear barcodes are smooth in the Y direction but use their width in the X direction to encode information and that is where the differences in texture stem from. Two dimensional barcodes (e.g. QR Codes) use both directions to encode information.

To assess differences in texture, you can train a classifier using Haralick's features that directly estimate differences in texture information via different metrics of a co-occurence matrix derived by each image.

For more information please this, this , this (for a Python library that implements Haralick features) and also this relevant response.

Hope this helps.

• Thanks for the input @A_A, I will try implementing your suggestion. At the moment, I am working with gradient measures along the x and y axes and that seems to be a differentiating factor. May 29 '15 at 15:52