Background technology
The recognition process of Quick Response Code is: capture image in 2 D code by the image inductor in two dimension code reading equipment, the image of crawl described here, is generally 8 gray level images.After recognizing apparatus has captured gray level image, two dimension code reading algorithm needs by 8 gray level images by black, the white binary picture of being converted into of pixel, and then obtain Quick Response Code binary code stream according to corresponding Quick Response Code code system, such as " black " represents 1, and " in vain " represents 0; Or " black " represents 0, " in vain " represents 1.Afterwards, then according to Quick Response Code coding rule and error correction algorithm, decoding is carried out to Quick Response Code, complete the recognition of Quick Response Code.
But, the medium depended on due to Quick Response Code is different, residing photoenvironment is different, and the other factors of imaging system, the Quick Response Code gray level image that two dimension code reading equipment grabs can be resulted through vary, thus make gray level image be converted into binary image to become very complicated, and the effect of binarization quality directly concerns the recognition effect of Quick Response Code.
In order to eliminate the impact of photoenvironment on binarization, the binarization method of current image in 2 D code is: adopt segmented areas method, then block-by-block uses large Tianjin (OTSU) algorithm calculated threshold, and to the smoothing process of the block threshold value obtained, to eliminate blocking effect, finally each pixel and corresponding block threshold value are made comparisons and then obtained binary image.
Here, a certain piece in each pixel corresponding grey scale image a certain region in other words, the all corresponding threshold value in this block or region, such as 8 gray level image values are between 0 to 255, if the gray-scale value of pixel is greater than this threshold value, are then judged as white, value is 0, if the gray-scale value of pixel is less than this threshold value, be then judged as black, value is 1; Can certainly conversely, if the gray-scale value of pixel is greater than this threshold value, then be judged as black, value be 1, if the gray-scale value of pixel is less than this threshold value, is then judged as white, and value is 0.So far, just can obtain each pixel by gray level image and only get " black " or the binary image of " in vain ".
But in the gray level image including Quick Response Code, Quick Response Code only accounts for the part of image, even less, so in blocking process, each piece of sub-fraction only comprising Quick Response Code.And when using Otsu algorithm, when target and background is out of proportion, Otsu algorithm also lost efficacy, so the binary image obtained is also with regard to distortion, binary picture image distortion, just had influence on the recognition of Quick Response Code naturally.
Summary of the invention
For this reason, technical matters to be solved by this invention is: the binarization method providing a kind of image in 2 D code, makes to adopt the method for the invention to obtain Quick Response Code binary image, and not only speed is fast, and undistorted, thus is conducive to the recognition of Quick Response Code.
So, the invention provides a kind of binarization method of image in 2 D code, comprising:
The segmented areas size of this gray level image is determined according to the specification self-adaptation of Quick Response Code gray level image;
Calculate the contrast in each piece of region, the height of decision block region contrast, by contrast, low and average gray value is set to 255 lower than the threshold value in the block region of entire image average gray value, by contrast, low and average gray value is set to 0 higher than the threshold value in the block region of entire image average gray value, and calculated threshold is then wanted in the block region that contrast is high;
To the smoothing process of threshold value in all pieces of regions calculated, and the threshold value in each pixel of image in 2 D code gray level image and the corresponding block region obtained after smoothing processing is compared, obtain binary image.
Wherein, the described specification according to Quick Response Code gray level image determines the segmented areas size of this gray level image, comprising:
With the size of the size adaptation determination segmented areas of the wide and high product of gray level image.
Such as the Quick Response Code gray level image of 752 pixel * 480 pixels is divided into the block region of 40 pixel * 40 pixels, or the Quick Response Code gray level image of 1280 pixel * 720 pixels is divided into the block region of 80 pixel * 80 pixels.
Wherein, the contrast in described calculating each piece of region, comprising: the grey level histogram adding up each piece of area image:
Li=ni/Ni=0,1,2 ..., 255, wherein, ni is 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 bright dark two parts that pixel quantity is identical, namely
=
So, comparatively dark pixel in block area image, namely average gray value is lower than entire image average gray value; The gray average of pixel is:
=
/
Brighter pixels in block area image, namely average gray value higher than the gray average of the pixel of entire image average gray value is:
=
/
The contrast C ontrast in block region, be then defined as the difference of the light and shade part gray average in block area image, that is:
Contrast=
-
。
Wherein, the height of described decision block region contrast, comprising:
Be judged to be that the contrast in the block region including image in 2 D code does reference with the contrast of view picture Quick Response Code gray level image and contiguous, setting threshold value;
The contrast in threshold value and current block region is compared, if the contrast in current block region is lower than the threshold value of described setting, not thinks the low contrast block region comprising Quick Response Code part; If the contrast in current block region is greater than the threshold value of described setting, then judge that this block region is as the high-contrast block area grayscale image including Quick Response Code, this block region needs further calculated threshold.
Wherein, the threshold value in described this block region of further calculating, comprising:
Cut out the part that in block region histogram, gray-scale value is maximum, if the span of block region histogram gray-scale value is gmin, gmin+1,, gmax, sum of all pixels is N, cutting out block region histogram gray portion is Delta, then, after cutting out part gray-scale value, histogrammic span is:
gmin+1,gmin+2,…,gmax-Delta;
If the threshold value calculating former piece of region with Otsu algorithm is T0, then, gray scale T0 is that former piece of area image pixel is divided into two classes by threshold value, and namely gray-scale value is gmin, gmin+1 ..., the pixel of T0 forms a class, is designated as C0, number of pixels N0; Gray-scale value is T0+1, T0+2, the pixel of gmax forms an other class, be designated as C1, number of pixels is N1, with bright dark pixel number ratio 1:1 for reference ratio, so threshold value T0 is split the pixel count Diff departed from and is: Diff=|N0 – N/2|, and the block region histogram gray portion range set cut out is:
Delta=(gmax-gmin)*Diff/N
Histogram remainder gray scale value is: gmin, gmin+1 ..., gmax-Delta;
Upgrade described sum of all pixels N parameter, utilize Otsu algorithm to be exactly the desirable threshold value in block region to histogram remainder computing block region threshold T, this threshold value T.
Wherein, the smoothing process of threshold value in the described block region to calculating, comprising:
With a certain piece of region for benchmark, be weighted the threshold value in block region around it, the threshold value obtained after weighting is the threshold value after this block segment smoothing process;
Repeat above-mentioned steps, until all carried out smoothing processing to the threshold value in all pieces of regions calculated.
The binarization method of image in 2 D code of the present invention, by the size of self-adaptation determination Quick Response Code gray level image segmented areas, and calculate the contrast in each piece of region and judge the height of contrast, and low and average gray value is set to 255 lower than the threshold value in the block region of entire image average gray value by contrast, by contrast, low and average gray value is set to 0 higher than the threshold value in the block region of entire image average gray value, the block region then further calculated threshold that all the other contrasts are high, afterwards, to the smoothing process of threshold value in all pieces of regions calculated and then the image in 2 D code obtaining binaryzation by the mode that pixel compares.Make to obtain Quick Response Code binary image by the method, not only speed is fast, and undistorted, thus is conducive to the recognition of Quick Response Code.
Embodiment
Below, describe the present invention by reference to the accompanying drawings.
As shown in Figure 1, present embodiments provide a kind of binarization method of image in 2 D code, comprising:
Step 1, determines the segmented areas size of this gray level image according to the specification of Quick Response Code gray level image.This process is completed automatically by system, and therefore we also claim this process to be self-adaptation blocking process.The described specification according to Quick Response Code gray level image determines the segmented areas size of this gray level image, comprise with the size of the size adaptation determination segmented areas of the wide and high product of gray level image, such as: Quick Response Code gray level image has all size, such as 640 pixel * 480 pixels, 752 pixel * 480 pixels, 1280 pixel * 720 pixels etc., because Quick Response Code part taken in the gray level image of different size accounts for the constant rate of gray level image, so, we can determine the size of block image according to the length of gray level image and wide product size, such as, the Quick Response Code gray level image of 752 pixel * 480 pixels is divided into the block region of 40 pixel * 40 pixels, the Quick Response Code gray level image of 1280 pixel * 720 pixels is divided into the block region of 80 pixel * 80 pixels.
Step 2, the contrast in described calculating each piece of region the height of decision block region contrast.By contrast, low and average gray value is set to 255, the black patch in corresponding binary image lower than the threshold value in the block region of entire image average gray value.By contrast, low and average gray value is set to 0, the white block in corresponding binary image higher than the threshold value in the block region of entire image average gray value.So just need not calculate the threshold value in these block regions further, thus reduce calculated amount.
Afterwards, step 4 is performed; Remaining block region then step 3.
Step 3, for the block region that contrast is high, calculates the threshold value in this block region further;
Step 4, to the smoothing process of threshold value in all pieces of regions calculated, and compares each pixel of image in 2 D code gray level image with the threshold value in the corresponding block region obtained after smoothing processing one by one, obtains binary image.
In step 2, the contrast in described calculating each piece of region, comprising: the grey level histogram adding up each piece of area image:
Li=ni/Ni=0,1,2 ..., 255, wherein, ni is 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 bright dark two parts that pixel quantity is identical, namely
=
So, comparatively dark pixel in block area image, namely average gray value lower than the gray average of the pixel of entire image average gray value is:
=
/
Brighter pixels in block area image, namely average gray value higher than the gray average of the pixel of entire image average gray value is:
=
/
The contrast C ontrast in block region, be then defined as the difference of the light and shade part gray average in block area image, that is:
Contrast=
-
。
In step 2, the height of described decision block region contrast, comprising:
Be judged to be that the contrast in the block region including image in 2 D code does reference with the contrast of view picture Quick Response Code gray level image and contiguous, setting threshold value;
The contrast in threshold value and current block region is compared, if the contrast in current block region is lower than the threshold value of described setting, not thinks the low contrast block region comprising Quick Response Code part; If the contrast in current block region is greater than the threshold value of described setting, then judge that this block region is as the high-contrast block area grayscale image including Quick Response Code, this block region needs further calculated threshold.
To the block region needing further calculated threshold, add up the histogram of acquisition before utilization, adopted the threshold value in large Tianjin (OTSU) algorithm computing block region.Block area image pixel is divided into light and shade two parts pixel by this threshold value, for the block region in image in 2 D code ideally, the two-part pixel quantity of light and shade than regular meeting close to 1:1, but in actual conditions, all can have either large or small deviation.And in most cases this ratio is greater than 1:1, that is, in block area image, the part total area of Quick Response Code " sky " is greater than the total area of the part of Quick Response Code " bar ".Set a threshold percentage, take 1:1 as target proportion, in block region histogram, clip the too much part pixel departing from ratio to the pixel that most highlights divides, the benefit of process like this can reduce calculated amount and light and shade two parts pixel ratio can be made again closer to 1:1.Afterwards, larger Tianjin (algorithm) calculated threshold is used to the histogram after process, the threshold value that second time calculates, will at desirable Near Threshold, the threshold value in the block region namely required for us.Concrete, in step 3, the threshold value in described this block region of further calculating, comprising:
Cut out the part that in block region histogram, gray-scale value is maximum, if the span of block region histogram gray-scale value is gmin, gmin+1,, gmax, sum of all pixels is N, cutting out block region histogram gray portion is Delta, then, after cutting out part gray-scale value, histogrammic span is:
gmin+1,gmin+2,…,gmax-Delta;
If the threshold value calculating former piece of region with Otsu algorithm is T0, then, gray scale T0 is that former piece of area image pixel is divided into two classes by threshold value, and namely gray-scale value is gmin, gmin+1 ..., the pixel of T0 forms a class, is designated as C0, number of pixels N0; Gray-scale value is T0+1, T0+2, the pixel of gmax forms an other class, be designated as C1, number of pixels is N1, with bright dark pixel number ratio 1:1 for reference ratio, so threshold value T0 is split the pixel count Diff departed from and is: Diff=|N0 – N/2|, and in the block region histogram cut out, gray portion range set is:
Delta=(gmax-gmin)*Diff/N
Histogram remainder gray scale value is: gmin, gmin+1 ..., gmax-Delta;
Upgrade described sum of all pixels N parameter, utilize Otsu algorithm to be exactly the desirable threshold value in block region to histogram remainder computing block region threshold T, this threshold value T.
In step 4, the smoothing process of threshold value in the described all pieces of regions to calculating, comprising:
With a certain piece of region for benchmark, be weighted the threshold value in block region around it, the threshold value obtained after weighting is the threshold value after this block segment smoothing process;
Repeat above-mentioned steps, until all carried out smoothing processing to the threshold value in all pieces of regions calculated.
After the threshold value in each piece of region obtains, in order to eliminate blocking effect, namely, the pixel light and shade change of adjacent block is slowly, but the threshold value calculated between adjacent block may differ larger, be necessary that, to the smoothing process of block threshold value, namely the threshold value of each piece is weighted average acquisition by the block threshold value of surrounding.8 the block regions here chosen around block region add that the block threshold value of namely 3*3 itself is weighted.As following table:
Wherein, the subscript of A represents the position of block region in Quick Response Code gray level image.
The threshold value in each pixel of image in 2 D code gray level image and the corresponding block region obtained after smoothing processing is compared, obtain binary image, such as 8 gray level image values are between 0 to 255, if the gray-scale value of pixel is greater than this threshold value, then be judged as white, value is 0, if the gray-scale value of pixel is less than this threshold value, then be judged as black, value is 1; Can certainly conversely, if the gray-scale value of pixel is greater than this threshold value, then be judged as black, value be 1, if the gray-scale value of pixel is less than this threshold value, is then judged as white, and value is 0.So far, just can obtain each pixel by gray level image and only get " black " or the binary image of " in vain ".
Binarization method described in the present embodiment, make use of the feature of Quick Response Code black and white module equilibrium, the gray level image that two dimension code reading equipment grabs is converted into the one more reasonably method of binary image in two dimension code reading process.Make it possible to more quick, obtain the threshold value in block region accurately, thus obtain binary image more accurately.
Use and a large amount of comprise PDF417, the Quick Response Code gray level image of the different code system such as QR, GM, DM is tested, and binarization method of the prior art contrasts, and this binarization method not only makes two dimension code reading speed fast, and recognition rate improves.
In sum, the binarization method of image in 2 D code described in the present embodiment, by determining the size of Quick Response Code gray level image segmented areas, and calculate the contrast in each piece of region and judge the height of contrast, by contrast, low and average gray value is set to 255 lower than the threshold value in the block region of entire image average gray value, by contrast, low and average gray value is set to 0 higher than the threshold value in the block region of entire image average gray value, and the block region calculated threshold high to contrast, afterwards, to the smoothing process of threshold value in all pieces of regions calculated and then the image in 2 D code obtaining binaryzation by the mode that pixel compares.Make to distinguish Quick Response Code by the method, speed of not only distinguishing is fast, and recognition rate improves.
The foregoing is only preferred embodiment of the present invention, not in order to limit the present invention, within the spirit and principles in the present invention all, any amendment done, equivalent replacement, improvement etc., all should be included within protection scope of the present invention.