Detailed Description
The embodiment of the invention provides a character recognition method and device in order to improve the efficiency and the precision of character recognition.
The present invention will be described in detail with reference to the accompanying drawings.
Fig. 1 is a schematic diagram of a character recognition process provided by the present invention, which includes the following steps:
s101: the method comprises the steps of carrying out binarization processing on a picture containing character information to be recognized, recognizing the boundary of characters to be recognized in the picture after the binarization processing, intercepting a character area to be recognized in a sample picture containing the character information to be recognized according to the determined boundary, and carrying out binarization and normalization processing on the intercepted character area.
S102: and in the character area to be identified after normalization processing, jumping of the pixel value is identified, according to the position of a white pixel point during jumping, the position of the corresponding pixel point in the character edge information graph is assigned with 255, and the positions of other pixel points are assigned with other values, wherein the size of the character edge information graph is equal to that of the character area after normalization.
S103: and identifying the pixel value of each pixel point in the character edge information graph, calculating the gradient value of the pixel point when the pixel value of the pixel point in the character edge information graph is identified to be 255, determining the direction of ownership, assigning a value to a corresponding position in the edge gradient array by adopting the direction of ownership, and assigning values to other positions of the array to be-1.
S104: and matching the edge gradient array with each template corresponding to each stored character, determining a matching distance, and taking the character corresponding to the minimum matching distance as a recognition result.
Before character recognition, a plurality of templates of each character are created, and when each template of each character is created, normalization processing, determination of a character edge information graph and determination of a value of each numerical value in an edge gradient array are also required, that is, the creation process of the templates is the same as the steps performed in the character recognition process. And after the assignment of the corresponding position in the edge gradient array corresponding to the template is determined, determining the assignment of each corresponding position in the edge gradient array corresponding to the character to be recognized by adopting the same method, determining the matching distance according to the matching between the edge gradient array corresponding to the character to be recognized and the template, and recognizing the character according to the matching distance.
According to the invention, the gradient direction of each pixel point in the character is used as a corresponding numerical value in the edge gradient array, and the gradient direction has stronger anti-jamming capability, so that the character recognition method has stronger robustness, is matched with each template corresponding to each character in the recognition process, and uses the character corresponding to the minimum matching distance as a recognition result according to the matching distance, thereby avoiding the problem of poor robustness of character single template matching and improving the application range of the matching method.
The character recognition process of the present invention will be described in detail below by way of specific examples.
In order to improve the accuracy of character recognition and improve the efficiency of character recognition, a plurality of templates need to be stored for each character, each template needs to be representative, and the difference between the templates is large. When creating and storing a template of a character, it is necessary to perform normalization processing on a character region and extract the features of the character region after the normalization processing.
Fig. 2 is a process of normalization processing in the process of creating a character template, which includes the following steps:
s201: and carrying out binarization processing on the sample picture containing the character information.
Generally, a sample picture including character information is a color picture, before binarization of the color picture, the color picture needs to be converted into a gray-scale picture firstly, and then binarization processing is performed on the gray-scale picture by adopting a corresponding binarization algorithm.
S202: searching the interior of the picture from the four boundaries of the picture after the binarization processing; when a white pixel point in the picture is searched, determining that the white pixel point is positioned at the boundary of the character; and determining the boundary of the character according to the positions of the white pixel points scanned from each boundary direction.
In order to detect the region where the character is located in the binarized picture, the invention scans the inside of the picture from the four boundaries of the binarized picture, namely scans the inside of the picture from the upper direction, the lower direction, the left direction and the right direction of the binarized picture. Specifically, when a white pixel point is recognized from the upper and lower directions for the first time, the row where the upper and lower boundaries of the character are located is considered as the row where the upper and lower boundaries of the character are located, and when the white pixel point is recognized from the left and right directions for the first time, the column where the white pixel point is located is considered as the column where the left and right boundaries of the character are located.
S203: and intercepting a character area in the sample picture containing the character information according to the determined boundary of the character.
After the rows and columns forming the character boundary are determined, the picture of the character area can be cut out from the sample picture containing the character information.
S204: and carrying out binarization processing on the intercepted character area, and carrying out normalization processing on the picture after binarization processing according to the set size.
After the character region is cut from the color picture, the picture corresponding to the character region is still the color picture, the color picture of the character region is converted into a gray scale picture, the otsu binarization algorithm is adopted for the gray scale picture for binarization processing, then the binarization processing is carried out on the picture after binarization processing according to the set size, for example, the set size can be 24 width and 48 height in the invention, and meanwhile, the normalization processing is carried out on the converted gray scale picture according to the set size. Thereby obtaining a normalized binary image and a normalized gray image.
Fig. 3 is a feature extraction process in the character template creation process provided by the present invention, which includes the following steps:
s301: and identifying the jump of the pixel value of the pixel point in the normalized character area.
The normalized character region is a binary character image, the width of the binary character image is W, and the height of the binary character image is H. And identifying the jump of the pixel value in the binary character image, namely changing the pixel values of two adjacent pixel points from 1 to 0 or changing the pixel values from 0 to 1.
S302: and according to the position of the white pixel point when the pixel value jumps, assigning the pixel point corresponding to the position of the white pixel point to be 255 in a character edge information graph, otherwise, assigning the pixel point to be other values, wherein the size of the character edge information graph is equal to that of the normalized character area.
The character edge information map and the normalized character area have the same size, that is, the character edge information map and the binary character map have the same size, that is, the number of rows, columns and the number of pixel points are the same. When the pixel value of each pixel point in the character edge information graph is determined, the jump of the pixel value in the binary character graph which needs to be identified, according to the position corresponding to the white pixel point in the jump, the pixel point corresponding to the position of the white pixel point in the character edge information graph is assigned to be 255, and the positions of other pixel points in the character edge information graph are assigned to be other values, for example, 0.
S303: and identifying the pixel value of each pixel point in the character edge information graph, calculating the gradient value of the pixel point when the pixel value of the pixel point in the character edge information graph is identified to be 255, and determining the attribution direction.
S304: and assigning the position corresponding to the pixel point position in the template as the gradient direction angle, and assigning other positions as-1.
In the invention, a template which is equal to the height and the width of the character edge information graph is created, and the template can also be regarded as a two-dimensional array which is equal to the height and the width of the character edge information graph. When the assignment of each position in the template is determined, scanning the character edge information graph, and when the pixel value of a scanned pixel point is other values, for example, 0, assigning the corresponding position in the template corresponding to the position of the pixel point to-1; when the pixel value of the scanned pixel point is 255, namely when the white point is scanned, calculating the gradient value of the pixel point according to the following formula at the position corresponding to the pixel point in the normalized gray image:
Gradient=dy/dx
wherein dy is g (i, j +1) -g (i, j-1), dx is g (i +1, j) -g (i-1, j), g (i, j) is the gray value of the corresponding position of the pixel point in the normalized gray image, i represents the row where the pixel point is located, j represents the column where the pixel point is located, and Gradient is the calculated Gradient value of the pixel point.
Dividing an angle range of 0 degree to 360 degrees into 8 equal parts, wherein each equal part corresponds to a gradient direction and is marked by 1-8, calculating the gradient direction angle of the pixel point according to the calculated gradient value of the pixel point, and determining the direction to which the gradient direction angle belongs according to the calculated gradient direction angle.
After a plurality of templates are created for each character, each template is saved in a position corresponding to each character in the template library, that is, a plurality of templates are saved for each character in the template library.
When characters are identified, after a picture containing the characters to be identified is obtained, the picture is converted into a gray-scale image according to the creation process of the template, and the converted picture is subjected to binarization processing by adopting a corresponding binarization algorithm, wherein the binarization algorithm is the same as that in the creation process of the template.
In the picture after the binarization processing, scanning the inside of the picture from four directions of the picture respectively, identifying the position of a first white pixel point in each direction, and determining the boundary of the character to be identified according to the position of the identified white pixel point in each direction; and intercepting the character area to be recognized from the color picture of the character to be recognized according to the determined character boundary.
Converting the intercepted character area to be recognized into a gray map, performing binarization processing on the gray map by adopting a corresponding binarization algorithm, and performing normalization processing on the gray map and the character area to be recognized after the binarization processing according to a set size, wherein the set size is the same as the size set in the template creation process, such as width 24 and height 48, and the binarization algorithm adopted here is also the same as the binarization algorithm adopted in the normalization process in the template creation.
After normalization processing is carried out on the character area to be recognized after binarization, jump of pixel values of pixel points after normalization is recognized, when the pixel values are recognized to jump from 0 to 1 or from 1 to 0, according to the positions of white pixel points during jump, the positions of corresponding pixel points in a character edge information graph are assigned to be 255, and the positions of other pixel points are assigned to be 0.
And identifying the pixel value of each pixel point in the assigned character edge information image, when a pixel point with the pixel value of 255 is identified, calculating the gradient value of the pixel point according to the gray values of the pixel point and the adjacent pixel points in the normalized gray image, and determining the gradient direction of the pixel point according to the calculated gradient value of the pixel point.
According to the determined gradient direction of the pixel point, determining the direction to which the gradient direction belongs in 8 directions equally divided in the range of 0-360 degrees, taking the direction to which the gradient direction belongs as a numerical value corresponding to the position of the pixel point in the edge gradient array of the character to be recognized, and assigning the other positions of the array as-1.
Matching the edge gradient array with each template corresponding to each stored character, and determining the matching distance with each template, wherein when the matching distance is determined, the following formula is adopted:
<math>
<mrow>
<mi>D</mi>
<mo>=</mo>
<mfrac>
<mrow>
<munderover>
<mi>Σ</mi>
<mrow>
<mi>i</mi>
<mo>=</mo>
<mn>0</mn>
</mrow>
<mi>H</mi>
</munderover>
<munderover>
<mi>Σ</mi>
<mrow>
<mi>j</mi>
<mo>=</mo>
<mn>0</mn>
</mrow>
<mi>W</mi>
</munderover>
<mi>d</mi>
<mrow>
<mo>(</mo>
<mi>i</mi>
<mo>,</mo>
<mi>j</mi>
<mo>)</mo>
</mrow>
</mrow>
<mi>S</mi>
</mfrac>
</mrow>
</math>
wherein, c (i, j) is the ith row and the jth column in the edge gradient array of the character to be recognized, t (i, j) is the ith row and the jth column in the template, H is the height of the template after normalization, W is the width of the template after normalization, and the value of S is determined according to the times which are not equal to-1 in the edge gradient array of the character to be recognized, namely the times and the sum of which are not equal to-1 in each position of the template.
Fig. 4 is a schematic structural diagram of a character recognition apparatus provided in the present invention, the apparatus including:
the normalization module 41 is configured to perform binarization processing on a picture containing information of characters to be recognized, recognize a boundary of the characters to be recognized in the picture after the binarization processing, intercept a region of the characters to be recognized in a sample picture containing the information of the characters to be recognized according to the determined boundary, and perform binarization and normalization processing on the intercepted region of the characters;
an edge information determining module 42, configured to identify a jump of a pixel value in the normalized character region to be identified, assign 255 a value to a position of a corresponding pixel point in a character edge information graph according to a position of a white pixel point during the jump, and assign other values to positions of other pixel points, where the character edge information graph is equal to the normalized character region in size;
a gradient direction determining module 43, configured to identify a pixel value of each pixel in the character edge information graph, calculate a gradient value of the pixel when the pixel value of the pixel in the character edge information graph is identified to be 255, determine an attribution direction, assign a value to a corresponding position in an edge gradient array by using the attribution direction value, and assign a value to another position of the array to be-1;
and the matching identification module 44 is configured to match the edge gradient array with each stored template corresponding to each character, determine a matching distance, and use the character corresponding to the minimum matching distance as an identification result.
The normalization module 41 is further configured to, when a template is created, perform binarization processing on a sample picture containing character information, and identify a boundary of a character in the picture after the binarization processing; according to the determined character boundary, intercepting a character area in a sample picture containing character information, and carrying out binarization and normalization processing on the intercepted character area;
the edge information determining module 42 is further configured to, when creating a template, identify a jump of a pixel value in a normalized character region, assign 255 a value to a position of a corresponding pixel point in a character edge information graph according to a position of a white pixel point in the jump, and assign other values to positions of other pixel points, where the size of the character edge information graph is equal to that of the normalized character region;
the gradient direction determining module 43 is further configured to, when creating the template, identify a pixel value of each pixel point in the character edge information graph, calculate a gradient value of the pixel point when the pixel value of the pixel point in the character edge information graph is identified to be 255, determine an attribution direction, assign a position in the template corresponding to the position of the pixel point as a direction value, and assign other positions as-1.
The normalization module 41 is specifically configured to search the inside of the picture from the four boundaries of the picture after the binarization processing; when a white pixel point in the picture is searched, determining that the white pixel point is positioned at the boundary of the character; and determining the boundary of the character according to the positions of the white pixel points scanned from each boundary direction.
The normalization module 41 is further configured to convert the intercepted character area into a grayscale image and perform normalization processing;
the gradient direction determining module 43 is configured to calculate, at a position corresponding to the pixel point in the normalized gray image, a gradient value of the pixel point according to the following formula:
Gradient=dy/dx
wherein dy is g (i, j +1) -g (i, j-1), dx is g (i +1, j) -g (i-1, j), g (i, j) is the gray value of the corresponding position of the pixel point in the normalized gray image, i represents the row where the pixel point is located, j represents the column where the pixel point is located, and Gradient is the calculated Gradient value of the pixel point;
calculating a gradient direction angle according to the calculated gradient value;
from the gradient direction angle, and 8 directions divided between 0 and 360 degrees, the direction to which the gradient direction angle belongs is determined.
The match identification module 44 is specifically configured toA matching distance is determined, wherein, c (i, j) is the ith row and the jth column in the edge gradient array of the character to be recognized, t (i, j) is the ith row and the jth column in the template, H is the height of the template after normalization, W is the width of the template after normalization, and the value of S is determined according to the times which are not equal to-1 in the edge gradient array of the character to be recognized, namely the times and the sum of which are not equal to-1 in each position of the template.
The invention provides a character recognition method and a device, wherein the method aims at character recognition and template creation to be recognized, normalization processing is carried out, a character edge information graph is determined, the value of each numerical value in an edge gradient array is determined, after the assignment of the corresponding position in the edge gradient array is determined, the matching distance is determined according to the matching between the edge gradient array corresponding to the character to be recognized and each template corresponding to each character, and the character is recognized according to the matching distance. According to the invention, the gradient direction of each pixel point in the character is used as a corresponding numerical value in the edge gradient array, and the gradient direction has strong anti-interference capability, so that the character recognition method has strong robustness, is matched with each template corresponding to each character in the recognition process, and uses the character corresponding to the minimum matching distance as a recognition result according to the matching distance, thereby avoiding the problem of poor robustness of character single template matching and improving the application range of the matching method.
While the foregoing description shows and describes a preferred embodiment of the invention, it is to be understood that the invention is not limited to the form disclosed herein, but is not to be construed as limited to the exclusion of other embodiments, and that it may be used in various other combinations, modifications, and environments and may be modified within the scope of the inventive concepts described herein by those skilled in the relevant art or knowledge. And that modifications and variations may be effected by those skilled in the art without departing from the spirit and scope of the invention as defined by the appended claims.