Search code examples
matlabimage-processingbarcode

Calculate width of each bar of Barcode in Matlab


I have a barcode and I would like process it in matlab and calculate the width of each bar in a 1-D barcode in pixels.

I have tried converting the image to gray scale through graythresh level and converted it to binary as well.

%read the image code3
barz=imread('barcode1.jpg');
grayBarz=rgb2gray(barz);

binImage = imbinarize(barz,graythresh(barz));

s = regionprops(binImage == 0,'Area','PixelIdxList');
imshow(barz);

I want the width in pixels of each bar in the barcode.

Barcode Image


Solution

  • Sometime it is fun to be able to do things without needing the full image processing toolbox.

    The solution below allows you to count the pixel width of every black bar, without needing any additional toolbox:

    %% Read the image
    barz=imread('barcode.jpg');
    grayBarz=rgb2gray(barz);
    
    %% Extract an horizontal line in the middle
    sz = size(grayBarz) ;
    idxMidLine = round(sz(1)/2) ; % index of a line roughly in the middle
    eline = grayBarz(idxMidLine,:) ;    % extract a line
    eline(eline<128) = 0 ;              % sharpen transitions
    eline = ~logical(eline) ;           % convert to logical (0=white / 1=black)
    
    %% Now count the pixels
    npts = numel(eline) ;   % number of points in the line
    
    % Find every transition:
        % high to low   => -1
        % no change     =>  0
        % low to high   => +1
    idd = find( diff(eline) ) ;
    
    % this contain the start and end indices of every interval
    ddd = [ 1 , idd ; ...
            idd , npts ] ;
    
    % This contains the width of every bar (white and black),
    % in order from left to right
    barWidth = diff(ddd) ;
    
    if ~eline(1)
        % The first interval is 0 (is white)
        pixBarWhite = barWidth( 1:2:end ) ;
        pixBarBlack = barWidth( 2:2:end ) ;
    else
        % The first interval is 1 (is black)
        pixBarBlack = barWidth( 1:2:end ) ;
        pixBarWhite = barWidth( 2:2:end ) ;
    end
    
    nBarWhite = numel(pixBarWhite) ;
    nBarBlack = numel(pixBarBlack) ;
    
    %% Display results
    fprintf('Found a total of %d black pixels along the horizontal,\n',sum(pixBarBlack))
    fprintf('spread over %d black bars,\n',nBarBlack)
    fprintf('Individual bar pixel thickness:\n')
    for k=1:nBarBlack
        fprintf('Bar %02d : Thickness: %02d pixels\n',k,pixBarBlack(k))
    end
    

    For your image it will return:

    Found a total of 599 black pixels along the horizontal,
    spread over 49 black bars,
    Individual bar pixel thinchness:,
    Bar 01 : Thickness: 13 pixels
    Bar 02 : Thickness: 07 pixels
    Bar 03 : Thickness: 20 pixels
    % [edited to keep it short]
    Bar 47 : Thickness: 20 pixels
    Bar 48 : Thickness: 07 pixels
    Bar 49 : Thickness: 13 pixels
    

    Note that the variable pixBarWhite also contain the pixel thickness of all the white intervals between the black bars. It might come handy for later ...