Disclosure of Invention
The invention provides a series of image preprocessing methods of morphological transformation combined with binaryzation aiming at the problem of low recognition rate of dot matrix DataMatrix two-dimensional codes, and the smooth blurring and the morphological transformation have self-adaptability through spot detection. The method can overcome the problems of overlarge gap, uneven illumination, noise interference and the like in the identification of the dot matrix type DataMatrix code, and converts the dot matrix type DM two-dimensional code into a standard format, so that the detection can be carried out through the conventional DM code handheld identification equipment, secondary development is not needed on hardware, the system design is feasible, quick and effective, and the actual requirement on dot matrix type DM decoding at present can be met.
The invention provides a dot matrix DM two-dimensional code image processing method, which comprises the following steps:
the method comprises the following steps: reading a dot matrix DM two-dimensional code image, and performing width unification processing by using a nearest neighbor interpolation algorithm or a bilinear interpolation algorithm on the basis of not changing the high width proportion of the original dot matrix DM two-dimensional code image;
step two: converting the images with uniform sizes into gray level images;
step three: performing Gaussian smoothing filtering processing on the grayed image to remove fine textures of an image background, so that solid points of the dot matrix code elements are smoother;
step four: converting the gray level image after Gaussian smooth filtering into a black and white binary image;
step five: carrying out code element detection on the image subjected to binarization in the fourth step to obtain the diameter of the dot matrix code element;
step six: dynamically changing the size of the average template according to the diameter size of the lattice code element obtained in the step five, further carrying out dynamic mean value filtering processing on the gray level image in the step two, and then carrying out improved binarization processing combining a kittler algorithm and a Bernsen algorithm on the gray level image after the dynamic mean value filtering;
step seven: and carrying out morphological opening operation and closing operation on the binary image obtained in the step six to obtain a processed dot matrix image, wherein,
the opening operation is represented by the following equation:
the closing operation is represented by the following equation:
wherein A is an input binary image, and B is a square structural element.
Step eight: and e, converting the dot matrix image obtained in the step seven into a standard DM two-dimensional code image of a recognizable block structure through median filtering denoising processing.
Furthermore, the side length of the square structural element B in the seventeenth operation in the step is preferably 1/3 of the lattice symbol diameter, and the side length of the square structural element B in the closed operation is preferably 1 to 5 display pixels smaller than the lattice symbol diameter.
Further, the median filtering denoising process used in the step eight is preferably implemented by the following steps:
replacing the value of a point in the image with the median of the values of the points in a neighborhood of the point, and making the surrounding pixel values close to the true values, thereby eliminating isolated noise points, wherein the two-dimensional median filter output is obtained by the following formula:
g(x,y)=med{f(x-k,y-1),(k,1∈W)}
wherein med { } represents the median of the access array sequence; f (x, y), g (x, y) are respectively an original image and a processed image; w is a two-dimensional template of 3 x 3 size; k and l are integers and respectively represent increment in the x and y directions of the coordinates.
Further, the dynamic mean filtering processing of step six preferably further includes the following steps:
(1) template size was obtained by the following formula:
wherein round represents a rounding operation;
(2) calculating an average filtering template:
wherein k is the size of the average template, D is the diameter of the lattice code element, and is the average template;
(3) the average filtering is performed as follows:
I1(x,y)=*I(x,y)
wherein I (x, y) represents a gray-scale map matrix, I1(x, y) represents a filtered image gray matrix;
further, the process of converting the gaussian smooth-filtered gray scale image into a black-and-white binary image in the fourth step specifically includes:
(1) obtaining a global threshold T by using a Kittler algorithm, wherein the method for calculating the global threshold T comprises the following steps:
wherein f (x, y) is the original gray scale map obtained in step two, and e (x, y) is max { | ex|,|eyI.e. the maximum value of the gradient, exF (x-1, y) -f (x +1, y) is the gradient in the horizontal direction, eyF (x, y-1) -f (x, y +1) is the gradient in the vertical direction;
(2) scanning the whole f (x, y) gray level image, the binarization result is:
wherein b (x, y) is the binarization result.
Further, the improved binarization processing combining the kittler algorithm and the Bernsen algorithm used in the step six comprises the following specific steps:
the method for calculating the global threshold T comprises the following steps of (1) obtaining the global threshold T by using a Kittler algorithm:
wherein f (x, y) is the original gray scale map obtained in step two, and e (x, y) is max { | ex|,|eyI.e. the maximum value of the gradient, exF (x-1, y) -f (x +1, y) is the gradient in the horizontal direction, eyF (x, y-1) -f (x, y +1) is the gradient in the vertical direction;
step (2) processing the interval with uneven illumination in the image histogram by adopting Bernsen algorithm, wherein the gray value of the interval is concentrated near the global threshold T obtained in step (1), if T is T3D is the interval width processed by Bernsen algorithm, namely the diameter of lattice element
The result of binarization
If T is3D is the interval width processed by Bernsen algorithm, namely the diameter of lattice element
The result of binarization
Wherein,
T3is the basis for threshold selection and T3(x,y)=maxs-mins,
T2(x,y)=0.5(maxd+mind),
T4(x,y)=0.5(T+T2(x,y));
Wherein,
maxdrepresenting the maximum pixel value of the pixel point (x, y) in a window with the size of 4w by 4 w;
representing the minimum pixel value of a pixel (x, y) in a large window of 4w by 4 w;
representing the maximum pixel value of a pixel (x, y) in a smaller window of size 2w x 2 w;
representing the minimum pixel value of a pixel (x, y) in a smaller window of size 2w x 2 w;
in the formula, max represents the maximum value of the pixel in the window, min represents the minimum value of the pixel in the window, k and l are integers and respectively represent the increment in the directions of coordinates x and y; w represents a window of local threshold operation, the value range of w is 5-9, and T is2Representing the mean value of the pixel grey levels, T, within the window4Is T2And the average of the global threshold T.
Further, the specific flow of symbol detection in step five is as follows:
detecting the image elements by using a Gaussian Laplacian operator, and for a two-dimensional Gaussian function:
the method comprises the following steps that sigma is a width parameter of a function, namely a characteristic scale, and is used for controlling the radial action range of the function, the larger sigma represents the wider radial direction of the function, the larger similar code element is, the smaller sigma represents the narrower radial direction of the function, the smaller similar code element is, x and y represent two-dimensional space positions, and g (x, y and sigma) represents a two-dimensional Gaussian function;
laplace transform of formula (1):
wherein, Delta2g represents the Laplace function, Delta, of a two-dimensional Gaussian function2Representing a second order differential operator, g representing a two-dimensional Gaussian function; normalized laplacian of gaussian transform:
in the formula (3)Represents a normalized laplacian of gaussian function with a variance of 0;
the normalized two-dimensional laplacian function shown in the formula (3) is a circularly symmetric function, and two-dimensional code elements of different sizes are detected by changing the value of σ to obtainObtaining the pole value is equivalent to solving the following equation:
wherein,representing a normalized Gaussian Laplace functionThe partial derivative of the sigma is calculated,representing normalized laplace function of gaussiansA pole value of (d);
that is:
r2-2σ2=0
wherein r represents the radius of the circular code element of the two-dimensional code image binaryzation in scaleThe laplacian of gaussian response value is maximized, and similarly, if a circular symbol in the image is black and white inverted, then the laplacian of gaussian response value for that symbol is scaled toThe time reaches the minimum, the scale sigma value when the Gaussian response reaches the peak value is the characteristic scale of code element detection, the discrete Laplacian response value of the binarized image under different scales is calculated, then each point in the position space is checked, if the Laplacian response value of the point is larger than or smaller than the values of other cubic space neighborhoods, the point is the detected two-dimensional code data element point, and the searching for the position space pixel pointAnd scale spaceCan be represented by the following function:
the function represents the point at which the laplacian response reaches a maximum or minimum at both spatial location and scaleThe value of (1) is the symbol to be detected; wherein t represents a scale value, (x, y) represents a spatial position, and max minus(x,y,t)(-) denotes the maximum or minimum of the response function at spatial and scale positions, arg (-) denotes the value of the variable corresponding to the function value,representing a standard two-dimensional laplacian function in scale space.
The invention also provides a dot-matrix DM two-dimensional code image processing device, which comprises:
the image reading module is used for reading the dot matrix DM two-dimensional code image and performing width unification processing by utilizing a nearest neighbor interpolation algorithm or a bilinear interpolation algorithm on the basis of not changing the high width proportion of the original dot matrix DM two-dimensional code image;
the gray level conversion module is used for converting the images with uniform sizes into gray level images;
the Gaussian smoothing filtering module is used for performing Gaussian smoothing filtering processing on the grayed image to remove fine textures of an image background and enable solid points of the dot matrix code elements to be smoother;
the binarization conversion module is used for converting the gray level image after Gaussian smooth filtering into a black and white binarization image;
the code element detection module is used for carrying out code element detection on the image subjected to binarization in the binarization conversion module to obtain the diameter of the dot matrix code element;
the dynamic mean value filtering and binary conversion module is used for dynamically changing the size of the mean template according to the diameter size of the lattice code element obtained by the code element detection module, further carrying out dynamic mean value filtering processing on the gray level image obtained by the gray level conversion module, and then carrying out improved binary processing combining a kittler algorithm and a Bernsen algorithm on the gray level image after the dynamic mean value filtering;
an operation module for performing morphological open operation and close operation on the binary image obtained by the dynamic mean filtering and binary conversion module to obtain a processed dot matrix image,
the opening operation is represented by the following equation:
the closing operation is represented by the following equation:
wherein A is an input binary image; b is a square structural element;
and the standardization module is used for converting the dot matrix image obtained by the operation module into a standard DM two-dimensional code image of a recognizable block structure through median filtering denoising processing.
Furthermore, the side length of the square structural element B in the opening operation of the operation module is preferably 1/3 of the dot matrix symbol diameter, and the side length of the square structural element B in the closing operation is preferably 1 to 5 display pixels smaller than the dot matrix symbol diameter.
Further, the median filtering denoising process used by the normalization module is preferably implemented by:
replacing the value of a point in the image with the median of the values of the points in a neighborhood of the point, and making the surrounding pixel values close to the true values, thereby eliminating isolated noise points, wherein the two-dimensional median filter output is obtained by the following formula:
g(x,y)=med{f(x-k,y-1),(k,1∈W)}
wherein med { } represents the median of the access array sequence; f (x, y), g (x, y) are respectively an original image and a processed image; w is a two-dimensional template of 3 x 3 size; k and l are integers and respectively represent increment in the x and y directions of the coordinates.
Detailed Description
The invention will be described in further detail with reference to the following figures and specific examples, without limiting the scope of the invention.
As shown in fig. 1, the lattice DM code (right) is significantly different from the standard DM code (left).
Because the lattice type DM code is composed of uniform round points according to a certain rule, if the pictures are directly subjected to fuzzy smoothing and morphological change according to a conventional method, the effect is not ideal, and the over-processing and the processing effect are not obvious easily.
[ example one ]
The embodiment provides a dot matrix DM two-dimensional code image processing flow and a method, which may be implemented by a computer program or a hardware circuit, and the specific flow is shown in fig. 2.
(one) reading images and unifying sizes
For convenience of processing, firstly, the size of the dot-matrix two-dimensional image is unified after the image is read, and the width is unified by using a nearest neighbor interpolation algorithm or a bilinear interpolation algorithm on the basis of not changing the high width proportion of the original image, and experiments show that the unification can enable the identification to be more accurate.
(II) conversion to grayscale
The image processing method is convenient for processing the image and needs to convert the originally acquired image into a gray image.
(III) Gaussian smoothing filter processing
Since the roughness of the barcode background affects the identification accuracy, and the dot matrix modules of the dot matrix DM code are all solid dots, and the solid dots are not greatly affected by the smoothness, a fuzzy smoothing process is required to remove the complex texture of the picture. The requirement of the lattice type DM bar code on the smoothing treatment is not too high, and the dynamic mean filtering and the Gaussian filtering are found to meet the requirement through experimental comparison of various smoothing filtering transformations. Therefore, the natural smoothing effect is obtained by using Gaussian smoothing in the step, and the blurring is not too much, so that the measurement of the spot detection is more accurate and stable.
(IV) binarization processing
The binarization processing of the image is to convert the gray level image into a black and white binarization image which can reflect the whole structure and local characteristics of the image by selecting a proper threshold value. In the dot Matrix Data Matrix barcode image processing process, by performing corresponding operation on the binarized image, the characteristic information such as the boundary, position, size and the like of the target area can be easily acquired, thereby laying a foundation for analysis and identification of the barcode image.
Firstly, a global threshold value T is obtained by using a Kittler algorithm, and the method for calculating the global threshold value T is as follows:
wherein f is(x, y) is the original gray scale map obtained in step two, and e (x, y) is max { | ex|,|eyI.e. the maximum value of the gradient, exF (x-1, y) -f (x +1, y) is the gradient in the horizontal direction, eyF (x, y-1) -f (x, y +1) is the gradient in the vertical direction;
then, scanning the whole f (x, y) gray image, the binarization result is:
wherein b (x, y) is the binarization result.
(V) speckle detection
The principle of spot detection (i.e., lattice symbol detection) is as follows:
detecting the image code element by utilizing a Laplacian of Guassian (LoG) operator, and for a two-dimensional Gaussian function:
the method comprises the following steps that sigma is a width parameter of a function, namely a characteristic scale, and is used for controlling the radial action range of the function, the larger sigma represents the wider radial direction of the function, the larger similar code element is, the smaller sigma represents the narrower radial direction of the function, the smaller similar code element is, x and y represent two-dimensional space positions, and g (x, y and sigma) represents a two-dimensional Gaussian function;
laplace transform of formula (1):
wherein, Delta2g represents the Laplace function, Delta, of a two-dimensional Gaussian function2Representing a second order differential operator, g representing a two-dimensional Gaussian function;
normalized laplacian of gaussian transform:
in the formula (3)Represents a normalized laplacian of gaussian function with a variance of 0;
the normalized two-dimensional laplacian function shown in the formula (3) is a circularly symmetric function, and two-dimensional code elements of different sizes are detected by changing the value of σ to obtainObtaining the pole value is equivalent to solving the following equation:
wherein,representing a normalized Gaussian Laplace functionThe partial derivative of the sigma is calculated,representing normalized laplace function of gaussiansA pole value of (d);
that is:
r2-2σ2=0
wherein r represents the radius of the circular code element of the two-dimensional code image binaryzation in scaleThe laplacian of gaussian response value is maximized, and similarly, if a circular symbol in the image is black and white inverted, then the laplacian of gaussian response value for that symbol is scaled toThe time reaches the minimum, the scale sigma value when the Gaussian response reaches the peak value is the characteristic scale of code element detection, the discrete Laplacian response value of the binarized image under different scales is calculated, then each point in the position space is checked, if the Laplacian response value of the point is larger than or smaller than the values of other cubic space neighborhoods, the point is the detected two-dimensional code data element point, and the searching for the position space pixel pointAnd scale spaceCan be represented by the following function:
the function represents the value of the point where the laplace response reaches the maximum or minimum value at the same time on the spatial position and scale, and the point is the code element to be detected; wherein t represents a scale value, (x, y) represents a spatial position, and max minus(x,y,t)(-) denotes the maximum or minimum of the response function at spatial and scale positions, arg (-) denotes the value of the variable corresponding to the function value,representing a standard two-dimensional laplacian function in scale space.
According to the above principle, the sizes of the data element points and their respective blobs of the two-dimensional code image can be well detected, see fig. 3.
(VI) dynamic filtering and improved binarization processing
Firstly, dynamic filtering processing is carried out:
(1) template size was obtained by the following formula:
wherein round represents a rounding operation;
(2) calculating an average filtering template:
wherein k is the size of the average template, D is the diameter of the lattice code element, and is the average template;
(3) the average filtering is performed as follows:
I1(x,y)=*I(x,y)
wherein I (x, y) represents a gray-scale map matrix, I1(x, y) represents a filtered image gray matrix;
then, an improved binarization process is performed:
the binarization algorithm of the method selects a binarization algorithm combining a kittler algorithm and an improved Bernsen algorithm, can well ignore unnecessary background details aiming at the characteristics of a dot matrix type two-dimensional code dot module relative to smoothness and similar background textures, and has good processing effect on pictures with uneven illumination. Firstly, finding out the area of the image with uneven illumination according to a simple statistical algorithm of Kittler, then improving the processing process of the Bernsen algorithm, adjusting parameters, weakening the artifact problem of the original algorithm, and processing the part of the image with uneven illumination by using the improved algorithm. The algorithm has good stability and self-adaptability, and can obviously improve the binarization effect and the recognition rate of the two-dimensional bar code.
The binaryzation processing method combining the kittler algorithm and the improved Bernsen algorithm is as follows:
the method for calculating the global threshold T comprises the following steps of (1) obtaining the global threshold T by using a Kittler algorithm:
wherein f (x, y) is the original gray scale map obtained in step two, and e (x, y) is max { | ex|,|eyI.e. the maximum value of the gradient, exF (x-1, y) -f (x +1, y) is the gradient in the horizontal direction, eyF (x, y-1) -f (x, y +1) is the gradient in the vertical direction;
step (2) adopts Bernsen algorithm to process the interval with uneven illumination in the image histogram, the gray value of the interval is concentrated near the global threshold T obtained in step (1),
if T is3D is the interval width processed by Bernsen algorithm, namely the diameter of lattice element
The result of binarization
If T is3D is the interval width processed by Bernsen algorithm, namely the diameter of lattice element
The result of binarization
Wherein,
T3is the basis for threshold selection and T3(x,y)=maxs-mins,
T2(x,y)=0.5(maxd+mind),
T4(x,y)=0.5(T+T2(x,y));
Wherein,
maxdrepresenting the maximum pixel value of the pixel point (x, y) in a window with the size of 4w by 4 w;
representing the minimum pixel value of a pixel (x, y) in a large window of 4w by 4 w;
representing the maximum pixel value of a pixel (x, y) in a smaller window of size 2w x 2 w;
representing the minimum pixel value of a pixel (x, y) in a smaller window of size 2w x 2 w;
in the formula, max represents the maximum value of the pixel in the window, min represents the minimum value of the pixel in the window, k and l are integers and respectively represent the increment in the directions of coordinates x and y; w represents a window of local threshold operation, the value range of w is 5-9, and T is2Representing the mean value of the pixel grey levels, T, within the window4Is T2And the average of the global threshold T.
(VII) performing opening and closing operation to obtain dot matrix image
The lattice type DM code is composed of lattice modules, and in order to change the lattice module into a recognizable standard DM code, the lattice module needs to be changed into a block structure, and morphological transformation is used for opening and closing operation, and the opening and closing operation is a combined form of expansion and corrosion.
Wherein A is swollen by B and is described asIs defined as:
wherein, A is corroded by B and is marked as A theta B, and the definition is as follows:
AΘB={z|(B)z∩Ac≠Φ}
the morphological open operation of A by B can be named as A omicB, and the operation is the result of swelling corrosion of A by B and then B:
the morphological closing operation of A by B can be denoted as A.B, which is the result of the expansion of A by B and then the erosion expansion by B:
in the method, A is an input binary image, and B is a square structural element.
The opening operation can smooth the contour of the image, can also break narrow connection and eliminate burrs, but is different from corrosion in that the large contour of the image is not subjected to overall shrinkage, and the position of an object is not changed at all. Closed arithmetic can also smooth the contour, but in contrast to open arithmetic, it is generally able to close narrow discontinuities and fill small holes. Fig. 4 is a schematic diagram of an open operation and a closed operation. Therefore, the open operation and the close operation are applied to the DM two-dimensional code standardization, and stray points and some burrs after binarization can be removed. A large number of experiments can be summarized, and the square structural element of one third of the spot size (namely the lattice code element diameter) obtained by spot detection is used for carrying out open operation to remove stray points and burrs. The square structural elements of 1 to 5 display pixel points slightly smaller than the spot size (namely the dot matrix code element diameter) are used for carrying out closed operation, so that the point module is changed into an approximate block structure, and the L-shaped boundary of the point module is clearly visible.
(eight) performing median filtering processing after opening and closing operation, and converting the median filtering processing into a standard two-dimensional code image
The median filtering denoising process is preferably implemented by:
replacing the value of a point in the image with the median of the values of the points in a neighborhood of the point, and making the surrounding pixel values close to the true values, thereby eliminating isolated noise points, wherein the two-dimensional median filter output is obtained by the following formula:
g(x,y)=med{f(x-k,y-1),(k,l∈W)}
wherein med { } represents the median of the access array sequence; f (x, y), g (x, y) are respectively an original image and a processed image; w is a two-dimensional template of 3 x 3 size; k and 1 are integers respectively representing increment in the x and y directions of the coordinates.
The effect after conversion into a standard DM two-dimensional code image is shown in fig. 5.
[ example two ]
The invention also provides a software virtual device of the functional module architecture, which comprises the following structures:
a dot-matrix DM two-dimensional code image processing device comprises:
the image reading module is used for reading the dot matrix DM two-dimensional code image and performing width unification processing by utilizing a nearest neighbor interpolation algorithm or a bilinear interpolation algorithm on the basis of not changing the high width proportion of the original dot matrix DM two-dimensional code image;
the gray level conversion module is used for converting the images with uniform sizes into gray level images;
the Gaussian smoothing filtering module is used for performing Gaussian smoothing filtering processing on the grayed image to remove fine textures of an image background and enable solid points of the dot matrix code elements to be smoother;
the binarization conversion module is used for converting the gray level image after Gaussian smooth filtering into a black and white binarization image;
the code element detection module is used for carrying out code element detection on the image subjected to binarization in the binarization conversion module to obtain the diameter of the dot matrix code element;
the dynamic mean filtering and binary conversion module is used for dynamically changing the size of an average template according to the diameter size of the lattice code element obtained by the code element detection module, further performing dynamic mean filtering processing on the gray level image obtained by the gray level conversion module, and then performing improved binary processing combining a kittler algorithm and a Bernsen algorithm on the gray level image after the dynamic mean filtering:
an operation module for performing morphological open operation and close operation on the binary image obtained by the dynamic mean filtering and binary conversion module to obtain a processed dot matrix image,
the opening operation is represented by the following equation:
the closing operation is represented by the following equation:
wherein A is an input binary image; b is a square structural element;
and the standardization module is used for converting the dot matrix image obtained by the operation module into a standard DM two-dimensional code image of a recognizable block structure through median filtering denoising processing.
Of course, the product of the above functional module architecture can also be implemented by a real hardware circuit.
The lattice blobs referred to in this invention are also referred to as lattice symbols, and the blobs and symbols are in the same concept.
All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.