Barcode digit detection and recognition algorithm based on image morphology processing and template matching

Table of contents

1. Barcode digital detection

2. Barcode digital recognition

3. Part of the core program

Four, MATLAB simulation results


        The barcode number detection and recognition algorithm based on image morphology processing and template matching is an algorithm for automatically identifying numbers in barcodes. The algorithm mainly consists of two steps: detection and recognition. Barcode detection is a technical process through which it is possible to determine whether the barcode symbol conforms to the symbol specification. For example, in a specific application, the parameters of the barcode symbol should follow the requirements of the application standard. Code 128 is an example. In order to apply the code in its system, the Uniform Code Committee and the International Article Numbering Association have made further regulations on the format, length and data content structure of the code. For the detection of UCC/EAN-128 code, not only to see whether it conforms to the symbol specification of 128 code, but also its length and other parameters should meet the requirements of EAN-UCC.

1. Barcode digital detection

image preprocessing

        In images, barcode numbers are often disturbed by noise, shadows, uneven lighting, etc., so preprocessing is required before number detection. Preprocessing typically includes filtering, smoothing, contrast enhancement, etc. to better extract barcode digits.

Binarization

        In order to distinguish the numbers in the barcode more clearly, we need to binarize the image. Binarization divides the pixel values ​​in an image into two levels: black and white. In barcode images, the colors of stripes and blanks are usually quite different, so they can be clearly distinguished by binarization.

Morphological processing

        Morphological processing is a method used in image analysis where it can be used to remove noise, connect disconnected lines, etc. In barcode digit detection, we usually use morphological erosion and dilation operations to process binarized images. Erosion removes fine noise from an image, while dilation connects broken lines.

template matching

       Template matching is a search algorithm based on image similarity, which is used to find similar regions in the target image to the template image. In barcode number detection, we can make each number into a template, and then use template matching to find regions similar to the template in the binarized image. The similar area found is the barcode number we want to detect.

2. Barcode digital recognition

feature extraction

       Before recognizing barcode digits, we need to extract features from the detected digits. There are many methods of feature extraction, such as method of moments, wavelet transform, etc. In barcode digit recognition, we usually use shape-based features such as width, length, area, etc.

classifier design

        A classifier is an algorithm for classification, which can classify based on the features of the input. In barcode digit recognition, we usually use classifiers such as neural networks and support vector machines. These classifiers can automatically learn and recognize different types of features, so as to realize the automatic recognition of barcode numbers.

Classification and Identification

       Finally, we input the extracted features into a classifier for classification and recognition. The classifier will classify according to the features and output the recognition result. If the output result is inconsistent with the actual result, it is necessary to adjust the classifier or redesign the feature extraction method to improve the recognition accuracy.

        To sum up, the barcode digit detection and recognition algorithm based on image morphology processing and template matching is an effective automatic recognition method. The algorithm realizes the detection of barcode numbers through preprocessing, binarization processing, morphological processing, template matching and other steps, and realizes the recognition of numbers through the steps of feature extraction and classifier design. The algorithm has high accuracy and robustness, and can be widely used in the field of barcode recognition.

3. Part of the core program

..........................................................................
temp = a_hist(max_1);
a_hist(max_1) = 0;
k = 0;
for i=1:n
    if k<a_hist(hist_max(i))
        k = a_hist(hist_max(i));
        max_2 = hist_max(i);
    end
end
a_hist(max_1) = temp;
if max_1>max_2
    k = max_1;
    max_1 = max_2;
    max_2 = k;
end
T = max_1;
k = a_hist(max_1);
for i=max_1:max_2
    if k>a_hist(i)
        k = a_hist(i);
        T = i;
    end
end
[m,n] = size(bar_Gray); %求灰度图的大小
for i=1:m        %对图像进行二值化处理
    for j=1:n
        if bar_Gray(i,j)>T    %选择适当的阈值进行二值化处理
            bar_10(i,j) = 1;
        else
            bar_10(i,j) = 0;
        end
    end
end
%imshow(bar_10);
l = 0;      %检测59根条形码
for i=1:m
    k = 1;
    l = l+1;
    for j=1:n-1
        if bar_10(i,j)~=bar_10(i,j+1)   %比较同一行相邻两点的颜色是否一致
            %bar_x(l,k) = i;
            bar_y(l,k) = j; %记录转折点的纵坐标
            k = k+1;    %准备记录下一个数据点
        end
        if k>61 %点数大于60,该行应该删掉
            l = l-1;
            break
        end
    end
    if k<61 %点数小于60,该行应该删掉
        l = l-1;
    end
end
[m,n] = size(bar_y);
if m<=1 %查看条形码是否有效
    code = '0';
    fprintf(1,'GameOver!\n');
    return
end
for i=1:m            %计算每根条形码的宽度
    for j=1:n-1
        bar_num(i,j) = bar_y(i,j+1) - bar_y(i,j);
        if bar_num(i,j)<0
            bar_num(i,j) = 0;
        end
    end
end
bar_sum = sum(bar_num)/m;   %求每根条形码宽度的平均值
k = 0;
for i=1:59   %计算59根条形码的总宽度
    k = k + bar_sum(i);
end
k = k/95;   %计算单位条形码的宽度
for i=1:59  %计算每根条形码所占位数
    bar_int(i) = round(bar_sum(i)/k);
end
k = 1;
for i=1:59  %将条形码转换成二进制数
    if rem(i,2)
        for j=1:bar_int(i)  %黑色条用1表示
            bar_01(k) = 1;
            k = k+1;
        end
    else
        for j=1:bar_int(i)  %白色条用0表示
            bar_01(k) = 0;
            k = k+1;
        end
    end
end
if ((bar_01(1)&&~bar_01(2)&&bar_01(3))...   %判断起始符是否正确
        &&(~bar_01(46)&&bar_01(47)&&~bar_01(48)&&bar_01(49)&&~bar_01(50))...    %判断中间分隔符是否正确
        &&(bar_01(95)&&~bar_01(94)&&bar_01(93)))    %判断终止符是否正确
    l = 1;
    for i=1:6   %将左侧42位二进制数转换为十进制数
        bar_left(l) = 0;
        for k=1:7
            bar_left(l) = bar_left(l)+bar_01(7*(i-1)+k+3)*(2^(7-k));
        end
        l = l+1;
    end
    l = 1;
    for i=1:6   %将右侧42位二进制数转换为十进制数
        bar_right(l) = 0;
        for k=1:7
            bar_right(l) = bar_right(l)+bar_01(7*(i+6)+k+1)*(2^(7-k));
            k = k-1;
        end
        l = l+1;
    end
end
num_bar = '';
num_first = 0;
................................................................
up2197

Four, MATLAB simulation results

Guess you like

Origin blog.csdn.net/ccsss22/article/details/132675216