Background technology
The recognition process of two-dimension code is: by the crawl of the image inductor in two dimension code reading equipment image in 2 D code, the image of crawl described here is generally 8 gray level images.After recognizing apparatus had grasped gray level image, then the two dimension code reading algorithm need to obtain the two-dimension code binary code stream according to corresponding two-dimension code code system be converted into black, the white binary picture of 8 gray level images by pixel, for example " deceived " to represent 1, and " in vain " represents 0; Perhaps " deceive " and represent 0, " in vain " represents 1.Afterwards, then according to two-dimension code coding rule and error correction algorithm, two-dimension code is carried out decoding, complete the recognition of two-dimension code.
But, because the medium that two-dimension code depends on is different, residing photoenvironment is different, and the other factors of imaging system, the two-dimension code gray level image that can cause grabbing by two dimension code reading equipment varies, thereby make gray level image be converted into binary image and become very complicated, and the effect quality of binaryzation process directly concerns the recognition effect of two-dimension code.
In order to eliminate photoenvironment to the impact of binaryzation process, the binarization method of present image in 2 D code is: adopt the segmented areas method, then block-by-block uses large Tianjin (OTSU) algorithm calculated threshold, and the block threshold value that obtains is carried out smoothing processing, to eliminate blocking effect, at last each pixel and corresponding block threshold value are made comparisons and then obtained binary image.
Here, in each pixel corresponding grey scale image a certain a certain zone in other words, this piece or zone be corresponding threshold value all, for example 8 gray level image values are between 0 to 255, if the gray-scale value of pixel greater than this threshold value, is judged as white, value is 0, if it is black that the gray-scale value of pixel less than this threshold value, is judged as, value is 1; Can certainly be conversely, black if the gray-scale value of pixel greater than this threshold value, is judged as, value is 1, and is white if the gray-scale value of pixel less than this threshold value, is judged as, value is 0.So far, just can obtain each pixel by gray level image only gets and " deceives " the perhaps binary image of " in vain ".
But in including the gray level image of two-dimension code, two-dimension code only accounts for the part of image, and is even less, so in blocking process, each piece only comprises the sub-fraction of two-dimension code.And when using Otsu algorithm, in the situation that target and background is out of proportion, Otsu algorithm also lost efficacy, then the binary image that obtains also with regard to distortion, the binary picture image distortion just has influence on the recognition of two-dimension code naturally.
Summary of the invention
For this reason, technical matters to be solved by this invention is: a kind of binarization method of image in 2 D code is provided, makes and adopt the method for the invention to obtain the two-dimension code binary image, not only speed is fast, and undistorted, thereby is conducive to the recognition of two-dimension code.
So, the invention provides a kind of binarization method of image in 2 D code, comprising:
Determine the segmented areas size of this gray level image according to the specification self-adaptation of two-dimension code gray level image;
Calculate the contrast in each piece zone, the height of decision block region contrast, low and average gray value is made as 255 lower than the threshold value in the piece zone of entire image average gray value with contrast, low and average gray value is made as 0 higher than the threshold value in the piece zone of entire image average gray value with contrast, and calculated threshold is wanted in the piece zone that contrast is high;
Threshold value to all pieces zone that calculates is carried out smoothing processing, and each pixel of image in 2 D code gray level image and corresponding threshold value through the piece zone that obtains after smoothing processing are compared, and obtains binary image.
Wherein, described specification according to the two-dimension code gray level image is determined the segmented areas size of this gray level image, comprising:
Determine the size of segmented areas with the size adaptation of the wide and high product of gray level image.
For example the two-dimension code gray level image of 752 pixel * 480 pixels is divided into the piece zone of 40 pixel * 40 pixels, perhaps the two-dimension code gray level image of 1280 pixel * 720 pixels is divided into the piece zone of 80 pixel * 80 pixels.
Wherein, the contrast in each piece of described calculating zone comprises: the grey level histogram of adding up each piece area image:
Li=ni/N i=0,1,2 ..., 255, wherein, ni is that the gray-scale value of image is the number of pixels of i; N is the sum of all pixels in image;
Select threshold value T, image is divided into identical bright dark two parts of pixel quantity, namely
So in the piece area image, than dark pixel, namely average gray value is lower than the entire image average gray value; The gray average of pixel is:
In the piece area image, than bright pixel, namely average gray value higher than the gray average of the pixel of entire image average gray value is:
The contrast C ontrast in piece zone is defined as the poor of light and shade part gray average in the piece area image, that is:
Wherein, the height of described decision block region contrast comprises:
Do reference, setting threshold with the contrast of view picture two-dimension code gray level image and the contiguous contrast that is judged to be the piece zone that includes image in 2 D code;
The contrast in threshold value and current block zone is compared, if the contrast in current block zone thinks not comprise the low contrast piece zone of two-dimension code part lower than the threshold value of described setting; The zone be the high-contrast piece area grayscale image that includes two-dimension code if the contrast in current block zone greater than the threshold value of described setting, is judged this piece, the further calculated threshold of these piece zone needs.
Wherein, the threshold value in this piece of described further calculating zone comprises:
Cut out the part of gray-scale value maximum in the piece region histogram, the span of establishing piece region histogram gray-scale value is gmin, gmin+1,, gmax, sum of all pixels are N, cut out piece region histogram gray scale and partly be Delta, after cutting out the part gray-scale value, histogrammic span is:
gmin+1,gmin+2,…,gmax-?Delta;
If calculating the threshold value in former zone with Otsu algorithm is T0,, gray scale T0 is that threshold value is divided into two classes with former area image pixel, and namely gray-scale value is gmin, gmin+1 ..., the pixel of T0 consists of a class, is designated as C0, number of pixels N0; Gray-scale value is T0+1, T0+2, the pixel of gmax consists of an other class, be designated as C1, number of pixels is N1, counts ratio 1:1 as with reference to ratio take bright dark pixel, threshold value T0 is cut apart the pixel count Diff that departs from and is so: Diff=| N0 – N/2|, the piece region histogram gray scale part scope that cuts out is set as:
Delta=(gmax-gmin)*?Diff/N
Histogram remainder gray scale value is: gmin, and gmin+1 ..., gmax-Delta;
Upgrade described sum of all pixels N parameter, utilize Otsu algorithm to histogram remainder computing block region threshold T, this threshold value T is exactly the desirable threshold value in piece zone.
Wherein, described threshold value to the piece zone that calculates is carried out smoothing processing, comprising:
Take a certain zone as benchmark, the threshold value in piece zone around it is weighted, the threshold value that obtains after weighting is the threshold value after processing for this piece segment smoothing;
Repeat above-mentioned steps, until the threshold value in all pieces zone that calculates has all been carried out smoothing processing.
the binarization method of image in 2 D code of the present invention, determine the size of two-dimension code gray level image segmented areas by self-adaptation, and calculate the contrast in each piece zone and judge the height of contrast, and low and average gray value is made as 255 lower than the threshold value in the piece zone of entire image average gray value with contrast, low and average gray value is made as 0 higher than the threshold value in the piece zone of entire image average gray value with contrast, the high piece zone of all the other contrasts is further calculated threshold, afterwards, the threshold value in all pieces zone that calculates is carried out smoothing processing and then obtained the image in 2 D code of binaryzation by the mode of pixel comparison.Make by the method and obtain the two-dimension code binary image, not only speed is fast, and undistorted, thereby is conducive to the recognition of two-dimension code.
Embodiment
Below, describe the present invention by reference to the accompanying drawings.
As shown in Figure 1, the present embodiment provides a kind of binarization method of image in 2 D code, comprising:
Step 1 determines that according to the specification of two-dimension code gray level image the segmented areas of this gray level image is big or small.This process is completed automatically by system, so we claim that also this process is the self-adaptation blocking process.described specification according to the two-dimension code gray level image is determined the segmented areas size of this gray level image, comprise the size of determining segmented areas with the size adaptation of the wide and high product of gray level image, for example: the two-dimension code gray level image has all size, 640 pixel * 480 pixels for example, 752 pixel * 480 pixels, 1280 pixel * 720 pixels etc., because in the gray level image of different size, taken two-dimension code partly accounts for the constant rate of gray level image, so, the size that we can determine block image according to length and the wide product size of gray level image, for example, the piece zone that the two-dimension code gray level image of 752 pixel * 480 pixels is divided into 40 pixel * 40 pixels, the piece zone that the two-dimension code gray level image of 1280 pixel * 720 pixels is divided into 80 pixel * 80 pixels.
Step 2, the contrast in each piece of described calculating zone and the height of decision block region contrast.Low and average gray value is made as 255, the black patch in corresponding binary image lower than the threshold value in the piece zone of entire image average gray value with contrast.Low and average gray value is made as 0, the white piece in corresponding binary image higher than the threshold value in the piece zone of entire image average gray value with contrast.So just needn't further calculate the threshold value in these pieces zone, thereby reduce calculated amount.
Afterwards, execution in step 4; Remaining piece zone is step 3.
Step 3 for the high piece of contrast zone, is further calculated the threshold value in this piece zone;
Step 4 is carried out smoothing processing to the threshold value in all pieces zone that calculates, and each pixel of image in 2 D code gray level image is compared with corresponding threshold value through the piece zone that obtains after smoothing processing one by one, obtains binary image.
In step 2, the contrast in each piece of described calculating zone comprises: the grey level histogram of adding up each piece area image:
Li=ni/N i=0,1,2 ..., 255, wherein, ni is that the gray-scale value of image is the number of pixels of i; N is the sum of all pixels in image;
Select threshold value T, image is divided into identical bright dark two parts of pixel quantity, namely
So in the piece area image, than dark pixel, namely average gray value lower than the gray average of the pixel of entire image average gray value is:
In the piece area image, than bright pixel, namely average gray value higher than the gray average of the pixel of entire image average gray value is:
The contrast C ontrast in piece zone is defined as the poor of light and shade part gray average in the piece area image, that is:
In step 2, the height of described decision block region contrast comprises:
Do reference, setting threshold with the contrast of view picture two-dimension code gray level image and the contiguous contrast that is judged to be the piece zone that includes image in 2 D code;
The contrast in threshold value and current block zone is compared, if the contrast in current block zone thinks not comprise the low contrast piece zone of two-dimension code part lower than the threshold value of described setting; The zone be the high-contrast piece area grayscale image that includes two-dimension code if the contrast in current block zone greater than the threshold value of described setting, is judged this piece, the further calculated threshold of these piece zone needs.
Piece zone to the further calculated threshold of needs utilizes the front to add up the histogram of acquisition, adopts the threshold value in algorithm computing block zone, large Tianjin (OTSU).This threshold value is divided into light and shade two parts pixel with piece area image pixel, for the piece in image in 2 D code ideally zone, the two-part pixel quantity of light and shade than regular meeting near 1:1, but in actual conditions, all either large or small deviation can be arranged.And in most cases this ratio greater than 1:1, that is to say, the part total area of two-dimension code in the piece area image " sky " is greater than the total area of the part of two-dimension code " bar ".Set a threshold percentage, take 1:1 as target proportion, in the piece region histogram, the pixel that highlights divides is clipped the too much part pixel that departs from ratio, the benefit of processing like this can reduce calculated amount and can make again light and shade two parts pixel ratio more near 1:1.Afterwards, then the histogram after processing is used large Tianjin (algorithm) calculated threshold, the threshold value that calculates for the second time will be at desirable Near Threshold, namely the threshold value in our needed zone.Concrete, in step 3, the threshold value in this piece of described further calculating zone comprises:
Cut out the part of gray-scale value maximum in the piece region histogram, the span of establishing piece region histogram gray-scale value is gmin, gmin+1,, gmax, sum of all pixels are N, cut out piece region histogram gray scale and partly be Delta, after cutting out the part gray-scale value, histogrammic span is:
gmin+1,gmin+2,…,gmax-?Delta;
If calculating the threshold value in former zone with Otsu algorithm is T0,, gray scale T0 is that threshold value is divided into two classes with former area image pixel, and namely gray-scale value is gmin, gmin+1 ..., the pixel of T0 consists of a class, is designated as C0, number of pixels N0; Gray-scale value is T0+1, T0+2, the pixel of gmax consists of an other class, be designated as C1, number of pixels is N1, counts ratio 1:1 as with reference to ratio take bright dark pixel, threshold value T0 is cut apart the pixel count Diff that departs from and is so: Diff=| N0 – N/2|, in the piece region histogram that cuts out, gray scale part scope is set as:
Delta=(gmax-gmin)*?Diff/N
Histogram remainder gray scale value is: gmin, and gmin+1 ..., gmax-Delta;
Upgrade described sum of all pixels N parameter, utilize Otsu algorithm to histogram remainder computing block region threshold T, this threshold value T is exactly the desirable threshold value in piece zone.
In step 4, described threshold value to all pieces zone that calculates is carried out smoothing processing, comprising:
Take a certain zone as benchmark, the threshold value in piece zone around it is weighted, the threshold value that obtains after weighting is the threshold value after processing for this piece segment smoothing;
Repeat above-mentioned steps, until the threshold value in all pieces zone that calculates has all been carried out smoothing processing.
After the threshold value in each piece zone obtains, in order to eliminate blocking effect, namely, it is slowly that the pixel light and shade of adjacent block changes, but it is larger that the threshold value that calculates between adjacent block may differ, be necessary block threshold value is carried out smoothing processing, namely the threshold value of each piece is weighted average acquisition by block threshold value on every side.Here choose 8 piece zones around the piece zone add itself namely the block threshold value of a 3*3 be weighted.As following table:
Wherein, the subscript of A represents the position of piece zone in the two-dimension code gray level image.
Each pixel of image in 2 D code gray level image and corresponding threshold value through the piece zone that obtains after smoothing processing are compared, obtain binary image, for example 8 gray level image values are between 0 to 255, if the gray-scale value of pixel is greater than this threshold value, be judged as whitely, value is 0, if the gray-scale value of pixel is less than this threshold value, be judged as blackly, value is 1; Can certainly be conversely, black if the gray-scale value of pixel greater than this threshold value, is judged as, value is 1, and is white if the gray-scale value of pixel less than this threshold value, is judged as, value is 0.So far, just can obtain each pixel by gray level image only gets and " deceives " the perhaps binary image of " in vain ".
The described binarization method of the present embodiment has utilized the characteristics of two-dimension code black and white module equilibrium, the gray level image that in the two dimension code reading process, two dimension code reading equipment is grabbed to be converted into a kind of more reasonably method of binary image.Make it possible to faster, obtain accurately the threshold value in piece zone, thereby obtain binary image more accurately.
Use a large amount of PDF417 that comprises, QR, GM, the two-dimension code gray level image of the different code systems such as DM is tested, and binarization method of the prior art does contrast, and this binarization method not only makes two dimension code reading speed fast, and the recognition rate improves.
in sum, the binarization method of the described image in 2 D code of the present embodiment, by determining the size of two-dimension code gray level image segmented areas, and calculate the contrast in each piece zone and judge the height of contrast, low and average gray value is made as 255 lower than the threshold value in the piece zone of entire image average gray value with contrast, low and average gray value is made as 0 higher than the threshold value in the piece zone of entire image average gray value with contrast, and to the regional calculated threshold of the high piece of contrast, afterwards, the threshold value in all pieces zone that calculates is carried out smoothing processing and then obtained the image in 2 D code of binaryzation by the mode of pixel comparison.Make by the method and distinguish two-dimension code, the speed of not only distinguishing is fast, and the recognition rate improves.
The above is only preferred embodiment of the present invention, and is in order to limit the present invention, within the spirit and principles in the present invention not all, any modification of doing, is equal to replacement, improvement etc., within all should being included in protection scope of the present invention.