zbar source code analysis - key technical points and optimization strategies

Previous article already said zbar in QR decoding process, and now here introduces some of the key technical points and recommendations focus on strategy optimization:

Affine transformation: the known three points: (x1, y1), (x2, y2), (x3, y3), the origin (x1, y1), maps to (0,0), (1,0), (0,1). No Gaussian elimination method for solving linear equations, the second is the direct use of the final formula is derived parameters. 1, composed of three points is calculated determinant. 2, calculated using the law of Cram h1, h2, h4, h5, h3, h6 is a translation parameter, h3 = x1, h6 = y0. To improve efficiency, all using integer arithmetic, the actual coordinates enlarged 2 ^ res times.

Homography transformation: Known four points: (x1, y1), (x2, y2), (x3, y3), (x4, y4), origin (x1, y1), maps to (0,0), ( 1,0), (0,1), (1,1). 4, 9 parameters, under normal circumstances need SVD (singular value decomposition) is calculated parameters, so that h9 = 1, the other parameters derived using Gaussian elimination, the final result. For efficiency, the use of integer arithmetic units enlarged 16384 times, that is, 14 bits. QR version 40 177 Maximum number of modules, each module occupies a data unit 92, the version number 173 39 modules, each data unit occupies 94, i.e. between the release of the at least two data units of differences have been sufficient. When changing from a square region to the location of the original image, it is necessary to use the horizontal and vertical dimensions of the module in the module size is estimated as the square area increments. Since the homography is used in a homogeneous coordinate system, the result of conversion, a third component needs to be divided further.

Linear least squares fit: hough transform may be employed, it is necessary to calculate hough transform parameter corresponding to the maximum number of dots in a straight line of small, the algorithm complexity of (2m * 180, m is the maximum image size), also needs to calculate parameters . The method of least squares, to estimate only the parameters (complexity n, n being the number of points), in addition to floating point operations may be integer operations. Parameter in the form of a straight line Ax + By + C = 0, using the parameters of this kind of form can not consider the case when the slope is not present. Least squares fit is possible when B is zero, the fitting parameters A / B or B / A.

Ransac algorithm: The disadvantage of using least squares fitting line is affected by a great noise, it is necessary to adopt ransac algorithm to estimate the concentration point, remove noise points. Such is the complexity of the algorithm ransac algorithms coupled with the complexity of the least squares method. Ransac The key is to find a judge is not a centralized point of decision rules, for fitting a straight line, the first to take the two points, that all points on both points fit a straight line and then judged according to the rules of the third point, fourth point, and so on. After several rounds of iterations, a maximum number of points corresponding to the straight line mark. Three rules in a straight line is determined that three points corresponding to the calculated value of the determinant, a threshold value may be set in practice, less than the threshold value in a straight line, is larger than the threshold value is not collinear. In addition, certain iterations, is a mathematical difficulty. Algorithm defects, for the next lot of points, the number of iterations increases, may affect efficiency.

Alignment pattern search algorithm: an initialization 25 data points; alignment pattern exactly 5x5 module, the mapping over the grid module 25 to the image, after which the center of the reading of 25 as a template, the following as a search template center point. Second, the mesh data 25 read points; calculating new offset center point above the center of the grid, is added to the offset point corresponding to the template, the read data bit is stored in a 32-bit integer data the binary. Third, the calculated Hamming distance; Hamming distance represents the number of the template between the actual minimum standard template does not match, the obtained value of 0 indicates exact match, for example 3, showing three modules can not be matched. Fourth, the search for the best Hamming distance points. Center point to search, the search radius is r, the search region is a square region of width 2r-1, starting from the upper left corner of the region, a clockwise traversal of the four sides of the square points, these points as the center point, according to the 5x5 template read the data. To find the point of minimum Hamming distance. If the middle of the Hamming distance of zero, the search is terminated. Fifth, adjust alignment pattern center position. Adjusting for point identified four steps corresponding to the minimum Hamming distance, precise positioning of alignment pattern corresponding to the center point. One of the best match template eight situations with a test stencil mask corresponding to meet the operational test stencil mask, satisfying a white - black - white central point, this point coordinates with respect to the minimum Hamming distance of the corresponding point offset weighted averaging, the center of a black satisfying given greater weight of the module, or to impart less weight.

Right, bottom fitting straight line: the initial point of a straight edge side of a finder pattern fitting a straight line, as shown on the left border of two imaginary, two false bottom border. Two virtual border to find pattern wherein a starting point of the white center of the module, the other virtual frame start point starting a white frame center (quiet side). Along the dotted line scanning, a step size of half the width of the module, each scan point, to find out whether two lines meet between the two points of white - black - white, if so, their center points are found, then this point was added to the edge in a straight line (straight line edges QR boundary is the boundary straight line passing through the center of the module). Whenever the edge points increases 1/4, re-fitting straight line parameters, the compensated linear scanning, such as scanning step size. Determining the range of approximately scanning pattern according to the lower left corner and the upper right range find Finder pattern (direction is a direction after the affine transformation) as a white outer border face or terminates the scanning. Finally, if sufficient in quantity point on the straight line obtained, the fitting straight line again, otherwise, the default straight line using linear configuration after affine transformation, right line parallel to the upper left corner and lower left finder pattern finder pattern; bottom line parallel to the upper left finder pattern and right finder pattern composed of straight lines. As described in FIG:
 
Right: not found to meet the white - black - white more than 14 times, terminates the scanning, see right linescan

Bresenham algorithm: see, http: //blog.csdn.net/mikedai/article/details/62885701 to implement the principle of do a very detailed introduction.

Image Binarization: threshold is equal to T = (m / n) -D ,, where D = 3, m is (x, y) coordinate values ​​of pixels within the field of w * h and, if T> f (x0, y0) , the binarization result is 0, 1 otherwise. When implemented, in order to avoid division, T> f (x0, y0) is converted to: f (x0, y0) * w * h + 3> m, the multiplication into a shift operation, for the calculation of m is initialized first column h and then subtracting the first window in the calculation of the next row line coupled to the next line of the window, the next column is the same processing can also be employed. Defects algorithm is the introduction of some random noise, which does not affect too much on the recognition effect. Related discussion comments in the code is clear:
/The above algorithms are computationally expensive, and do not work as well as the simple algorithm below.Sauvola by itself does an excellent job of classifying regions outside the QR code as background, which greatly reduces the chance of false alarms.However, it also tends to over-shrink isolated black dots inside the code,making them easy to miss with even slight mis-alignment.Since the Gatos method uses Sauvola as input to its background interpolation method, it cannot possibly mark any pixels as foreground which Sauvola classified as background, and thus suffers from the same problem.The following simple adaptive threshold method does not have this problem,though it produces essentially random noise outside the QR code region. QR codes are structured well enough that this does not seem to lead to any actual false alarms in practice, and it allows many more codes to be detected and decoded successfully than the Sauvola or Gatos binarization methods./

Optimization strategy:
existing zbar decoding program to decode pdf417 might want to consider, so the need to scan at least two times when the scanned image, there is the possibility of one-dimensional bar for vertical arrangement. However, in practice pdf417 rarely used on the phone, so it can consider not support its decoding. So there is no need to scan twice, the following process may be employed: a first vertical scanning, for mobile applications is rotated 90 degrees, the estimated range of possible QR finder pattern. Second, one may miss the transverse dimensional bar code, so the horizontal scanning line 17. Below the red zone.


Of course, this optimization may also produce some bad circumstances, for example, FIG, severe overlapping some red area. Test image as an ideal picture, the red part of the region is the need for horizontal scanning section.

Another consideration is that optimization, the number of interlaced or interlaced line, However, this can affect the positioning accuracy, however, for a relatively large field of application module size, this treatment is undoubtedly effective means to enhance the efficiency.
Memory optimization, can be used instead of the conventional value 0 0,255, and 8 pixels occupy one byte, the memory usage amount becomes 1/8 of the original.
For the finder pattern center positioning accuracy, the conventional approach is not very good effect, can be considered has been estimated on the basis of the center of the finder pattern, recalibration, to meet the black - white - black - white - black ratio of 1: 1 : 3: 1: 1, to find the center of this line, do the same correction in this line a center. Thereby estimating a precise point. If the estimated failure, using the original center point.

Finally, the original can be removed and some bounds checking multiplication operations into the adder. Pick replaced with some function or macro definition using inline functions.
 

He published 188 original articles · won praise 35 · views 40000 +

Guess you like

Origin blog.csdn.net/dop102/article/details/102917695