A text correction method, apparatus and electronic device
By calculating the rotation angle by determining the angle between the lines of the smallest bounding rectangle and the largest inscribed rectangle in the irregular text, the problem of cumbersome irregular text correction in the existing technology is solved, and a simpler and more efficient character correction is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING JIAHE HAISEN HEALTH TECH CO LTD
- Filing Date
- 2023-02-24
- Publication Date
- 2026-05-05
AI Technical Summary
Existing technologies for correcting irregular text are cumbersome, requiring model training for characters at various deflection angles, resulting in a complex and time-consuming process.
By acquiring images of irregular text, the minimum bounding rectangle and the maximum inscribed rectangle of the target character are determined. The rotation angle is calculated using the angle between the lines within these two rectangles, and the character is corrected according to the rotation angle, thus eliminating the dependence on the model.
It simplifies the irregular text correction process, avoids the complexity and time consumption of model training, and achieves simpler and more efficient character correction.
Smart Images

Figure CN116189189B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of image processing technology, and in particular to a text correction method, apparatus and electronic device. Background Technology
[0002] Irregular text generally refers to text that, due to factors such as scanning, photographing angle, or photographing method, has been transformed from something that should be regular into text that is slanted, curved, or crooked. Correcting irregular text helps optical character recognition (OCR) technology to recognize characters.
[0003] Existing technologies typically utilize multiple models to determine the orientation of a character. In the process of determining character orientation, images of a character at various deflection angles are usually used to train the model, and then the trained model is used to determine the deflection angle of the character.
[0004] In existing technologies, determining the deflection angle of a character requires training a model on characters with various deflection angles, which presents a cumbersome approach to correcting irregular text. Summary of the Invention
[0005] In view of this, embodiments of this application provide a text correction method, apparatus, and electronic device, aiming to solve the problem of cumbersome implementation of irregular text correction in the prior art.
[0006] In a first aspect, embodiments of this application provide a text correction method, the method comprising:
[0007] Obtain an image containing irregular text; the irregular text includes target characters;
[0008] The minimum bounding rectangle of the target character in the image is determined, wherein a pair of opposite sides of the minimum bounding rectangle are parallel to the horizontal direction; and, the region enclosed by multiple vertices of the target character in the image is determined, wherein the region enclosed by multiple vertices of the target character is used to determine the maximum inscribed rectangle of the target character; the two adjacent sides of the maximum inscribed rectangle are not equal.
[0009] Within the smallest circumscribed rectangle, a first straight line perpendicular to the horizontal direction is determined, and within the largest inscribed rectangle, a second straight line perpendicular to the direction of the shorter side is determined.
[0010] The rotation angle of the target character is determined based on the angle between the first straight line and the second straight line;
[0011] The target character is corrected according to its rotation angle.
[0012] Optionally, determining the minimum bounding rectangle of the target character in the image specifically includes:
[0013] The left, right, top, and bottom boundaries of the target character in the image are determined. The left and right boundaries are parallel to the vertical direction, and the top and bottom boundaries are parallel to the horizontal direction. The left, right, top, and bottom boundaries are used to determine the minimum bounding rectangle of the target character.
[0014] Optionally, the method further includes:
[0015] The image containing irregular text is divided into multiple sub-images by character line.
[0016] Determining the left, right, top, and bottom boundaries of the target character in the image specifically includes:
[0017] Determine the left, right, top, and bottom boundaries of the target character in the target sub-image among the plurality of sub-images;
[0018] The determination of the region enclosed by multiple vertices of the target character in the image specifically includes:
[0019] Determine the region enclosed by multiple vertices of the target character in the target sub-image among the multiple sub-images.
[0020] Optionally, determining the left and right boundaries of the target character in the target sub-image among the plurality of sub-images specifically includes:
[0021] Construct a first matrix, which has M rows and 1 column, and all elements in the first matrix have a value of 1; wherein M is the same as the number of pixel rows of the target sub-image;
[0022] Multiply the transpose of the column matrix corresponding to the pixel values of each pixel column in the target sub-image with the first matrix to obtain multiple first values;
[0023] The pixel column corresponding to the first value that is not equal to the first preset value is determined as the target pixel column;
[0024] The pixel column to the left of the target pixel column that is equal to the first preset value is determined as the left boundary of the target character; the pixel column to the right of the target pixel column that is equal to the first preset value is determined as the right boundary of the target character.
[0025] Optionally, determining the upper and lower boundaries of the target character in the target sub-image among the plurality of sub-images specifically includes:
[0026] Construct a second matrix with 1 row and N columns, where each element in the second matrix has a value of 1; wherein N is the same as the number of pixel columns between the left and right boundaries of the target character;
[0027] Multiply the row matrix corresponding to the pixel values of each pixel row in the target sub-image with the transpose of the second matrix to obtain multiple second values;
[0028] Determine the pixel row corresponding to the second value that is not equal to the second preset value;
[0029] The pixel row above the target pixel row, corresponding to the second value equal to the second preset value, is determined as the upper boundary of the target character; the pixel row below the target pixel row, corresponding to the second value equal to the second preset value, is determined as the lower boundary of the target character.
[0030] Optionally, after correcting the target character according to its rotation angle, the method further includes:
[0031] In the vertical direction, multiple rotated target characters in the image are moved to the same horizontal line.
[0032] Optionally, moving multiple rotated target characters in the image to the same horizontal line specifically includes:
[0033] Obtain the ordinate of the midpoint of the rotated target character located in the same row in the image containing irregular text.
[0034] Calculate the average of the ordinates of multiple rotated target characters;
[0035] Move multiple rotated target characters in the image containing irregular text to the horizontal line where the average value is located.
[0036] Optionally, correcting the target character according to its rotation angle specifically includes:
[0037] If the rotation angle is greater than the preset angle, the target character is corrected according to the rotation angle of the target character.
[0038] Secondly, embodiments of this application provide a text correction device, the device comprising:
[0039] The acquisition module is specifically used to acquire an image containing irregular text; the irregular text includes target characters.
[0040] The determining module is specifically used to determine the minimum bounding rectangle of the target character in the image, wherein a pair of opposite sides of the minimum bounding rectangle are parallel to the horizontal direction; and to determine the region enclosed by multiple vertices of the target character in the image, wherein the region enclosed by multiple vertices is used to determine the maximum inscribed rectangle of the target character; the two adjacent sides of the maximum inscribed rectangle are not equal; within the minimum bounding rectangle, a first straight line perpendicular to the horizontal direction is determined, and within the maximum inscribed rectangle, a second straight line perpendicular to the direction of the shorter side is determined; and the rotation angle of the target character is determined based on the angle between the first straight line and the second straight line.
[0041] The correction module is specifically used to correct the target character according to the rotation angle of the target character.
[0042] Optionally, the determining module is specifically used for:
[0043] The left, right, top, and bottom boundaries of the target character in the image are determined. The left and right boundaries are parallel to the vertical direction, and the top and bottom boundaries are parallel to the horizontal direction. The left, right, top, and bottom boundaries are used to determine the minimum bounding rectangle of the target character.
[0044] Optionally, the device further includes a segmentation module, which is specifically used to segment the image containing irregular text into multiple sub-images by character rows.
[0045] The determining module is specifically used to determine the left boundary, right boundary, top boundary, and bottom boundary of the target character in the target sub-image among the plurality of sub-images; and to determine the region enclosed by multiple vertices of the target character in the target sub-image among the plurality of sub-images.
[0046] Optionally, the determining module specifically includes a construction unit, a calculation unit, and a determining unit;
[0047] The construction unit is specifically used to construct a first matrix, which has M rows and 1 column, and each element in the first matrix has a value of 1; wherein M is the same as the number of pixel rows of the target sub-image;
[0048] The calculation unit is specifically used to multiply the transpose of the column matrix corresponding to the pixel values of each pixel column in the target sub-image with the first matrix to obtain multiple first values;
[0049] The determining unit is specifically used to determine the pixel column corresponding to the first value that is not equal to the first preset value as the target pixel column; to determine the pixel column to the left of the target pixel column that is equal to the first preset value as the left boundary of the target character; and to determine the pixel column to the right of the target pixel column that is equal to the first preset value as the right boundary of the target character.
[0050] Optionally, the construction unit is specifically used to construct a second matrix, which has 1 row and N columns, and each element in the second matrix has a value of 1; wherein, N is the same as the number of pixel columns between the left and right boundaries of the target character;
[0051] The calculation unit is specifically used to multiply the row matrix corresponding to the pixel values of each pixel row in the target sub-image with the transpose of the second matrix to obtain multiple second values;
[0052] The determining unit is specifically used to determine the pixel row corresponding to the second value which is not equal to the second preset value as the target pixel row; to determine the pixel row above the target pixel row corresponding to the second value which is equal to the second preset value as the upper boundary of the target character; and to determine the pixel row below the target pixel row corresponding to the second value which is equal to the second preset value as the lower boundary of the target character.
[0053] Optionally, the device further includes a mobility module, which is specifically used for:
[0054] After the target character is corrected according to its rotation angle, the multiple rotated target characters in the image are moved to the same horizontal line in the vertical direction.
[0055] Optionally, the mobile module is specifically used for:
[0056] Obtain the ordinate of the midpoint of the rotated target character located in the same row in the image containing irregular text.
[0057] Calculate the average of the ordinates of multiple rotated target characters;
[0058] Move multiple rotated target characters in the image containing irregular text to the horizontal line where the average value is located.
[0059] Optionally, the correction module is specifically used for:
[0060] If the rotation angle is greater than the preset angle, the target character is corrected according to the rotation angle of the target character.
[0061] Thirdly, embodiments of this application provide an electronic device, including: a memory and a processor;
[0062] The memory is used to store programs;
[0063] The processor calls the program and uses it for:
[0064] Obtain an image containing irregular text; the irregular text includes target characters;
[0065] The minimum bounding rectangle of the target character in the image is determined, wherein a pair of opposite sides of the minimum bounding rectangle are parallel to the horizontal direction; and the region enclosed by multiple vertices of the target character in the image is determined, wherein the region enclosed by multiple vertices is used to determine the maximum inscribed rectangle of the target character; wherein two adjacent sides of the maximum inscribed rectangle are not equal.
[0066] Within the smallest circumscribed rectangle, a first straight line perpendicular to the horizontal direction is determined, and within the largest inscribed rectangle, a second straight line perpendicular to the direction of the shorter side is determined.
[0067] The rotation angle of the target character is determined based on the angle between the first straight line and the second straight line;
[0068] The target character is corrected according to its rotation angle.
[0069] This application provides a text correction method, apparatus, and electronic device. In executing the method, firstly, an image containing irregular text is acquired; then, the minimum bounding rectangle of the target character in the image is determined, wherein a set of opposite sides of the minimum bounding rectangle is parallel to the horizontal direction; and, the region enclosed by multiple vertices of the target character in the image is determined, the region enclosed by the multiple vertices of the target character is used to determine the maximum inscribed rectangle of the target character; two adjacent sides of the maximum inscribed rectangle are unequal; within the minimum bounding rectangle, a first straight line perpendicular to the horizontal direction is determined, and within the maximum inscribed rectangle, a second straight line perpendicular to the direction of the shorter side is determined; next, the rotation angle of the target character is determined based on the angle between the first and second straight lines; finally, the target character is corrected according to the rotation angle. Thus, by determining the rotation angle of the target character based on the angle between the first and second straight lines, and correcting the target character based on the rotation angle, the means of correcting irregular characters is freed from dependence on models, making the implementation process simpler and overcoming the cumbersome problem of existing methods for irregular text correction. Attached Figure Description
[0070] To more clearly illustrate the technical solutions in this embodiment or the prior art, the drawings used in the description of the embodiment or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0071] Figure 1 A flowchart of a text correction method provided in this application embodiment;
[0072] Figure 2 An irregular text illustration provided for an embodiment of this application;
[0073] Figure 3 A schematic diagram of a character boundary provided for an embodiment of this application;
[0074] Figure 4 Another character boundary diagram provided for an embodiment of this application;
[0075] Figure 5A A schematic diagram of the minimum bounding rectangle of a character provided in an embodiment of this application;
[0076] Figure 5B A schematic diagram of the region enclosed by multiple vertices of a character, provided in an embodiment of this application;
[0077] Figure 5C A schematic diagram of the maximum inscribed rectangle of a character provided in an embodiment of this application;
[0078] Figure 6 This application provides a schematic diagram of the merging of the minimum bounding rectangle and the maximum inscribed rectangle of a character.
[0079] Figure 7A A schematic diagram illustrating the positional relationship between a first straight line within the smallest bounding rectangle of a character and a second straight line within the largest inscribed rectangle of the character, provided as an embodiment of this application.
[0080] Figure 7B A schematic diagram showing the positional relationship between the first straight line within the smallest bounding rectangle of a character and the second straight line within the largest inscribed rectangle of the character, as provided in an embodiment of this application.
[0081] Figure 8 This is a schematic diagram of the structure of a text correction device provided in an embodiment of this application;
[0082] Figure 9 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0083] Irregular text generally refers to text that, due to factors such as scanning, photographing angle, or photographing method, has been transformed from something that should be regular into text that is slanted, curved, or crooked. Correcting irregular text helps optical character recognition (OCR) technology to recognize and process text.
[0084] In existing technologies, when correcting irregular text, image feature extraction is usually performed using image deep learning models first, and then further processing is carried out on the processed image to correct the irregular text.
[0085] This approach has the following disadvantages:
[0086] First, model training requires a large amount of similar data, and collecting this data is itself a massive undertaking. Model training is also a very time-consuming operation.
[0087] Secondly, even models trained on large datasets will have varying levels of accuracy, never reaching 100%. Using multiple models further complicates matters, as subsequent steps must be performed on incorrect model outputs, requiring separate rule processing for different error types – a highly complex approach.
[0088] Third, when determining the angle of a character, because a single character can have a variety of angles, current technologies can only predict four angles (0°, 9°, 18°, and 27°) using models, or they don't predict the angle at all and directly feed the character with angles into a deep learning model for training. This requires all angles of the character to be accurately identified; a single character with 360° angles would require 360 images. For N characters, this would require 360*N images, a massive workload.
[0089] In view of this, embodiments of this application provide a text correction method. Within the smallest bounding rectangle of the target character, a first straight line perpendicular to the horizontal direction is determined, and within the largest inscribed rectangle, a second straight line perpendicular to the direction of the shorter side is determined. Then, the rotation angle of the target character is determined based on the angle between the first and second straight lines. The target character is then corrected according to this rotation angle. This method eliminates the reliance on models for correcting irregular characters, simplifies the implementation process, and overcomes the cumbersome nature of existing methods for correcting irregular text.
[0090] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.
[0091] See Figure 1 , Figure 1 A flowchart of a text correction method provided in this application embodiment includes the following steps:
[0092] S101. Obtain an image containing irregular text; the irregular text includes target characters.
[0093] When text correction is needed for an image containing irregular text, you first need to input an image containing irregular text into the text correction tool; the text correction tool then retrieves the image containing irregular text and performs the text correction process.
[0094] When an image is input, especially one containing irregular text, binarization is the first step. Binarization involves subtracting each pixel value from 0 and 255. Pixels whose absolute difference from 0 is less than the absolute difference from 255 are assigned 0 (pixels closer to darkness). Pixels whose absolute difference from 0 is not less than the absolute difference from 255 are assigned 255 (pixels closer to light are assigned 255). This addresses the influence of color on the image, widens the contrast between light and dark areas, and highlights the text within the image.
[0095] S102. Determine the minimum bounding rectangle of the target character in the image, wherein a pair of opposite sides of the minimum bounding rectangle are parallel to the horizontal direction; and determine the region enclosed by multiple vertices of the target character in the image, wherein the region enclosed by multiple vertices of the target character is used to determine the maximum inscribed rectangle of the target character; wherein two adjacent sides of the maximum inscribed rectangle are not equal.
[0096] In one possible implementation, the minimum bounding rectangle of the target character can be achieved as follows:
[0097] The left, right, top, and bottom boundaries of the target character in the image are determined. The left and right boundaries are parallel to the vertical direction, and the top and bottom boundaries are parallel to the horizontal direction. The left, right, top, and bottom boundaries are used to determine the minimum bounding rectangle of the target character. The determined minimum bounding rectangle is a square rectangle.
[0098] After binarizing the image containing irregular text, a deep learning model is used to segment the image by character line, resulting in multiple sub-images. Each sub-image contains only one line of text from the original irregular text image. This allows the text correction tool to correct each line of text. After correcting each line, the text correction tool can synthesize the sub-images according to the original line order, and then combine the text-corrected sub-images into a single output image.
[0099] After segmenting an image of irregular text into character rows, determining the left, right, top, and bottom boundaries of the target character in the image is equivalent to determining the left, right, top, and bottom boundaries of the target character in a target sub-image among multiple sub-images.
[0100] The left, right, top, and bottom boundaries are used to determine the minimum bounding rectangle of the target character. Determining the region enclosed by multiple vertices of the target character in an image including irregular text is equivalent to determining the region enclosed by multiple vertices of the target character in a target sub-image within multiple sub-images. Then, the maximum inscribed rectangle of the target character is determined within the region enclosed by the multiple vertices of the target character; the two adjacent sides of this maximum inscribed rectangle are not equal. This limitation is based on the writing rules of Chinese characters, namely, Chinese characters are square. Making the two adjacent sides of the maximum inscribed rectangle unequal makes it easier to find the character's deflection direction; usually, the shorter side of the maximum inscribed rectangle represents the character's deflection direction.
[0101] S103. Within the smallest circumscribed rectangle, determine a first straight line perpendicular to the horizontal direction, and within the largest inscribed rectangle, determine a second straight line perpendicular to the direction of the shorter side.
[0102] Within the smallest bounding rectangle, determine a first straight line perpendicular to the horizontal direction. The direction of this first line represents the direction in which the character is not deflected. Within the largest inscribed rectangle, determine a second straight line perpendicular to the direction of the shorter side. The direction of this second line represents the direction in which the character is deflected.
[0103] S104. Determine the rotation angle of the target character based on the angle between the first straight line and the second straight line.
[0104] The rotation angle of the target character can be determined by the angle between the first and second straight lines. Usually, only the part above the horizontal line at the intersection of the first and second straight lines is considered, and the angle between the first and second straight lines is taken as the deflection angle of the character.
[0105] S105. Correct the target character according to the rotation angle of the target character.
[0106] Adjust the direction of the target character according to its rotation angle so that it aligns with the direction of the first straight line. The direction of the target character refers to its rotation direction; in other words, adjust the target character to its unrotated state. This completes the correction of the target character.
[0107] This application provides a text correction method. When executing the method, firstly, an image containing irregular text is acquired; then, the smallest bounding rectangle of the target character in the image is determined, wherein one pair of opposite sides of the smallest bounding rectangle is parallel to the horizontal direction; next, the largest inscribed rectangle of the target character in the image is determined, wherein two adjacent sides of the largest inscribed rectangle are unequal; within the smallest bounding rectangle, a first straight line perpendicular to the horizontal direction is determined, and within the largest inscribed rectangle, a second straight line perpendicular to the direction of the shorter side is determined; then, the rotation angle of the target character is determined based on the angle between the first and second straight lines; finally, the target character is corrected according to the rotation angle. In this way, determining the rotation angle of the target character based on the angle between the first and second straight lines, and correcting the target character based on the rotation angle, eliminates the dependence on models in the implementation of irregular character correction, making the implementation process simpler and overcoming the cumbersome problem of existing methods for irregular text correction.
[0108] In an optional embodiment of this application, the left and right boundaries of the target character in a target sub-image among multiple sub-images can be determined in the following ways:
[0109] Construct a first matrix, which is M rows and 1 column, and all values in the first matrix are 1; where M is the same as the number of pixel rows in the target sub-image; for example, the first matrix Y = [1,1,1…1]. ^T The image is scanned from left to right using the first matrix. This is done to identify and define the boundaries between each column of blank areas and the character areas in the image.
[0110] The first matrix slides across the image from left to right, with a step size of one pixel. During the slide, the transpose of the column matrix corresponding to the pixel values of each column in the target sub-image is multiplied by the first matrix to obtain multiple first values.
[0111] Assuming the target sub-image is illustrated as follows: Figure 2 As shown, the target sub-image has 100 rows and 200 columns, represented by (w, h) = (200, 100), where w represents the width and h represents the height. An image with width w and height h is represented in rows and columns as (200, 100); 200 represents 200 columns of pixels, and 100 represents 100 rows of pixels. Since M has the same number of rows and columns as the target sub-image, M = h = 100 in this case.
[0112] Suppose you slide to the 20th column y of the target sub-image. 20 Then, matrix multiplication is performed to obtain the first value, value1 = y. 20 ^ T *Y. Since the pixels in the image are either 0 or 255, if the value1 obtained by sliding to a column is 25500, that is, 255*h = 25500 (h = 100 in this embodiment), 25500 is the first preset value, indicating that this column is completely blank, which is very likely the interval between two characters. Then continue sliding until you encounter the previous column where value1 ≠ 25500. If value1 ≠ 25500, it means that you have encountered a place where characters exist. The pixel column corresponding to the first value that is not equal to the first preset value is the target pixel column.
[0113] Then, the pixel column to the left of the target pixel column that equals the first preset value is determined as the left boundary of the target character; the pixel column to the right of the target pixel column that equals the first preset value is determined as the right boundary of the target character. A schematic diagram of the character boundary is shown below. Figure 3 As shown, the right boundary of the previous character and the left boundary of the next character are as follows: Figure 3 As shown.
[0114] It should be noted that, in order to avoid splitting a character with left and right radicals into two characters, when determining the left and right boundaries, the pixel column corresponding to the first preset value can be determined as the left and right boundaries of the character if there is a continuous pixel column corresponding to a value equal to the first preset value, and the continuous pixel column exceeds a threshold.
[0115] In optional embodiments of this application, the upper and lower boundaries of the target characters in a target sub-image among multiple sub-images can be determined in the following ways:
[0116] Construct a second matrix, which is 1 row and N columns, with each value being 1. N is the same as the number of pixel columns between the left and right boundaries of the target character. If the number of pixel columns between the left and right boundaries of the target character is 20, then N = 20. Let the second matrix be X = [1,1,1…1]. This setting is used to find the upper and lower boundaries of the target character.
[0117] The second matrix is placed within the left and right boundaries of the target character and slid downwards with a step size of one pixel. During the sliding, the row matrix corresponding to the pixel values of each pixel row in the target sub-image is multiplied by the transpose of the second matrix to obtain multiple second values.
[0118] Suppose you slide to the 10th row X of the image. 10 Then, matrix multiplication is performed to obtain the second value, value2 = X. 10 *X ^T Since the pixels in the image are either 0 or 255, if you slide to a row, the resulting value2 will be 5100, which is 255 * N (N = 20 in this embodiment) = 5100. 5100 is the second preset value, indicating that this row is completely blank, most likely representing a gap between two lines of text. Continue sliding until you encounter the previous row where value2 ≠ 5100. Value2 ≠ 5100 indicates that you have encountered a row containing characters. The pixel row corresponding to the second value (not equal to the second preset value) will be designated as the target pixel row.
[0119] Then, the pixel row above the target pixel row, corresponding to the second preset value, is determined as the upper boundary of the target character; the pixel row below the target pixel row, corresponding to the second preset value, is determined as the lower boundary of the target character. In other words, the row above the target character that is blank is taken as the upper boundary of the target character; the row below the target character that is blank is taken as the lower boundary of the target character.
[0120] A diagram of character boundaries is shown below. Figure 4 As shown, the upper boundary and lower boundary of a certain line of characters are as follows: Figure 4 As shown.
[0121] It should be noted that, in order to avoid splitting a character with a top and bottom radical into two characters, when determining the top and bottom boundaries, the pixel column corresponding to the second preset value can be determined as the top and bottom boundaries of the character if there are consecutive pixel rows corresponding to values equal to the second preset value and the consecutive pixel rows exceed a threshold.
[0122] After finding the left, right, top, and bottom boundaries of the target character using the above method, the font angle is identified using the rules of Chinese character writing. One of the most important principles of Chinese character writing is squareness and regularity. Therefore, based on the left, right, top, and bottom boundaries of the target character, the minimum bounding rectangle of the target character can be determined. The minimum bounding rectangle of the target character is as follows: Figure 5A As shown.
[0123] After obtaining the minimum bounding rectangle of the target character, within the frame of the minimum bounding rectangle, a maximum inscribed rectangle is obtained within the region enclosed by multiple vertices of the target character. For example, if the target character is "素", the region enclosed by multiple vertices of the character "素" is as shown in Figure 5B as shown, and the obtained maximum inscribed rectangle is as shown in Figure 5C as shown. It can be understood that the obtained maximum inscribed rectangle refers to the maximum inscribed rectangle obtained within the region enclosed by multiple vertices of the target character.
[0124] After obtaining the minimum bounding rectangle and the maximum inscribed rectangle of the target character, these two rectangles are merged. The schematic diagram is as shown in Figure 6 as shown. For example, Figure 6 shows the schematic diagram of the merger of the minimum bounding rectangle and the maximum inscribed rectangle of the character "因"; and the schematic diagram of the merger of the minimum bounding rectangle and the maximum inscribed rectangle of the character "素"; then the rotation angle of the target character is determined according to the angle between the first straight line and the second straight line.
[0125] In an optional embodiment of the present application, after correcting the target character according to the rotation angle of the target character, the target character can also be adjusted to the same line. Specifically, it can be implemented in the following manner:
[0126] Obtain the ordinate of the midpoint of the rotated target characters located on the same line in the picture; and calculate the average value of the ordinates corresponding to multiple rotated target characters;
[0127] Move the multiple rotated target characters in the picture in the vertical direction so that the target characters move to the horizontal line where the average value is located. In this way, the moved target characters are on the same line, and the further adjustment of the target characters is completed. In addition, any straight line can be selected as the reference line, and then the center of the target character is moved to this reference line. Those skilled in the art should know the above. The above methods for adjusting the target position are not used to limit the present invention, and other methods are also within the protection scope of the present invention.
[0128] In an optional embodiment of the present application, correcting the target character according to the rotation angle of the target character can be implemented in the following manner:
[0129] Judge whether the rotation angle of the target character is greater than the preset angle. If it is greater, the target character is corrected according to the rotation angle of the target character. If it is less than the preset angle, it can be approximately considered that the target character has not rotated, and the target character does not need to be corrected. The preset angle can be set according to the angle at which the character deflection cannot be visually detected. For example, the preset angle can be set to 0.5 degrees.
[0130] Specifically, taking the angle relationship above the intersection point of the first straight line within the largest inscribed rectangle of the target character and the second straight line within the largest inscribed rectangle as a reference, if the first straight line is to the right of the second straight line, such as... Figure 7A As shown, and with a rotation angle of α, the target character is rotated counterclockwise by α according to the rotation angle α of the target character.
[0131] Similarly, taking the angle relationship between the first straight line within the largest inscribed rectangle of the target character and the second straight line within the largest inscribed rectangle as a reference, if the first straight line is to the left of the second straight line, such as... Figure 7B As shown, and with a rotation angle of α, the target character is rotated clockwise by α according to the rotation angle α of the target character.
[0132] Generally, the rotation angle of the target character will not exceed 180 degrees. If it exceeds 180 degrees, the image can be rotated 180 degrees first, so that the deflection angle is within 180 degrees, and then the target character can be adjusted according to the method described above. Those skilled in the art should understand that the above-described method for correcting the angle of the target character is not intended to limit the present invention, and other methods are also within the scope of protection of the present invention.
[0133] The above describes some specific implementations of a text correction method provided in this application. Based on this, this application also provides a corresponding apparatus. The apparatus provided in this application will be described below from the perspective of functional modularity.
[0134] See Figure 8 , Figure 8 This is a schematic diagram of a text correction device provided in an embodiment of the present application. The device includes: an acquisition module 801, a determination module 802, and a correction module 803.
[0135] The acquisition module 801 is specifically used to acquire images including irregular text;
[0136] The determining module 802 is specifically used to determine the minimum bounding rectangle of the target character in the image, wherein a pair of opposite sides of the minimum bounding rectangle are parallel to the horizontal direction; and to determine the region enclosed by multiple vertices of the target character in the image, wherein the region enclosed by multiple vertices is used to determine the maximum inscribed rectangle of the target character; wherein two adjacent sides of the maximum inscribed rectangle are not equal; within the minimum bounding rectangle, a first straight line perpendicular to the horizontal direction is determined, and within the maximum inscribed rectangle, a second straight line perpendicular to the direction of the shorter side is determined; and the rotation angle of the target character is determined based on the angle between the first straight line and the second straight line.
[0137] The correction module 803 is specifically used to correct the target character according to the rotation angle of the target character.
[0138] This application provides a text correction device for executing a corresponding method. When executing the method, firstly, an image containing irregular text is acquired; then, the smallest bounding rectangle in the image is determined; and, the region enclosed by multiple vertices of the target character in the image is determined, the region enclosed by the multiple vertices of the target character is used to determine the largest inscribed rectangle of the target character; the two adjacent sides of the largest inscribed rectangle are unequal; within the smallest bounding rectangle, a first straight line perpendicular to the horizontal direction is determined, and within the largest inscribed rectangle, a second straight line perpendicular to the direction of the shorter side is determined; then, the rotation angle of the target character is determined based on the angle between the first and second straight lines; finally, the target character is corrected according to the rotation angle. Thus, by determining the rotation angle of the target character based on the angle between the first and second straight lines, and correcting the target character based on the rotation angle, the means of correcting irregular characters is freed from dependence on models, making the implementation process simpler and overcoming the cumbersome problem of existing methods for irregular text correction.
[0139] In an optional embodiment of this application, the determining module 802 is specifically used for:
[0140] The left, right, top, and bottom boundaries of the target character in the image are determined. The left and right boundaries are parallel to the vertical direction, and the top and bottom boundaries are parallel to the horizontal direction. The left, right, top, and bottom boundaries are used to determine the minimum bounding rectangle of the target character.
[0141] In an optional embodiment of this application, the device further includes a segmentation module, which is specifically used to segment the image containing irregular text into multiple sub-images by character rows.
[0142] The determining module is specifically used to determine the left boundary, right boundary, top boundary, and bottom boundary of the target character in the target sub-image among the plurality of sub-images; and to determine the region enclosed by multiple vertices of the target character in the target sub-image among the plurality of sub-images.
[0143] In an optional embodiment of this application, the determining module 802 specifically includes a construction unit, a calculation unit, and a determining unit;
[0144] The construction unit is specifically used to construct a first matrix, which has M rows and 1 column, and each element in the first matrix has a value of 1; wherein M is the same as the number of pixel rows of the target sub-image;
[0145] The calculation unit is specifically used to multiply the transpose of the column matrix corresponding to the pixel values of each pixel column in the target sub-image with the first matrix to obtain multiple first values;
[0146] The determining unit is specifically used to determine the pixel column corresponding to the first value that is not equal to the first preset value as the target pixel column; to determine the pixel column to the left of the target pixel column that is equal to the first preset value as the left boundary of the target character; and to determine the pixel column to the right of the target pixel column that is equal to the first preset value as the right boundary of the target character.
[0147] In an optional embodiment of this application, the construction unit is specifically used to construct a second matrix, which has 1 row and N columns, and each element in the second matrix has a value of 1; wherein, N is the same as the number of pixel columns between the left and right boundaries of the target character;
[0148] The calculation unit is specifically used to multiply the row matrix corresponding to the pixel values of each pixel row in the target sub-image with the transpose of the second matrix to obtain multiple second values;
[0149] The determining unit is specifically used to determine the pixel row corresponding to the second value which is not equal to the second preset value as the target pixel row; to determine the pixel row above the target pixel row corresponding to the second value which is equal to the second preset value as the upper boundary of the target character; and to determine the pixel row below the target pixel row corresponding to the second value which is equal to the second preset value as the lower boundary of the target character.
[0150] In an optional embodiment of this application, the device further includes a moving module, which is specifically used for:
[0151] After the target character is corrected according to its rotation angle, the multiple rotated target characters in the image are moved to the same horizontal line in the vertical direction.
[0152] In an optional embodiment of this application, the mobile module is specifically used for:
[0153] Obtain the ordinate of the midpoint of the rotated target character located in the same row in the image containing irregular text.
[0154] Calculate the average of the ordinates of multiple rotated target characters;
[0155] Move multiple rotated target characters in the image containing irregular text to the horizontal line where the average value is located.
[0156] In an optional embodiment of this application, the correction module 803 is specifically used for:
[0157] If the rotation angle is greater than the preset angle, the target character is corrected according to the rotation angle of the target character.
[0158] See Figure 9 , Figure 9 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. The electronic device includes a memory 901 and a processor 902, wherein the memory 901 is used to store programs;
[0159] Processor 902 calls the program and uses it for:
[0160] Retrieve images containing irregular text;
[0161] The minimum bounding rectangle of the target character in the image is determined, wherein a pair of opposite sides of the minimum bounding rectangle are parallel to the horizontal direction; and, the region enclosed by multiple vertices of the target character in the image is determined, wherein the region enclosed by multiple vertices of the target character is used to determine the maximum inscribed rectangle of the target character; the two adjacent sides of the maximum inscribed rectangle are not equal.
[0162] Within the smallest circumscribed rectangle, a first straight line perpendicular to the horizontal direction is determined, and within the largest inscribed rectangle, a second straight line perpendicular to the direction of the shorter side is determined.
[0163] The rotation angle of the target character is determined based on the angle between the first straight line and the second straight line;
[0164] The target character is corrected according to its rotation angle.
[0165] This application also provides a corresponding computer storage medium for implementing the solution provided in this application.
[0166] In the embodiments of this application, the terms "first" and "second" are used only as name identifiers and do not represent the order of first and second.
[0167] As can be seen from the above description of the embodiments, those skilled in the art can clearly understand that all or part of the steps in the methods of the above embodiments can be implemented by means of software plus a general-purpose hardware platform. Based on this understanding, the technical solution of this application can be embodied in the form of a software product. This computer software product can be stored in a storage medium, such as a read-only memory (ROM) / RAM, magnetic disk, optical disk, etc., including several instructions to cause a computer device (which may be a personal computer, a server, or a network communication device such as a router) to execute the methods described in various embodiments or some parts of the embodiments of this application.
[0168] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on its differences from other embodiments. In particular, the apparatus embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions in the method embodiments. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without creative effort.
[0169] The above are merely exemplary embodiments of this application and are not intended to limit the scope of protection of this application.
Claims
1. A text correction method, characterized in that, The method includes: Obtain an image containing irregular text; the irregular text includes target characters; The minimum bounding rectangle of the target character in the image is determined, wherein a pair of opposite sides of the minimum bounding rectangle are parallel to the horizontal direction; and, the region enclosed by multiple vertices of the target character in the image is determined, wherein the region enclosed by multiple vertices of the target character is used to determine the maximum inscribed rectangle of the target character; the two adjacent sides of the maximum inscribed rectangle are not equal. Within the smallest circumscribed rectangle, a first straight line perpendicular to the horizontal direction is determined, and within the largest inscribed rectangle, a second straight line perpendicular to the direction of the shorter side is determined. The rotation angle of the target character is determined based on the angle between the first straight line and the second straight line; The target character is corrected according to its rotation angle; Determining the minimum bounding rectangle of the target character in the image specifically includes: The left, right, top, and bottom boundaries of the target character in the image are determined. The left and right boundaries are parallel to the vertical direction, and the top and bottom boundaries are parallel to the horizontal direction. The left, right, top, and bottom boundaries are used to determine the minimum bounding rectangle of the target character.
2. The method according to claim 1, characterized in that, The method further includes: The image containing irregular text is divided into multiple sub-images by character line. Determining the left, right, top, and bottom boundaries of the target character in the image specifically includes: Determine the left, right, top, and bottom boundaries of the target character in the target sub-image among the plurality of sub-images; The determination of the region enclosed by multiple vertices of the target character in the image specifically includes: Determine the region enclosed by multiple vertices of the target character in the target sub-image among the multiple sub-images.
3. The method according to claim 2, characterized in that, Determining the left and right boundaries of the target character in the target sub-image among the plurality of sub-images specifically includes: Construct a first matrix, which has M rows and 1 column, and all elements in the first matrix have a value of 1; wherein M is the same as the number of pixel rows of the target sub-image; Multiply the transpose of the column matrix corresponding to the pixel values of each pixel column in the target sub-image with the first matrix to obtain multiple first values; The pixel column corresponding to the first value that is not equal to the first preset value is determined as the target pixel column; The pixel column to the left of the target pixel column that is equal to the first preset value is determined as the left boundary of the target character; the pixel column to the right of the target pixel column that is equal to the first preset value is determined as the right boundary of the target character.
4. The method according to claim 3, characterized in that, Determining the upper and lower boundaries of the target character in the target sub-image among the plurality of sub-images specifically includes: Construct a second matrix with 1 row and N columns, where each element in the second matrix has a value of 1; wherein N is the same as the number of pixel columns between the left and right boundaries of the target character; Multiply the row matrix corresponding to the pixel values of each pixel row in the target sub-image with the transpose of the second matrix to obtain multiple second values; Determine the pixel row corresponding to the second value that is not equal to the second preset value; The pixel row above the target pixel row, corresponding to the second value equal to the second preset value, is determined as the upper boundary of the target character; the pixel row below the target pixel row, corresponding to the second value equal to the second preset value, is determined as the lower boundary of the target character.
5. The method according to claim 2, characterized in that, After correcting the target character according to its rotation angle, the method further includes: In the vertical direction, multiple rotated target characters in the image are moved to the same horizontal line.
6. The method according to claim 5, characterized in that, Moving multiple rotated target characters in the image to the same horizontal line specifically includes: Obtain the ordinate of the midpoint of the rotated target character located in the same row in the image containing irregular text. Calculate the average of the ordinates of multiple rotated target characters; Move multiple rotated target characters in the image containing irregular text to the horizontal line where the average value is located.
7. The method according to any one of claims 1-6, characterized in that, The step of correcting the target character according to its rotation angle specifically includes: If the rotation angle is greater than the preset angle, the target character is corrected according to the rotation angle of the target character.
8. A text correction device, characterized in that, The device includes: an acquisition module, a determination module, and a correction module; The acquisition module is specifically used to acquire an image containing irregular text; the irregular text includes target characters. The determining module is specifically used to determine the minimum bounding rectangle of the target character in the image, wherein a pair of opposite sides of the minimum bounding rectangle are parallel to the horizontal direction; and to determine the region enclosed by multiple vertices of the target character in the image, wherein the region enclosed by multiple vertices is used to determine the maximum inscribed rectangle of the target character; the two adjacent sides of the maximum inscribed rectangle are not equal; within the minimum bounding rectangle, a first straight line perpendicular to the horizontal direction is determined, and within the maximum inscribed rectangle, a second straight line perpendicular to the direction of the shorter side is determined; and the rotation angle of the target character is determined based on the angle between the first straight line and the second straight line. The correction module is specifically used to correct the target character according to the rotation angle of the target character; The determining module is specifically used for: The left, right, top, and bottom boundaries of the target character in the image are determined. The left and right boundaries are parallel to the vertical direction, and the top and bottom boundaries are parallel to the horizontal direction. The left, right, top, and bottom boundaries are used to determine the minimum bounding rectangle of the target character.
9. An electronic device, characterized in that, include: Memory and processor; The memory is used to store programs; The processor calls the program and uses it for: Obtain an image containing irregular text; the irregular text includes target characters; The minimum bounding rectangle of the target character in the image is determined, wherein a pair of opposite sides of the minimum bounding rectangle are parallel to the horizontal direction; and the region enclosed by multiple vertices of the target character in the image is determined, wherein the region enclosed by multiple vertices is used to determine the maximum inscribed rectangle of the target character; wherein two adjacent sides of the maximum inscribed rectangle are not equal. Within the smallest circumscribed rectangle, a first straight line perpendicular to the horizontal direction is determined, and within the largest inscribed rectangle, a second straight line perpendicular to the direction of the shorter side is determined. The rotation angle of the target character is determined based on the angle between the first straight line and the second straight line; The target character is corrected according to its rotation angle; Determining the minimum bounding rectangle of the target character in the image specifically includes: The left, right, top, and bottom boundaries of the target character in the image are determined. The left and right boundaries are parallel to the vertical direction, and the top and bottom boundaries are parallel to the horizontal direction. The left, right, top, and bottom boundaries are used to determine the minimum bounding rectangle of the target character.
Citation Information
Patent Citations
3D scene text picture synthetic method and system
CN108416828A
Irregular text correction method and device, computer equipment and storage medium
CN111242126A