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

  • 1
    $\begingroup$ 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. $\endgroup$
    – Moti
    May 28 '15 at 5:15
  • $\begingroup$ What do you mean by 1D or 2D ? Like normal bar code vs QR code (the only 2D bar code I could think of) ? $\endgroup$
    – Loufylouf
    May 28 '15 at 13:11
  • $\begingroup$ @Moti I did derive the binary code from the image $\endgroup$ May 29 '15 at 15:49
  • $\begingroup$ @Loufylouf Yes exactly. The names mentioned by you are a few examples in each category $\endgroup$ May 29 '15 at 15:50
  • $\begingroup$ Does you mean to say you solved your challenge? $\endgroup$
    – 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);

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

% compute sobel gradients
[Gmag,Gdir] = imgradient(I);

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

% threshold the gradients: reduces noise
gradSelect = find(Gmag>mingrad);
numGrad = length(gradSelect);

% histogram of gradients
[N, ~] = histcounts(gdir(gradSelect), 180);

% 'relMag' percent of the gradients are pointing towards a single dir.
relMag = max(N)./numGrad;

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


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

enter image description here enter image description here

You could use the MATLAB function as follows:

% read the image
img = imread('real6s.png');

% apply custom rotation for robustness test

% 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:

enter image description here

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.

  • $\begingroup$ 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? $\endgroup$ Jul 28 '15 at 20:44
  • $\begingroup$ 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. $\endgroup$ Jul 28 '15 at 20:52
  • $\begingroup$ 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? $\endgroup$ Jul 31 '15 at 18:31
  • $\begingroup$ Updated my reply with a code. It will be easier for you to grasp the idea. $\endgroup$ Jul 31 '15 at 21:37
  • $\begingroup$ 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! $\endgroup$ 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.

  • $\begingroup$ 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. $\endgroup$ May 29 '15 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.