A method, device, equipment and storage medium for removing blank spaces in text images
By preprocessing and opening operations on text images, and automatically calculating and cropping the white space positions, the problem of cumbersome white space in the prior art is solved, and the processing efficiency and OCR recognition accuracy are improved.
Patent Information
- Application Number
- CN202111554301.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-17
- Publication Date
- 2025-06-27
- Estimated Expiration
- 2041-12-17
AI Technical Summary
The prior art removes the blanking of text images by manually marking the white space position of the image, which is cumbersome and inefficient.
By preprocessing the text image, a binarized image is obtained, and the operation is performed to remove noise, traverse the image rows and columns to count the pixel points, set the starting point to find the pixel value mutation point, calculate the white space position, and crop the image.
It realizes automatic removal of white space in text images, improves processing efficiency, reduces the impact on text information extraction, and improves OCR recognition accuracy.
Smart Images

Figure CN114240890B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of image processing, and in particular, to a method, device, equipment and storage medium for removing white space in text images. Background Art
[0002] Large white spaces will appear at the upper, lower, left and right edges of contract text images. In the image OCR text recognition technology, the image will be scaled to a fixed size for recognition. Scaling an image with white space will cause a large loss of text information features; thus, OCR text recognition of such images will affect the recognition accuracy of text information.
[0003] To solve the above problems, the prior art manually marks the positions of the upper, lower, left and right white spaces of the image, and then crops the image to obtain an image after removing the upper, lower, left and right white spaces of the image. The process of removing the upper, lower, left and right white spaces of the image by this method is relatively cumbersome and the efficiency is extremely low. Summary of the Invention
[0004] The object of the present invention is to provide a method, device, equipment and storage medium for removing white space in text images, aiming to solve the problems of cumbersome and low efficiency in the process of removing the upper, lower, left and right white spaces of the image by manually marking the positions of the upper, lower, left and right white spaces of the image in the prior art.
[0005] To solve the above technical problems, the object of the present invention is achieved by the following technical solutions: providing a method for removing white space in text images, including:
[0006] Preprocessing the text image to obtain a binary image;
[0007] Performing opening operation on the binary image to obtain a binary image containing only text features;
[0008] Traversing the rows and columns in the binary image, and counting all pixel points in each row and each column;
[0009] Setting an upper starting point and a lower starting point in the binary image, respectively starting from the upper starting point and the lower starting point to traverse the pixel points in each row and finding the corresponding pixel value mutation points, and calculating the upper white space position and the lower white space position according to the corresponding pixel value mutation points;
[0010] Setting a left starting point and a right starting point in the binary image, respectively starting from the left starting point and the right starting point to traverse the pixel points in each column and finding the corresponding pixel value mutation points, and calculating the left white space position and the right white space position according to the corresponding pixel value mutation points;
[0011] According to the obtained upper blank position, lower blank position, left blank position, and right blank position, crop the text image to obtain a target image with blanks removed.
[0012] In addition, the technical problem to be solved by the present invention is also to provide a device for removing blanks from a text image, including:
[0013] A preprocessing unit for preprocessing a text image to obtain a binary image;
[0014] An opening operation unit for performing an opening operation on the binary image to obtain a binary image containing only text features;
[0015] A statistical unit for traversing rows and columns in the binary image and counting all pixel points in each row and each column;
[0016] An upper and lower blank calculation unit for setting an upper starting point and a lower starting point in the binary image, traversing pixel points in each row starting from the upper starting point and the lower starting point respectively to find corresponding pixel value mutation points, and calculating the upper blank position and the lower blank position according to the corresponding pixel value mutation points;
[0017] A left and right blank calculation unit for setting a left starting point and a right starting point in the binary image, traversing pixel points in each column starting from the left starting point and the right starting point respectively to find corresponding pixel value mutation points, and calculating the left blank position and the right blank position according to the corresponding pixel value mutation points;
[0018] A cropping unit for cropping the text image according to the obtained upper blank position, lower blank position, left blank position, and right blank position to obtain a target image with blanks removed.
[0019] In addition, an embodiment of the present invention further provides a computer device, which includes a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the computer program, the method for removing blanks from a text image described in the first aspect above is implemented.
[0020] In addition, an embodiment of the present invention further provides a computer-readable storage medium, where the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the processor is caused to execute the method for removing blanks from a text image described in the first aspect above.
[0021] An embodiment of the present invention discloses a method, device, equipment and storage medium for removing blank spaces in a text image. The method includes preprocessing the text image to obtain a binary image; performing an opening operation on the binary image to obtain a binary image containing only text features; traversing the rows and columns in the binary image and counting all the pixel points in each row and column; setting upper, lower, left, and right starting points in the binary image respectively, starting from the upper, lower, left, and right starting points to traverse the pixel points in each row and column and finding the corresponding pixel value mutation points, and calculating the upper blank space position, lower blank space position, left blank space position, and right blank space position according to the corresponding pixel value mutation points; and cropping the text image according to all the obtained blank space positions to obtain a target image with blank spaces removed. The embodiment of the present invention can reduce the influence of the blank space position of the image on the extraction of contract-like text information and lays a foundation for the subsequent OCR recognition accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following will briefly introduce the drawings required for the description of the embodiments. Obviously, the following drawings are some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.
[0023] Figure 1 It is a flowchart of the method for removing blank spaces in a text image provided by an embodiment of the present invention;
[0024] Figure 2 It is a sub-flowchart of the method for removing blank spaces in a text image provided by an embodiment of the present invention;
[0025] Figure 3 It is another sub-flowchart of the method for removing blank spaces in a text image provided by an embodiment of the present invention;
[0026] Figure 4 It is another sub-flowchart of the method for removing blank spaces in a text image provided by an embodiment of the present invention;
[0027] Figure 5 It is another sub-flowchart of the method for removing blank spaces in a text image provided by an embodiment of the present invention;
[0028] Figure 6 It is another sub-flowchart of the method for removing blank spaces in a text image provided by an embodiment of the present invention;
[0029] Figure 7 It is another sub-flowchart of the method for removing blank spaces in a text image provided by an embodiment of the present invention;
[0030] Figure 8 It is an example diagram of the horizontal projection of the binary image provided by an embodiment of the present invention;
[0031] Figure 9 It is a schematic block diagram of the text image blank space removing device provided by the embodiment of the present invention;
[0032] Figure 10 It is a schematic block diagram of the computer device provided by the embodiment of the present invention. Specific embodiments
[0033] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
[0034] It should be understood that when used in this specification and the appended claims, the terms "comprising" and "including" indicate the presence of the described features, wholes, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or their combinations.
[0035] It should also be understood that the terms used in this specification of the present invention are only for the purpose of describing specific embodiments and are not intended to limit the present invention. As used in this specification of the present invention and the appended claims, unless the context clearly indicates otherwise, the singular forms "a", "an" and "the" are intended to include the plural forms.
[0036] It should be further understood that the term " / and / " used in this specification of the present invention and the appended claims refers to any combination and all possible combinations of one or more of the related listed items, and includes these combinations.
[0037] Please refer to Figure 1 , Figure 1 It is a schematic flowchart of the text image blank space removing method provided by the embodiment of the present invention;
[0038] As Figure 1 shown, the method includes steps S101 to S106.
[0039] S101. Preprocess the text image to obtain a binary image;
[0040] S102. Perform an opening operation on the binary image to obtain a binary image containing only text features;
[0041] The main purpose of steps S101 - S102 is to remove the noise in the text image and obtain a binarized image containing text features, which facilitates the subsequent calculation of the blank space positions.
[0042] S103. Traverse the rows and columns in the binarized image, and count all the pixel points in each row and each column;
[0043] This step mainly counts the pixel points of text features and the pixel points of background features in each row and each column.
[0044] S104. Set the upper starting point and the lower starting point in the binarized image. Starting from the upper starting point and the lower starting point respectively, traverse the pixel points in each row and find the corresponding pixel value mutation points, and calculate the upper blank space position and the lower blank space position according to the corresponding pixel value mutation points;
[0045] S105. Set the left starting point and the right starting point in the binarized image. Starting from the left starting point and the right starting point respectively, traverse the pixel points in each column and find the corresponding pixel value mutation points, and calculate the left blank space position and the right blank space position according to the corresponding pixel value mutation points;
[0046] In steps S104 - S105, by analyzing the pixel value mutation points of the pixel points in each row and the pixel value mutation points of the pixel points in each column, the blank space positions are calculated and obtained. The specific calculation method will be described later.
[0047] S106. According to the obtained upper blank space position, lower blank space position, left blank space position and right blank space position, crop the text image and obtain the target image with blank space removed.
[0048] For contract - type text images, the methods in steps S101 - S106 are used to calculate the upper blank space position, lower blank space position, left blank space position and right blank space position of the text image, and the main text information area in the text image is cropped using these blank space positions, thereby reducing the influence of the blank space positions on the contract - type text information extraction and making the OCR text recognition result have higher accuracy and robustness.
[0049] In an embodiment, as Figure 2 shown, step S101 includes:
[0050] S201. Convert the text image into a grayscale image and perform Gaussian smoothing filtering on the grayscale image;
[0051] S202. Perform binarization processing on the grayscale image to obtain a binarized image;
[0052] S203. According to the preset feature threshold, distinguish the text features and background features in the binarized image.
[0053] In this embodiment, image conversion algorithms such as FreeImage and FormatConvertedBitmap Class can be used to convert the text image into a grayscale image, and then perform Gaussian smoothing filtering on the grayscale image to remove some noises in the grayscale image; the grayscale image is binarized to obtain a binarized image, so that the pixel values of the pixel points on the image are represented as 0 or 255; finally, according to the preset feature threshold, the text feature and the background feature can be distinguished.
[0054] In one embodiment, as Figure 3 shown, step S102 includes:
[0055] S301. First perform erosion processing on the binarized image, and then perform dilation processing;
[0056] S302. Count the number of eight-connected regions in the binarized image and the area of each eight-connected region;
[0057] S303. According to the preset area threshold, filter out the eight-connected regions with an area smaller than the area threshold to obtain a binarized image containing only text features, where the pixel values of the pixel points of the text features are 255 and the pixel values of the pixel points of the background features are 0.
[0058] In this embodiment, after the binarized image is first subjected to erosion processing, the noises in the binarized image can be removed, but the binarized image is also compressed. Then, the eroded binarized image is subjected to dilation processing to restore the binarized image compressed during the erosion process to its original state.
[0059] Count the number of eight-connected regions in the binarized image and the area of each eight-connected region, where the eight-connected region refers to the connection relationship between the pixel points of the binary image, that is, the region that is connected among up, down, left, right, upper left, upper right, lower left, and lower right is an eight-connected region.
[0060] Generally speaking, the larger the area of the eight-connected region, the more likely it is that there is text in the region; through the preset area threshold, filter out the eight-connected regions with an area smaller than the preset area threshold. These filtered eight-connected regions are basically small noise-isolated regions, and the remaining eight-connected regions are basically text, so as to more clearly highlight the text features in the binarized image.
[0061] In one embodiment, as Figure 4 shown, step S104 includes:
[0062] S401. In the height direction of the binary image, starting from the upper left corner as the upper starting point, traverse to the height / / 3-th row in the top-down direction, where height / / 3 is the integer obtained by dividing the total number of rows in the binary image by 3.
[0063] S402. Calculate and obtain the first candidate upper blank position and the second candidate upper blank position according to the following upper blank conditions:
[0064] The first candidate upper blank position is the maximum value of i that satisfies the conditions h_white[i] <= h_mean / / 2 and h_white[i] == 0. The second candidate upper blank position is the maximum value of i that satisfies the condition h_white[i] <= h_mean / / 2, where h_white[i] is the total number of pixel points of the text feature in the i-th row, h_mean / / 2 is the integer obtained by dividing the average number of pixel points of the text feature in each row by 2, and h_white[i] == 0 means that the pixel value of the pixel points in the i-th row is 0, where == means equal to (e.g., A == A means 'true', A == B means 'false').
[0065] S403. Take the smaller value of the first candidate upper blank position and the second candidate upper blank position as the upper blank position.
[0066] Steps S401 - S403 are the process of calculating the upper blank position. Refer to Figure 8 as shown Figure 8 is a schematic diagram of the binary image after horizontal projection; in the figure, the upper left corner is the coordinate origin (0, 0), the image height is height, and the width is white; the white area in the figure is the text feature; the process of calculating the upper blank position is: starting from the upper left corner as the upper starting point, traverse to the height / / 3-th row. It can be seen that the area of max1_height in the figure can satisfy that i is the maximum value under the conditions h_white[i] <= h_mean / / 2 and h_white[i] == 0, that is, the area of max1_height is the first candidate upper blank position; the area of max2_height can satisfy that i is the maximum value under the condition h_white[i] <= h_mean / / 2, that is, at max2_height is the second candidate upper blank position. Take the smaller value of the first candidate upper blank position and the second candidate upper blank position as the upper blank position, that is, the first candidate upper blank position as the final upper blank position.
[0067] For ease of understanding, a specific example is introduced below. Assume that the height of the binary image is 15, and there are text feature pixels in rows 4 to 12. The calculation process of the upper blank position is as follows: Traverse to the 15 / / 3-th row, that is, the integer part of 15 divided by 3 is equal to 5. Traverse to the 5th row. During the traversal, in the conditions h_white[i] <= h_mean / / 2 and h_white[i] == 0, because the condition h_white[i] == 0 makes i can only take [1, 4], so the maximum value of i that satisfies the conditions h_white[i] <= h_mean / / 2 and h_white[i] == 0 can only be 4, that is, the first candidate upper blank position can only be the area of rows 1 to 4; and under the condition h_white[i] <= h_mean / / 2, when the total number of text feature pixels in the 5th row is small, there may be a situation where i = 5 also satisfies this condition. Therefore, the maximum value of i that satisfies the condition h_white[i] <= h_mean / / 2 can be 5, that is, the second candidate upper blank position can be the area of rows 1 to 5 at most. Finally, take the smaller value of the first candidate upper blank position and the second candidate upper blank position as the upper blank position, that is, the area of rows 1 to 4.
[0068] In one embodiment, as Figure 5 shown, step S104 further includes the process of calculating the lower blank position:
[0069] S501. In the height direction of the binary image, starting from the lower left corner as the lower starting point and traversing in the up-down direction to the 2*height / / 3-th row, where 2*height / / 3 is the integer part of 2 times the total number of rows on the binary image divided by 3;
[0070] In this step, continue to illustrate with the above specific example. Here, it is traversed in reverse order to the 2*height / / 3-th row, that is, from the 15th row traversed in reverse order to the 10th row.
[0071] S502. Calculate and obtain the first candidate lower blank position and the second candidate lower blank position according to the following lower blank conditions:
[0072] The first candidate lower blank position is the minimum value of i that satisfies the conditions h_white[i] <= h_mean / / 2 and h_white[i] == 0, and the second candidate lower blank position is the minimum value of i that satisfies the condition h_white[i] <= h_mean / / 2;
[0073] S503. Take the larger value of the first candidate lower blank position and the second candidate lower blank position as the lower blank position.
[0074] It can be understood that according to the conditions in steps S502 - S503, based on the calculation method introduced above, the lower blank position can be calculated.
[0075] In one embodiment, as Figure 6 shown, step S105 includes:
[0076] S601. In the width direction of the binary image, starting from the upper left corner as the left starting point and traversing to the width / / 10th row in the left - to - right direction, where width / / 10 is the integer obtained by dividing the total number of columns in the binary image by 10.
[0077] S602. Calculate and obtain the first candidate left blank position and the second candidate left blank position according to the following left - blank conditions:
[0078] The first candidate left blank position is the maximum value of i that satisfies the conditions w_white[i] <= w_mean / / 5 and w_white[i] == 0, and the second candidate left blank position is the maximum value of i that satisfies the condition w_white[i] <= w_mean / / 5; where w_white[i] is the total number of pixel points of the text feature in the i - th column, w_mean / / 5 is the integer obtained by dividing the average number of pixel points of the text feature in each column by 5, and w_white[i] == 0 means that the pixel value of the pixel points in the i - th column is 0.
[0079] S603. Take the smaller value of the first candidate left blank position and the second candidate left blank position as the left blank position.
[0080] It can be understood that according to the conditions in steps S601 - S603, based on the calculation method introduced above, the left blank position can be calculated.
[0081] In one embodiment, as Figure 7 shown, step S105 further includes:
[0082] S701. In the width direction of the binary image, starting from the upper right corner as the right starting point and traversing to the 9*width / / 10th row in the right - to - left direction, where 9*width / / 10 is the integer obtained by dividing 9 times the total number of columns in the binary image by 10.
[0083] S702. Calculate and obtain the first candidate right blank position and the second candidate right blank position according to the following right - blank conditions:
[0084] The first candidate right blank position is the minimum value of i that satisfies the conditions w_white[i] <= w_mean / / 5 and w_white[i] == 0, and the second candidate right blank position is the minimum value of i that satisfies the condition w_white[i] <= w_mean / / 5.
[0085] S703. Take the larger value of the first candidate right blank space position and the second candidate right blank space position as the right blank space position.
[0086] It can be understood that according to the conditions in steps S701 - S703 and based on the calculation method introduced above, the right blank space position can be calculated.
[0087] The embodiment of the present invention also provides a text image blank space removal device, which is used to execute any embodiment of the foregoing text image blank space removal method. Specifically, please refer to Figure 9 , Figure 9 which is a schematic block diagram of the text image blank space removal device provided by the embodiment of the present invention.
[0088] As Figure 9 shown, the text image blank space removal device 900 includes: a preprocessing unit 901, an opening operation unit 902, a statistics unit 903, an up and down blank space calculation unit 904, a left and right blank space calculation unit 905, and a cropping unit 906.
[0089] The preprocessing unit 901 is used to preprocess the text image to obtain a binary image;
[0090] The opening operation unit 902 is used to perform an opening operation on the binary image to obtain a binary image containing only text features;
[0091] The statistics unit 903 is used to traverse the rows and columns in the binary image and count all the pixel points in each row and each column;
[0092] The up and down blank space calculation unit 904 is used to set an upper starting point and a lower starting point in the binary image, start traversing the pixel points in each row from the upper starting point and the lower starting point respectively and find the corresponding pixel value mutation points, and calculate the upper blank space position and the lower blank space position according to the corresponding pixel value mutation points;
[0093] The left and right blank space calculation unit 905 is used to set a left starting point and a right starting point in the binary image, start traversing the pixel points in each column from the left starting point and the right starting point respectively and find the corresponding pixel value mutation points, and calculate the left blank space position and the right blank space position according to the corresponding pixel value mutation points;
[0094] The cropping unit 906 is used to crop the text image according to the obtained upper blank space position, lower blank space position, left blank space position, and right blank space position, and obtain the target image with blank space removed.
[0095] The device analyzes the pixel value mutation points of the pixels in each row and the pixel value mutation points of the pixels in each column, thereby calculating and obtaining the blank positions. According to all the obtained blank positions, the text image is cropped to obtain the target image after blank removal, which can reduce the influence of the blank positions of the image on the extraction of contract text information and lay a foundation for the subsequent OCR recognition accuracy.
[0096] Those skilled in the art can clearly understand that for the convenience and simplicity of description, the specific working processes of the above-described device and unit can refer to the corresponding processes in the foregoing method embodiments and will not be described herein again.
[0097] The above text image blank removal device can be implemented in the form of a computer program, and the computer program can run on a computer device as shown in Figure 10 shown.
[0098] Please refer to Figure 10 , Figure 10 which is a schematic block diagram of the computer device provided by an embodiment of the present invention. The computer device 1000 is a server, and the server can be an independent server or a server cluster composed of multiple servers.
[0099] Refer to Figure 10 , the computer device 1000 includes a processor 1002, a memory, and a network interface 1005 connected through a system bus 1001. Among them, the memory can include a non-volatile storage medium 1003 and an internal memory 1004.
[0100] The non-volatile storage medium 1003 can store an operating system 10031 and a computer program 10032. When the computer program 10032 is executed, the processor 1002 can be caused to execute the image blank removal method.
[0101] The processor 1002 is used to provide computing and control capabilities to support the operation of the entire computer device 1000.
[0102] The internal memory 1004 provides an environment for the operation of the computer program 10032 in the non-volatile storage medium 1003. When the computer program 10032 is executed by the processor 1002, the processor 1002 can be caused to execute the image blank removal method.
[0103] The network interface 1005 is used for network communication, such as providing the transmission of data information, etc. Those skilled in the art can understand that Figure 10The structure shown is only a block diagram of some structures related to the solution of the present invention, and does not constitute a limitation on the computer device 1000 to which the solution of the present invention is applied. Specifically, the computer device 1000 may include more or fewer components than those shown in the figure, or combine some components, or have different component arrangements.
[0104] Those skilled in the art can understand that Figure 10 the embodiments of the computer device shown do not constitute a limitation on the specific composition of the computer device. In other embodiments, the computer device may include more or fewer components than those shown in the figure, or combine some components, or have different component arrangements. For example, in some embodiments, the computer device may only include a memory and a processor. In such an embodiment, the structures and functions of the memory and the processor are the same as those Figure 10 in the shown embodiment and will not be described in detail here.
[0105] It should be understood that in the embodiments of the present invention, the processor 1002 may be a central processing unit (CPU), and the processor 1002 may also be other general-purpose processors, digital signal processors (DSPs), application specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Among them, the general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.
[0106] In another embodiment of the present invention, a computer-readable storage medium is provided. The computer-readable storage medium may be a non-volatile computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for removing image blank spaces in the embodiments of the present invention is implemented.
[0107] The storage medium is a physical, non-transitory storage medium, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a magnetic disk, or an optical disc, etc., which are all physical storage media that can store program codes.
[0108] Those skilled in the art can clearly understand that for the convenience and brevity of description, the specific working processes of the above-described devices, apparatuses, and units can refer to the corresponding processes in the foregoing method embodiments and will not be described in detail here.
[0109] As described above, it is only the specific implementation manner of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art within the technical scope disclosed by the present invention can easily think of various equivalent modifications or substitutions, and these modifications or substitutions should be covered within the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.
Claims
1. A method for removing white space in text images, characterized in that, Including: Preprocess the text image to obtain a binary image; Perform an opening operation on the binary image to obtain a binary image containing only text features; Traverse the rows and columns in the binary image and count all the pixel points in each row and each column; Set an upper starting point and a lower starting point in the binary image, start traversing the pixel points in each row from the upper starting point and the lower starting point respectively, find the corresponding pixel value mutation points, and calculate the upper blank space position and the lower blank space position according to the corresponding pixel value mutation points; Specifically, the maximum and minimum values of i under the condition that the total number of pixel points of the text features in the i-th row is less than or equal to the integer part of the average value of the pixel points of the text features in each row divided by 2, and the pixel value of the pixel points in the i-th row is 0, are used as the first candidate upper blank space position and the first candidate lower blank space position respectively; the maximum and minimum values of i under the condition that the total number of pixel points of the text features in the i-th row is less than or equal to the integer part of the average value of the pixel points of the text features in each row divided by 2 are used as the second candidate upper blank space position and the second candidate lower blank space position respectively; take the smaller value of the first candidate upper blank space position and the second candidate upper blank space position as the upper blank space position; Take the larger value of the first candidate lower blank space position and the second candidate lower blank space position as the lower blank space position; Set a left starting point and a right starting point in the binary image, start traversing the pixel points in each column from the left starting point and the right starting point respectively, find the corresponding pixel value mutation points, and calculate the left blank space position and the right blank space position according to the corresponding pixel value mutation points; Specifically, the maximum and minimum values of i under the condition that the total number of pixel points of the text features in the i-th column is greater than or equal to the integer part of the average value of the pixel points of the text features in each column divided by 5, and the pixel value of the pixel points in the i-th column is 0, are used as the first candidate left blank space position and the first candidate right blank space position respectively; the maximum and minimum values of i under the condition that the total number of pixel points of the text features in the i-th column is greater than or equal to the integer part of the average value of the pixel points of the text features in each column divided by 5 are used as the second candidate left blank space position and the second candidate right blank space position respectively; take the smaller value of the first candidate left blank space position and the second candidate left blank space position as the left blank space position; Take the larger value of the first candidate right blank space position and the second candidate right blank space position as the right blank space position; Crop the text image according to the obtained upper blank space position, lower blank space position, left blank space position and right blank space position, and obtain a target image with blank space removed.
2. The text image blank space removal method according to claim 1, characterized in that The preprocessing of the text image to obtain a binary image includes: Convert the text image into a grayscale image and perform Gaussian smoothing filtering on the grayscale image; Perform binary processing on the grayscale image to obtain a binary image; Distinguish the text features and background features in the binary image according to a preset feature threshold.
3. The method for removing blank spaces in a text image according to claim 1, characterized in that, After performing the opening operation on the binary image, obtaining a binary image containing only text features includes: First perform erosion processing on the binary image, and then perform dilation processing; Count the number of eight-connected regions and the area of each eight-connected region in the binary image; Filter out the eight-connected regions with an area smaller than the set area threshold according to a preset area threshold to obtain a binary image containing only text features, where the pixel value of the pixel points of the text features is 255 and the pixel value of the pixel points of the background features is 0.
4. The method for removing blank spaces in a text image according to claim 1, characterized in that, Setting an upper starting point and a lower starting point in the binary image, respectively traversing the pixel points in each row starting from the upper starting point and the lower starting point to find the corresponding pixel value mutation points, and calculating the upper blank position and the lower blank position according to the corresponding pixel value mutation points, including: In the height direction of the binary image, take the upper left corner as the upper starting point and traverse to the height / / 3-th row in the top-to-bottom direction, where height / / 3 is the integer obtained by dividing the total number of rows in the binary image by 3; Calculate and obtain the first candidate upper blank position and the second candidate upper blank position according to the following upper blank conditions: The first candidate upper blank position is the maximum value of i that satisfies h_white[i] <= h_mean / / 2 and h_white[i] == 0, and the second candidate upper blank position is the maximum value of i that satisfies h_white[i] <= h_mean / / 2, where h_white[i] is the total number of pixel points of the text features in the i-th row, h_mean / / 2 is the integer obtained by dividing the average number of pixel points of the text features in each row by 2, and h_white[i] == 0 means that the pixel value of the pixel points in the i-th row is 0; Take the smaller value of the first candidate upper blank position and the second candidate upper blank position as the upper blank position.
5. The method for removing blank spaces in a text image according to claim 4, characterized in that, Setting an upper starting point and a lower starting point in the binary image, respectively traversing the pixel points in each row starting from the upper starting point and the lower starting point to find the corresponding pixel value mutation points, and calculating the upper blank position and the lower blank position according to the corresponding pixel value mutation points, further includes: In the height direction of the binary image, take the lower left corner as the lower starting point and traverse to the 2*height / / 3-th row in the bottom-to-top direction, where 2*height / / 3 is the integer obtained by dividing 2 times the total number of rows in the binary image by 3; Calculate and obtain the first candidate lower blank position and the second candidate lower blank position according to the following lower blank conditions: The first candidate lower blank position is the minimum value of i that satisfies h_white[i] <= h_mean / / 2 and h_white[i] == 0, and the second candidate lower blank position is the minimum value of i that satisfies h_white[i] <= h_mean / / 2; Take the larger value of the first candidate lower blank position and the second candidate lower blank position as the lower blank position.
6. The text image blank space removal method according to claim 5, wherein Setting a left starting point and a right starting point in the binary image, respectively traversing the pixel points in each column starting from the left starting point and the right starting point to find the corresponding pixel value mutation points, and calculating the left blank position and the right blank position, including: In the width direction of the binary image, starting from the upper left corner as the left starting point and traversing to the width / / 10-th row in the left-to-right direction, where width / / 10 is the integer obtained by dividing the total number of columns in the binary image by 10. Calculate and obtain the first candidate left blank position and the second candidate left blank position according to the following left blank condition: The first candidate left blank position is the maximum value of i that satisfies w_white[i] <= w_mean / / 5 and w_white[i] == 0, and the second candidate left blank position is the maximum value of i that satisfies w_white[i] <= w_mean / / 5; where w_white[i] is the total number of pixel points of the text feature in the i-th column, w_mean / / 5 is the integer obtained by dividing the average number of pixel points of the text feature in each column by 5, and w_white[i] == 0 means that the pixel value of the pixel points in the i-th column is 0. Take the smaller value of the first candidate left blank position and the second candidate left blank position as the left blank position.
7. The method for removing blank spaces in a text image according to claim 6, wherein Setting the left starting point and the right starting point in the binary image, respectively starting from the left starting point and the right starting point to traverse the pixel points in each column and find the corresponding pixel value mutation points, and calculating the left blank position and the right blank position according to the corresponding pixel value mutation points, further includes: In the width direction of the binary image, starting from the upper right corner as the right starting point and traversing to the 9*width / / 10-th row in the right-to-left direction, where 9*width / / 10 is 9 times the integer obtained by dividing the total number of columns in the binary image by 10. Calculate and obtain the first candidate right blank position and the second candidate right blank position according to the following right blank condition: The first candidate right blank position is the minimum value of i that satisfies w_white[i] <= w_mean / / 5 and w_white[i] == 0, and the second candidate right blank position is the minimum value of i that satisfies w_white[i] <= w_mean / / 5. Take the larger value of the first candidate right blank position and the second candidate right blank position as the right blank position.
8. A text image blank space removal device, characterized in that, Includes: A preprocessing unit for preprocessing the text image to obtain a binary image; An opening operation unit for performing an opening operation on the binary image to obtain a binary image containing only text features; A statistical unit for traversing the rows and columns in the binary image and counting all pixel points in each row and each column. The upper and lower blank space calculation unit is used to set the upper starting point and the lower starting point in the binary image, start traversing the pixel points in each row from the upper starting point and the lower starting point respectively to find the corresponding pixel value mutation points, and calculate the upper blank space position and the lower blank space position according to the corresponding pixel value mutation points; specifically, the maximum value and the minimum value of i under the conditions that the total number of pixel points satisfying the text features of the i-th row is less than or equal to the integer obtained by dividing the average number of pixel points of the text features in each row by 2, and the pixel value of the pixel points in the i-th row is 0, are used as the first candidate upper blank space position and the first candidate lower blank space position respectively; the maximum value and the minimum value of i under the condition that the total number of pixel points satisfying the text features of the i-th row is less than or equal to the integer obtained by dividing the average number of pixel points of the text features in each row by 2 are used as the second candidate upper blank space position and the second candidate lower blank space position respectively; take the smaller value of the first candidate upper blank space position and the second candidate upper blank space position as the upper blank space position; take the larger value of the first candidate lower blank space position and the second candidate lower blank space position as the lower blank space position; The left and right blank space calculation unit is used to set the left starting point and the right starting point in the binary image, start traversing the pixel points in each column from the left starting point and the right starting point respectively to find the corresponding pixel value mutation points, and calculate the left blank space position and the right blank space position according to the corresponding pixel value mutation points; specifically, the maximum value and the minimum value of i under the conditions that the total number of pixel points satisfying the text features of the i-th column is greater than or equal to the integer obtained by dividing the average number of pixel points of the text features in each column by 5, and the pixel value of the pixel points in the i-th column is 0, are used as the first candidate left blank space position and the first candidate right blank space position respectively; the maximum value and the minimum value of i under the condition that the total number of pixel points satisfying the text features of the i-th column is greater than or equal to the integer obtained by dividing the average number of pixel points of the text features in each column by 5 are used as the second candidate left blank space position and the second candidate right blank space position respectively; take the smaller value of the first candidate left blank space position and the second candidate left blank space position as the left blank space position; Take the larger value of the first candidate right blank space position and the second candidate right blank space position as the right blank space position; The cropping unit is used to crop the text image according to the obtained upper blank space position, lower blank space position, left blank space position and right blank space position, and obtain the target image with blank spaces removed.
9. A computer device, comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the text image blank space removal method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, and when the computer program is executed by the processor, the processor is enabled to execute the text image blank space removal method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Character recognition method and device, electronic equipment and storage medium
CN113505745A