Barcode angle correction algorithm based on Otsu algorithm

Table of contents

1. Otsu calculation overview

2. Barcode angle correction through Otsu algorithm

3.matlab core program

4. Algorithm test results


        The barcode angle correction algorithm based on the Otsu algorithm is a method for automatically identifying and correcting the angle of the barcode. This method combines image processing and machine learning technology to find the angle of the barcode and correct it by binarizing and thresholding the image.

1. Otsu calculation overview

       The Otsu algorithm is an adaptive threshold segmentation method used to convert grayscale images into binary images. This algorithm finds the optimal threshold by calculating the inter-class variance to maximize the distinction between two classes of pixels (foreground and background).

       In the barcode angle correction algorithm based on the Otsu algorithm, we first need to preprocess the image, including grayscale and noise reduction operations. Then, we use the Otsu algorithm to binarize the image to separate the barcode area and the background area.

       Next, we need to find the angle of the barcode. This can be achieved by calculating the projections in all possible straight directions in the image. Specifically, we can divide the image into multiple small rectangular areas and count the number of white pixels in each rectangular area. We then take these quantities as the projection values ​​of this rectangular area in the corresponding straight line direction.

       We can find the angle of the barcode by calculating the variance of the projected values ​​in all possible straight line directions. Specifically, we can divide the projection values ​​into two groups, one is the projection value of the barcode area, and the other is the projection value of the background area. We then calculate the variance of these two sets of projection values ​​and find the direction of the line that maximizes the variance, which is the angle of the barcode.

      Finally, we need to perform operations such as rotating and translating the image according to the found angle to correct the barcode to a horizontal orientation. This can be achieved by using rotation and translation functions from image processing libraries.

The inter-class variance calculation formula of Otsu algorithm:

σ^2_B = w_0 * w_1 * (μ_0 - μ_1)^2

Among them, w_0 and w_1 are the weights of the foreground and background pixels respectively, and μ_0 and μ_1 are the average gray values ​​of the foreground and background pixels respectively.

The formula for calculating the projection value:

P(θ) = ∑_{x,y} I(x,y) * R(x cosθ + y sinθ, -x sinθ + y cosθ)

Among them, I(x,y) is the gray value of the image, R(x,y) is the indicator function of a rectangular area, and θ is the angle in the straight line direction.

The formula for calculating variance:

Var(P) = ∑_{i=1}^{N} (P_i - μ)^2 / N

where P_i is the projected value, μ is the average of the projected values, and N is the number of projected values.

2. Barcode angle correction through Otsu algorithm

The implementation of barcode angle correction through the Otsu algorithm mainly includes the following steps:

  1. Preprocessing: First, preprocess the input barcode image, including grayscale and noise reduction operations. Grayscale can convert color images into grayscale images, and noise reduction can remove noise and interference from images.
  2. Binarization: Use the Otsu algorithm to binarize the preprocessed image to separate the barcode area and the background area. The Otsu algorithm finds the optimal threshold by calculating the inter-class variance to maximize the distinction between two types of pixels (foreground and background). In the binarized image, the barcode area is white and the background area is black.
  3. Projection calculation: Divide the binarized image into multiple small rectangular areas, and calculate the number of white pixels in each rectangular area. These quantities are then taken as the projection values ​​of the rectangular area in the corresponding straight line direction. By calculating the projection values ​​in all possible straight line directions, a set of projection value sequences can be obtained.
  4. Angle determination: Calculate the variance of the projection value sequence and find the straight line direction that maximizes the variance, which is the angle of the barcode. Specifically, the projection value sequence can be divided into two groups, one group is the projection value of the barcode area, and the other group is the projection value of the background area. Then, calculate the variance of these two sets of projection values, and find the straight line direction that maximizes the variance, which is the angle of the barcode.
  5. Image correction: Perform operations such as rotation and translation on the image according to the found angle to correct the barcode to a horizontal orientation. This can be achieved by using rotation and translation functions from image processing libraries. In the corrected image, the barcode area is placed horizontally to facilitate subsequent decoding operations.

       It should be noted that in practical applications, the algorithm may need to be optimized and improved to adapt to different barcode types and image qualities. In addition, for barcodes with a large tilt angle, it may be necessary to perform tilt correction first and then perform angle correction.

       In short, the angle correction of barcodes through the Otsu algorithm can effectively improve the recognition rate and accuracy of barcodes, and provide reliable guarantee for subsequent decoding operations.

3.matlab core program

function I_seg=Otsu(I)

I= double(I);
I= medfilt2(I);         % 二维中值滤波
h_Tmean = mean(mean(I));
[height,width] = size(I);
Size = height * width;  % 图像大小
h_T = sum(sum(I));      % 图像总灰度值
G_min = min(min(I));    % 最小灰度值
G_max = max(max(I));    % 最大灰度值
I_seg = zeros(height,width);     % the array to store the segmented image
thresh = 0;          % the threshold(边界)
num1 = 0;
num2 = 0;            % count the num of the pixel(像素) from the diffrient class
P1 = 0;
P2 = 0;              % the probability (概率)of the different class
h_T1 = 0;
h_T2 = 0;            % the total gray value of different class 
h_T1mean = 0;
h_T2mean = 0;        % the mean value of the class
Max = 0;
for thresh=G_min:G_max     % find the best threshold
    h_T1 = 0;
    h_T2 = 0;
    num1 = 0;
    for h=1:height
        for w=1:width
             if I(h,w) <= thresh
                  num1 = num1 + 1;
                  h_T1 = h_T1 + I(h,w);
             end
         end
     end
     num2 = Size - num1;
     h_T2 = h_T - h_T1;
     P1 = num1/Size;
     P2 = num2/Size;
     h_T1mean = h_T1/num1;
     h_T2mean = h_T2/num2;
     D1 = P1*P2*(h_T1mean - h_T2mean)^2;%类间方差            
     if D1 > Max
         Max = D1;
         T_best = thresh;   % T record the best thresh
     end
 end
 
 %%%%%%% Seg the image %%%%%%%%%
 for i=1:height
     for j=1:width
         if I(i,j) > T_best
             I_seg(i,j) =255;
         end
     end
 end
up2208

4. Algorithm test results

->

Guess you like

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