Text segmentation method, electronic device, and storage medium
By acquiring target image features for enhancement processing and rectangular contour correction, the problem of low segmentation accuracy in image text segmentation with inconsistent formats is solved, achieving more efficient text segmentation results.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HONG FU JIN PRECISION IND (SHENZHEN) CO LTD
- Filing Date
- 2021-07-22
- Publication Date
- 2026-04-14
AI Technical Summary
Existing technologies have low accuracy in image text segmentation for images with inconsistent text formats and low-resolution images, especially in license plate recognition and ID card recognition.
By acquiring the features of the target image, performing enhancement processing, detecting closed edges, determining and correcting the first rectangular contour, the second rectangular contour is obtained, and finally text segmentation is achieved.
It improves the accuracy and efficiency of text segmentation, especially in the precise segmentation of text in IC package printed images.
Smart Images

Figure CN115700824B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of image detection, and more particularly to a text segmentation method, electronic device, and storage medium. Background Technology
[0002] Image text segmentation is used in many applications, such as license plate recognition and ID card recognition. Currently, the main method for text segmentation is through training neural network models. This method requires a large amount of data samples for training and has high requirements for the format, size, and quality of the text. When the text format in an image is not fixed or the clarity is insufficient, inaccurate detection can easily occur. Summary of the Invention
[0003] In view of the above, it is necessary to provide a text segmentation method, electronic device, and storage medium that can improve the accuracy of text segmentation.
[0004] This application provides a text segmentation method, the method comprising: acquiring image features of a target image; performing enhancement processing on the target image based on the image features to obtain an enhanced image; detecting closed edges of text in the enhanced image; determining a first rectangular contour corresponding to the text based on the closed edges; correcting the first rectangular contour to obtain a second rectangular contour; and performing text segmentation on the target image based on the second rectangular contour.
[0005] In one possible implementation, the image features include: pixel mean, standard deviation, image length, and width. The enhancement process of the target image based on these image features to obtain an enhanced image includes: comparing the pixel mean with a preset mean threshold, and performing image inversion processing on the binarized image based on the comparison result to obtain an inverted image; matching a denoising algorithm corresponding to the standard deviation; determining the kernel parameters of the denoising algorithm based on the image length and width, and using the denoising algorithm to denoise the inverted image to obtain the enhanced image.
[0006] In one possible implementation, determining the first rectangular outline of the text based on the closed edge includes: generating a blank image of the same size as the target image; generating a rectangular outline on the blank image according to the closed edge to obtain a outline image, wherein the position of the rectangular outline in the outline image is the same as the position of the outline corresponding to the closed edge in the target image; detecting all outlines in the outline image; and determining each outline as the first rectangular outline.
[0007] In one possible implementation, correcting the first rectangular outline to obtain the second rectangular outline includes: calculating the average area, average horizontal spacing, and average vertical spacing of the first rectangular outline; correcting the first rectangular outline based on the average area, the average horizontal spacing, and the average vertical spacing to obtain the second rectangular outline corresponding to the text.
[0008] In one possible implementation, calculating the average area, average horizontal spacing, and average vertical spacing of the first rectangular contour includes: determining the coordinates of the four vertices of each first rectangular contour; determining the width and length of each first rectangular contour based on the vertex coordinates; determining the average area based on the width and the length; determining horizontally adjacent first rectangular contours based on the vertex coordinates; calculating the average horizontal spacing of the horizontally adjacent first rectangular contours; determining vertically adjacent first rectangular contours based on the vertex coordinates; and calculating the average vertical spacing of the vertically adjacent first rectangular contours.
[0009] In one possible implementation, the step of correcting the first rectangular outline based on the average area and the average spacing to obtain the second rectangular outline corresponding to the text includes: determining the adjacent horizontal spacing between two horizontally adjacent first rectangular outlines and the adjacent vertical spacing between two vertically adjacent first rectangular outlines; merging the first rectangular outlines based on the adjacent horizontal spacing and the adjacent vertical spacing to obtain candidate rectangular frames; removing candidate rectangular frames that are smaller than a first preset multiple of the average area and splitting candidate rectangular frames that are larger than a second preset multiple of the average area to obtain the second rectangular outline.
[0010] In one possible implementation, the step of merging the first rectangular outlines based on the adjacent horizontal spacing and the adjacent vertical spacing to obtain a candidate rectangular frame includes: merging two first rectangular outlines corresponding to adjacent horizontal spacings that are smaller than the average horizontal spacing; and merging two first rectangular outlines corresponding to adjacent vertical spacings that are smaller than the average vertical spacing.
[0011] In one possible implementation, detecting the edges of text in the enhanced image includes: binarizing the enhanced image to obtain a binarized image; performing morphological processing on the binarized image to obtain a morphological image; performing edge detection on the morphological image to obtain an edge image; and extracting the edges in the edge image.
[0012] This application also provides an electronic device, which includes a processor and a memory, wherein the processor is used to implement the text segmentation method by executing a computer program stored in the memory.
[0013] This application also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the text segmentation method described above.
[0014] The text segmentation method and related equipment disclosed in this application enhance the target image by acquiring its image features to obtain an enhanced image. Closed edges of the characters in the enhanced image are detected, and a first rectangular contour corresponding to the characters is determined based on these closed edges. The first rectangular contour is then corrected to obtain a second rectangular contour, and text segmentation is performed on the target image based on the second rectangular contour. By enhancing the target image and performing text segmentation based on the enhanced image, the accuracy and efficiency of text segmentation can be improved. Attached Figure Description
[0015] Figure 1 This is a schematic diagram of the structure of an electronic device that implements a preferred embodiment of the text segmentation method of this application.
[0016] Figure 2 This is a flowchart of a preferred embodiment of a text segmentation method disclosed in this application.
[0017] Figure 3 This is an exemplary target image disclosed in this application.
[0018] Figure 4 This is an exemplary closed edge map disclosed in this application.
[0019] Figure 5 This is an exemplary rectangular outline diagram disclosed in this application.
[0020] Figure 6 This is an exemplary first rectangular outline diagram disclosed in this application.
[0021] Figure 7 This is an exemplary splitting of the first rectangular outline disclosed in this application.
[0022] Figure 8 This is an exemplary second rectangular outline acquisition image disclosed in this application.
[0023] Figure 9 This is an exemplary text segmentation effect comparison diagram disclosed in this application. Detailed Implementation
[0024] To make the objectives, technical solutions, and advantages of this application clearer, the application will be described in detail below with reference to the accompanying drawings and specific embodiments.
[0025] Please see Figure 1 , Figure 1This is a schematic diagram of an electronic device according to an embodiment of this application. (See attached diagram) Figure 1 As shown, the electronic device 1 includes, but is not limited to, a memory 11 and at least one processor 12. These components can be connected via a bus or directly.
[0026] The electronic device 1 can be a computer, mobile phone, tablet computer, personal digital assistant (PDA), or other device with an application installed. Those skilled in the art will understand that the illustration... Figure 1 This is merely an example of electronic device 1 and does not constitute a limitation on electronic device 1. It may include more or fewer components than shown, or combine certain components, or different components. For example, electronic device 1 may also include input / output devices, network access devices, buses, etc.
[0027] like Figure 2 The diagram shown is a flowchart of a preferred embodiment of the text segmentation method of this application. The text segmentation method is applied in the electronic device 1. Depending on different requirements, the order of the steps in the flowchart can be changed, and some steps can be omitted. In this embodiment, the text segmentation method includes:
[0028] S11. Obtain the image features of the target image.
[0029] In order to perform subsequent enhancement processing on the target image, it is necessary to obtain the image features of the target image, and then perform enhancement processing on the target image based on the image features.
[0030] In this embodiment, the target image is an image containing text, such as an IC package printing image. During the IC packaging process, text needs to be printed onto the IC. To ensure clear and complete printing, the text in the IC package printing image needs to be precisely segmented.
[0031] In one implementation, the target image can be a black and white image, for example, white text on a black background, or white text on a black background. When the target image is a black and white image, the pixel mean, standard deviation, image length, and width of the target image are calculated as image features of the target image.
[0032] In one implementation, the target image can be a color image. When the target image is a color image, it is first binarized to obtain a binary image with white text on a black background or white text on a black background. Then, the pixel mean, standard deviation, image length, and width of the binarized image are calculated as image features of the target image.
[0033] S12. Enhance the target image based on the image features to obtain an enhanced image.
[0034] Since different target images require different parameters, enhancing the target image based on its features makes it more personalized, and the resulting enhanced image helps improve the segmentation effect of text in the target image.
[0035] In this embodiment, the step of enhancing the target image based on the image features to obtain an enhanced image includes:
[0036] (1) Compare the pixel mean with a preset mean threshold, and perform image inversion processing on the target image based on the comparison result to obtain an inverted image.
[0037] During IC packaging, the IC packaging material can be plastic, metal, or ceramic. The images of the printed text on the IC package will differ depending on the packaging material, resulting in either black text on a white background or white text on a black background. For ease of subsequent processing, all IC package printed text images are converted to white text on a black background. Therefore, we determine whether the captured IC package printed text image is black text on a white background or white text on a black background based on the average pixel value.
[0038] In practice, a mean threshold is preset. The system determines whether the average pixel value is greater than the mean threshold. When the average pixel value is greater than the mean threshold, it indicates that white is the dominant color in the target image, meaning the binarized image is a white background with black text, and no image inversion processing is required. When the average pixel value is less than the mean threshold, it indicates that black is the dominant color in the target image, meaning the target image is a black background with white text, and the target image undergoes black-and-white tone inversion processing.
[0039] (2) Match the denoising algorithm corresponding to the standard deviation.
[0040] Even when using the same material, different light sources will result in varying levels of noise in the target image. The standard deviation is used to determine the amount of noise in the image, thus deciding which denoising method to use. Employing different denoising methods makes denoising more targeted and yields better results.
[0041] In practice, a standard deviation threshold is preset. It is then determined whether the standard deviation is greater than the threshold. If the standard deviation is greater than the threshold, it indicates that the inverted image has a lot of noise, and a Gaussian blur denoising algorithm can be used. If the standard deviation is less than the threshold, it indicates that the inverted image has less noise, and a bilateral filter denoising algorithm can be used.
[0042] (3) Determine the kernel parameters of the denoising algorithm based on the length and width of the image, and use the denoising algorithm to denoise the inverted image to obtain an enhanced image.
[0043] For example, an IC package printing image with a size of 1102 pixels * 1102 pixels is obtained. The calculated pixel mean is 48.97, and the standard deviation is 28.01. Assuming a mean threshold of 128 and a standard deviation threshold of 31 are set, since 48.97 is less than the mean threshold of 128, the IC package printing image is a black background with white text. The image is then subjected to a black-and-white tonal inversion, transforming it into a white background with black text. Since 28.01 is less than the standard deviation threshold of 31, the image has low noise, so a bilateral filter is used for noise reduction. Because the size of the IC package printing image is between the threshold values of 1000 and 2000, a kernel of 13*13 is selected for the noise reduction algorithm parameters.
[0044] By enhancing the target image using its image features, text can be segmented more accurately.
[0045] S13. Detect the closed edges of the text in the enhanced image.
[0046] To extract the outline of the text, its edges need to be extracted first. However, the text in the enhanced image may exhibit character breaks, for example... Figure 3 The text shown contains English letters, therefore all closed edges need to be extracted. See the extracted closed edges for details. Figure 4 As shown.
[0047] Edge detection algorithms can be used to detect closed edges of text in the enhanced image. However, considering the characteristics of IC package printed images, this embodiment can perform morphological processing on the enhanced image, including erosion and dilation, to obtain a morphological image. Edge detection is then performed on the morphological image to obtain an edge image. The closed edges are then extracted from the edge image.
[0048] By performing morphological processing on the enhanced image, it is easier to separate the text from the background. Edge detection based on morphological images can more easily detect and extract closed edges in the edge image, resulting in better extraction of closed edges.
[0049] S14. Determine the first rectangular outline corresponding to the text based on the closed edge.
[0050] In this embodiment, determining the first rectangular outline corresponding to the text based on the closed edge includes:
[0051] (1) Generate a blank image with the same size as the target image. In specific implementation, the blank image can be generated according to the length and width of the target image.
[0052] (2) A rectangular outline is generated on the blank image based on the closed edges to obtain a outline image. The position of the rectangular outline in the outline image is the same as the position of the outline image corresponding to the closed edge in the target image. In specific implementation, the outline corresponding to each closed edge is found in the target image, and the position coordinates of the outline are obtained. The rectangular outline is generated on the blank image based on the position coordinates. For example, Figure 5 .
[0053] (3) Detect all contours in the contour image. In specific implementation, since the text may be broken, for example, if the text is broken into two parts, two closed edges will be generated, thus generating two rectangular contours. These rectangular contours may overlap, and it is necessary to merge the overlapping rectangular contours. By performing contour searching on the contour image, overlapping rectangular contours can be merged into a single rectangular contour. For example, Figure 6 .
[0054] (4) Define each contour as the first rectangular contour.
[0055] By drawing the borders of all continuous outlines onto a blank image, all irregular borders will merge into a single complete rectangular border. Using two outline lookups and merging operations, the text outline is obtained. Furthermore, considering the possibility of broken text, merging all overlapping rectangular outlines generated by broken text improves the accuracy of text segmentation.
[0056] S15. Correct the first rectangular contour to obtain the second rectangular contour.
[0057] Step S14 merges overlapping contours in the broken text of the target image to obtain the first rectangular contour. However, other situations may exist, such as the contours in the broken text not overlapping, texts that are too close together being selected by the same first rectangular contour, and small characters that are considered noise or do not need to be segmented being selected by the first rectangular contour.
[0058] In this embodiment, correcting the first rectangular contour to obtain the second rectangular contour includes:
[0059] (1) Calculate the average area, average horizontal spacing, and average vertical spacing of the first rectangular contours. Specifically, a third coordinate system (XOY) is established with the lower left corner of the enhanced image when it is placed upright as the origin O, the horizontal axis as the X-axis, and the vertical axis as the Y-axis. The coordinates of the four vertices of each first rectangular contour are determined. The width and length of each first rectangular contour are determined based on the vertex coordinates, and the average area is calculated based on the width and length. Horizontally adjacent first rectangular contours are determined based on the vertex coordinates, and the average horizontal spacing of the horizontally adjacent first rectangular contours is calculated. Vertically adjacent first rectangular contours are determined based on the vertex coordinates, and the average vertical spacing of the vertically adjacent first rectangular contours is calculated.
[0060] (2) The first rectangular outline is corrected based on the average area, the average horizontal spacing, and the average vertical spacing to obtain the second rectangular outline corresponding to the text. Specifically, the adjacent horizontal spacing between two horizontally adjacent first rectangular outlines and the adjacent vertical spacing between two vertically adjacent first rectangular outlines are determined. Two first rectangular outlines with adjacent horizontal spacing smaller than the average horizontal spacing are merged, and two first rectangular outlines with adjacent vertical spacing smaller than the average vertical spacing are merged to obtain candidate rectangular frames. Candidate rectangular frames with an area smaller than a first preset multiple of the average area are removed, for example, punctuation marks ",". Candidate rectangular frames with an area larger than a second preset multiple of the average area are split, for example, ... Figure 7 The second rectangular outline is obtained. The first preset multiple is less than 1, and the second preset multiple is greater than 1.
[0061] Optionally, obtaining the candidate rectangle includes:
[0062] S501: Locate the first rectangular outline whose adjacent horizontal spacing is less than the average horizontal spacing;
[0063] S502: Merge the first rectangular contour according to the vertex coordinates of the first rectangular contour to obtain the fourth rectangular contour;
[0064] S503: Locate the fourth rectangular outline whose adjacent longitudinal spacing is less than the average longitudinal spacing;
[0065] S504: Merge the fourth rectangular contour according to the vertex coordinates of the fourth rectangular contour to obtain the fifth rectangular contour;
[0066] S505: Determine the adjacent horizontal spacing between two horizontally adjacent fifth rectangular contours, and the adjacent vertical spacing between two vertically adjacent fifth rectangular contours, and execute step 1;
[0067] S506: When the adjacent horizontal spacing is greater than the average horizontal spacing and the adjacent vertical spacing is greater than the average vertical spacing, the merging ends.
[0068] For example, such as Figure 8 As shown, the "X" is broken into five parts, thus generating five first rectangular outlines. In order to merge the five first rectangular outlines, the adjacent horizontal spacing between the first rectangular outlines is obtained, and the first rectangular outlines whose adjacent horizontal spacing is less than the average horizontal spacing are merged. The adjacent vertical spacing between the first rectangular outlines is obtained, and the first rectangular outlines whose adjacent vertical spacing is less than the average vertical spacing are merged to obtain the complete rectangular outline of the "X".
[0069] By correcting the outline of the first rectangle, the accuracy of text segmentation in the target image can be improved.
[0070] S16. Perform text segmentation on the target image based on the second rectangular outline.
[0071] In this embodiment, the second rectangular outline selects a complete character, so the target image can be segmented based on the second rectangular outline.
[0072] For example, Figure 9 The final text segmentation results are shown, with the left image being the text segmentation result obtained using a neural network model and the right image being the text segmentation result obtained using the text segmentation method of this application.
[0073] Please continue reading. Figure 1 In this embodiment, the memory 11 can be the internal memory of the electronic device 1, that is, the memory built into the electronic device 1. In other embodiments, the memory 11 can also be the external memory of the electronic device 1, that is, the memory externally connected to the electronic device 1.
[0074] In some embodiments, the memory 11 is used to store program code and various data, and to enable high-speed and automatic access to programs or data during the operation of the electronic device 1.
[0075] The memory 11 may include random access memory, and may also include non-volatile memory, such as hard disk, memory, plug-in hard disk, smart media card (SMC), secure digital (SD) card, flash card, at least one disk storage device, flash memory device, or other volatile solid-state storage device.
[0076] In one embodiment, the processor 12 may be a Central Processing Unit (CPU), or 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. The general-purpose processor may be a microprocessor, or the processor itself may be any other conventional processor.
[0077] If the program code and various data in the memory 11 are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments, such as the text segmentation method, can also be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include any entity or device capable of carrying the computer program code, a recording medium, a USB flash drive, a portable hard drive, a magnetic disk, an optical disk, a computer memory, a read-only memory (ROM), etc.
[0078] It is understood that the module division described above is a logical functional division, and there may be other division methods in actual implementation. Furthermore, the functional modules in the various embodiments of this application can be integrated into the same processing unit, or each module can exist physically separately, or two or more modules can be integrated into the same unit. The integrated modules described above can be implemented in hardware or in a combination of hardware and software functional modules.
[0079] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application and are not intended to limit it. Although this application has been described in detail with reference to preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions can be made to the technical solutions of this application without departing from the spirit and scope of the technical solutions of this application.
Claims
1. A text segmentation method, characterized in that, The text segmentation method includes: Obtain the image features of the target image; The target image is enhanced based on the image features to obtain an enhanced image; Detect the closed edges of the text in the enhanced image; The first rectangular outline corresponding to the text is determined based on the closed edge; The first rectangular contour is corrected to obtain the second rectangular contour, including: Calculating the average area, average horizontal spacing, and average vertical spacing of the first rectangular contour includes: determining the coordinates of the four vertices of each first rectangular contour; determining the width and length of each first rectangular contour based on the vertex coordinates; determining the average area based on the width and length; determining horizontally adjacent first rectangular contours based on the vertex coordinates; calculating the average horizontal spacing of the horizontally adjacent first rectangular contours; determining vertically adjacent first rectangular contours based on the vertex coordinates; and calculating the average vertical spacing of the vertically adjacent first rectangular contours. The process of correcting the first rectangular outline based on the average area, the average horizontal spacing, and the average vertical spacing to obtain the second rectangular outline corresponding to the text includes: determining the adjacent horizontal spacing between two horizontally adjacent first rectangular outlines and the adjacent vertical spacing between two vertically adjacent first rectangular outlines; merging the first rectangular outlines based on the adjacent horizontal spacing and the adjacent vertical spacing to obtain candidate rectangular frames, including: merging two first rectangular outlines corresponding to adjacent horizontal spacing smaller than the average horizontal spacing and merging two first rectangular outlines corresponding to adjacent vertical spacing smaller than the average vertical spacing; removing candidate rectangular frames smaller than a first preset multiple of the average area and splitting candidate rectangular frames larger than a second preset multiple of the average area to obtain the second rectangular outline; The target image is segmented into text based on the second rectangular outline.
2. The text segmentation method according to claim 1, characterized in that, The image features include: pixel mean, standard deviation, image length, and width. The enhancement processing of the target image based on the image features to obtain the enhanced image includes: The pixel mean is compared with a preset mean threshold, and the target image is inverted based on the comparison result to obtain an inverted image; A denoising algorithm that matches the standard deviation; The kernel parameters of the denoising algorithm are determined based on the image length and width, and the denoising algorithm is used to denoise the inverted image to obtain an enhanced image.
3. The text segmentation method according to claim 2, characterized in that, Determining the first rectangular outline of the text based on the closed edge includes: Generate a blank image of the same size as the target image; A rectangular outline is generated on the blank image based on the closed edge to obtain a outline image. The position of the rectangular outline in the outline image is the same as the position of the outline corresponding to the closed edge in the target image. Detect all contours in the contour image; Each contour is defined as the first rectangular contour.
4. The text segmentation method according to claim 2, characterized in that, The detection of the edges of text in the enhanced image includes: The enhanced image is binarized to obtain a binarized image; The binarized image is subjected to morphological processing to obtain a morphological image; Edge detection is performed on the morphological image to obtain an edge image; Extract the edges from the edge image.
5. An electronic device, characterized in that, The electronic device includes a processor and a memory, the processor being configured to execute a computer program stored in the memory to implement the text segmentation method as described in any one of claims 1 to 4.
6. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores at least one instruction, which, when executed by a processor, implements the text segmentation method as described in any one of claims 1 to 4.
Citation Information
Patent Citations
Character data generation based on transformed imaged data to identify nutrition-related data or other types of data
US20150169972A1