Table data extraction method, device, electronic device and storage medium
By selecting and operating line intersections in the document image, determining the target table outline and extracting cell data, the problems of difficult and low accuracy in the extraction of table data in the prior art are solved, and efficient and accurate extraction of tables of different sizes are achieved.
Patent Information
- Application Number
- CN202111496243.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-08
- Publication Date
- 2025-02-11
- Estimated Expiration
- 2041-12-08
AI Technical Summary
The prior art is difficult and has low accuracy when extracting table data, especially because the table sizes in different document images are different, and the universality of the exclusion rules is not good.
By responding to user input, obtaining the document image, selecting horizontal lines and vertical lines for bitwise operations to obtain intersection points, and then performing bitwise or operations to obtain the alternative table outline. Select the target table outline from the alternative contour based on the intersection point, obtain its position information, determine the table area and extract cell data.
This method can accurately extract table data for tables of different sizes, improve extraction accuracy, and reduce dependence on exclusion rules.
Smart Images

Figure CN114387606B_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of data processing, and specifically relates to a method, device, electronic device and storage medium for extracting table data. Background Art
[0002] When a user browses a document image, the document image may contain multiple contents such as tables, texts or pictures. In some cases, the user only cares about part of the contents in the document image, such as the table data in the document image. Therefore, in order to facilitate the user to quickly understand the table data in the document image, it is necessary to extract the table data from the document image.
[0003] In some scenarios, some lines in the document image that are not tables are excluded through exclusion rules to filter out tables and extract table data, but usually through the length, width or area of the lines. However, the universality of the exclusion rules is not very good, because the sizes of tables in different document images are different, and it is difficult to apply tables of different sizes according to the length, width or area of the lines. Therefore, the exclusion rules perform well on a certain document image, but may not be applicable to another document image. The extraction of table data is more difficult and the accuracy is lower. Summary of the invention
[0004] The embodiments of the present application provide a method, device, electronic device and storage medium for extracting table data, which can solve the problem in the related art that when data in a table needs to be extracted, the extraction of table data is difficult and the accuracy is low.
[0005] In a first aspect, an embodiment of the present application provides a method for extracting table data, including: acquiring a document image in response to a first input from a user; selecting a first horizontal line and a first vertical line in the document image, performing a bitwise AND operation on the first horizontal line and the first vertical line to obtain multiple first intersections, and performing a bitwise OR operation on the first horizontal line and the first vertical line to obtain at least one alternative table outline; selecting a target table outline from the alternative table outlines based on the first intersections, and acquiring position information of the target table outline in the document image, wherein there are multiple first intersections on the target table outline; determining a table area of the document image based on the position information, and extracting data of at least one cell from the table area.
[0006] In a second aspect, an embodiment of the present application provides a device for extracting table data, comprising: an acquisition module, for acquiring a document image in response to a first input from a user; a first selection module, for selecting a first horizontal line and a first vertical line in the document image, performing a bitwise AND operation on the first horizontal line and the first vertical line to obtain a plurality of first intersections, and performing a bitwise OR operation on the first horizontal line and the first vertical line to obtain at least one alternative table outline; a second selection module, for selecting a target table outline from the alternative table outlines based on the first intersections, and obtaining position information of the target table outline in the document image, wherein there are a plurality of the first intersections on the target table outline; and a determination module, for determining a table area of the document image based on the position information, and extracting data of at least one cell from the table area.
[0007] In a third aspect, an embodiment of the present application provides an electronic device, which includes a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the steps of the method described in the first aspect.
[0008] In a fourth aspect, an embodiment of the present application provides a readable storage medium, on which a program or instruction is stored, and when the program or instruction is executed by a processor, the steps of the method described in the first aspect are implemented.
[0009] In a fifth aspect, an embodiment of the present application provides a chip, comprising a processor and a communication interface, wherein the communication interface is coupled to the processor, and the processor is used to run a program or instruction to implement the method described in the first aspect.
[0010] In an embodiment of the present application, a document image is acquired in response to a first input from a user, a first horizontal line and a first vertical line in the document image are selected, a bitwise AND operation is performed on the first horizontal line and the first vertical line to obtain a plurality of first intersections, a bitwise OR operation is performed on the first horizontal line and the first vertical line to obtain at least one candidate table outline, a target table outline is selected from the candidate table outlines based on the first intersections, position information of the target table outline in the document image is obtained, there are a plurality of the first intersections on the target table outline, a table area of the document image is determined based on the position information, data of at least one cell is extracted from the table area, and for different document images, a target table outline that satisfies the requirements of the document image can be selected from the candidate table outlines, thereby determining the table area and extracting cell data from the table area, and the table data can be extracted relatively easily, and the accuracy of the extracted table data is improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] Figure 1 This is a first flow chart of a method for extracting table data provided in an embodiment of the present application;
[0012] Figure 2 This is a second flow chart of a method for extracting table data provided in an embodiment of the present application;
[0013] Figure 3 is a binary image of a first horizontal line provided in an embodiment of the present application;
[0014] Figure 4 is a binary image of a first vertical line provided in an embodiment of the present application;
[0015] Figure 5 This is a second flow chart of a method for extracting table data provided in an embodiment of the present application;
[0016] Figure 6 is a binary image of a first intersection provided in an embodiment of the present application;
[0017] Figure 7 is a schematic diagram of a reference table outline provided in an embodiment of the present application;
[0018] Figure 8 This is a third flow chart of a method for extracting table data provided in an embodiment of the present application;
[0019] Fig. 9 It is a binary line segment graph provided in an embodiment of the present application;
[0020] Fig.10 is a binary map of a second intersection provided in an embodiment of the present application;
[0021] Fig.11 It is a binary image of intersecting horizontal and vertical lines provided in an embodiment of the present application;
[0022] Fig.12 It is a structural schematic diagram of a device for extracting table data provided in an embodiment of the present application;
[0023] Fig.13 It is a schematic diagram of the structure of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION
[0024] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0025] The terms "first", "second", etc. in the specification and claims of the present application are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable under appropriate circumstances, so that the embodiments of the present application can be implemented in an order other than those illustrated or described here, and the objects distinguished by "first", "second", etc. are generally of one type, and the number of objects is not limited. For example, the first object can be one or more. In addition, "and / or" in the specification and claims represents at least one of the connected objects, and the character " / " generally indicates that the objects associated with each other are in an "or" relationship.
[0026] In some scenarios, some lines in the document image that are not tables are excluded through exclusion rules to filter out tables and extract table data. However, exclusion rules are usually based on the length, width or area of the lines. The universality of exclusion rules is not very good, because the sizes of tables in different document images are different. For tables with many rows and columns, the lines of some rows or columns may be very long or very short. In this way, it is very difficult to simply establish exclusion rules. It is difficult to apply tables of different sizes based on the length, width or area of the lines. Therefore, the exclusion rules perform well on a certain document image, but may not be applicable to another document image. Therefore, in many complex tables, it will become extremely difficult to simply write exclusion rules, the universality is not ideal, and there will be too many hyperparameters that need to be adjusted.
[0027] In conjunction with the accompanying drawings, a table data extraction method, device, electronic device and storage medium provided in the embodiments of the present application are described in detail through specific embodiments and their application scenarios.
[0028] Figure 1 A method for extracting table data provided by an embodiment of the present invention is shown. The method can be executed by an electronic device, such as a terminal device. That is, the method for extracting table data can be executed by hardware or software installed in the terminal device. The method includes the following steps:
[0029] Step S101: In response to a first input from a user, a document image is acquired.
[0030] Specifically, the document image can be any picture containing at least one table input by the user, and the area of the table in the document image is the table area. After the user inputs the document image, in order to facilitate the execution of subsequent steps, the document image can be converted into a grayscale image, and then the grayscale image is adaptively binarized using an adaptive binarization algorithm to obtain a binary image. It is worth noting that the document image not only contains at least one table, but also contains other text content and pictures.
[0031] Step S102: Select the first horizontal line and the first vertical line in the document image, perform a bitwise AND operation on the first horizontal line and the first vertical line to obtain a plurality of first intersections, and perform a bitwise OR operation on the first horizontal line and the first vertical line to obtain at least one candidate table outline.
[0032] Specifically, after the document image is acquired, the document image contains multiple horizontal lines and multiple vertical lines. A first horizontal line that meets the condition is selected from the multiple horizontal lines in the document image, and a first vertical line that meets the condition is selected from the multiple vertical lines.
[0033] Further, a bitwise OR operation is performed on the first horizontal line and the first vertical line, and a binary image of the intersection of the horizontal and vertical lines is outputted. A contour search is performed in the binary image to find all the external contours of the table as candidate table contours. The candidate table contours are called candidate table contours because many external contours will be obtained after the intersection of the horizontal and vertical lines, and it is not certain which is the contour of the table, so the multiple external contours are first used as candidate contours. The binary image contains multiple first intersection points, and the first intersection point is the intersection point after the first horizontal line and the first vertical line are intersected by a bitwise OR operation.
[0034] Step S103: selecting a target table outline from the candidate table outlines based on the first intersection points, and acquiring position information of the target table outline in the document image, wherein the target table outline has a plurality of first intersection points.
[0035] Specifically, the alternative table outline can be an outline that conforms to the outer frame of the table. The number of first intersections on some of the outlines in the alternative table outline may be small or even zero. The target table outline can be determined by the number of first intersections on the alternative table outline. The first intersection is obtained by the intersection of horizontal lines and vertical lines. In this way, useless horizontal lines and vertical lines are excluded as much as possible, and unnecessary first intersections that affect the selection of the target table outline are reduced, thereby improving the selection accuracy of the target table outline. For example, a contour in the alternative table outline whose number of first intersections is greater than a predetermined number is selected as the target table outline, wherein the predetermined number can be determined according to actual conditions, and the embodiments of the present application are not limited thereto.
[0036] Step S104: determining a table area of the document image according to the position information, and extracting data of at least one cell from the table area.
[0037] Specifically, the position information of the target table outline indicates the position of the target table outline in the table area of the document image, the table area includes at least one table, and a table includes multiple cells. Data of at least one cell can be extracted from the table area, such as extracting a row, a column, or the entire table data, etc. The cell data includes but is not limited to cell attributes and text data, etc. The cell attributes include but are not limited to length, width,
[0038] Through the technical solution disclosed in the embodiment of the present application, it is possible to select, for different document images, a target table outline that satisfies the requirements of the document image from the alternative table outlines, thereby determining the table area and extracting cell data from the table area, thereby making it easier to extract table data and improving the accuracy of the extracted table data.
[0039] In one possible implementation, Figure 2 As shown, selecting the first horizontal line and the first vertical line in the document image includes the following steps:
[0040] Step S1021: Adaptively binarize the document image and output a binarized document image.
[0041] Step S1022: Select all horizontal lines of the binary document image through the corrosion and expansion algorithm, merge the lines whose upper and lower spacing is less than the first threshold in the horizontal direction, and then select the lines whose line length exceeds the second threshold and the lines whose line width is less than the third threshold as the first horizontal lines.
[0042] Specifically, the lines in the horizontal direction (x direction) of the binary document image are selected through the corrosion and expansion algorithm, and then the lines with upper and lower spacing less than the first threshold are merged by the closing operation method. The lines with line length exceeding the second threshold and line width less than the third threshold are selected by the opening operation, and the lines with too short length and too wide width are excluded. The output is as follows Figure 3 Shown is a binarized image of only the first horizontal line in the horizontal direction.
[0043] Step S1023: Select all the lines in the vertical direction of the binary document image through the corrosion and expansion algorithm, merge the lines whose left and right spacing is less than the fourth threshold in the vertical direction, and then select the lines whose line length exceeds the fifth threshold and the lines whose line width is less than the sixth threshold as the first vertical lines.
[0044] Specifically, the lines in the vertical direction (y direction) of the binary document image are selected through the corrosion and expansion algorithm, and then the lines with left and right spacing less than the fourth threshold are merged by the closing operation method. Then the lines with line length exceeding the fifth threshold and line width less than the sixth threshold are selected by the opening operation, and the lines with too short length and too wide width are excluded. The output is as follows Figure 4 Shown is a binarized image of only the first vertical line in the vertical direction.
[0045] The first horizontal line and the first vertical line in the document image are selected by setting the selection conditions. For different tables, the first horizontal line and the first vertical line that meet the conditions of the table are selected. It has high versatility and can accurately locate the table area in the document image.
[0046] In one possible implementation, Figure 5 As shown, selecting a target table outline from candidate table outlines based on the first intersection point includes the following steps:
[0047] Step S1031: selecting a second horizontal line passing through the first intersection from the first horizontal lines, and selecting a second vertical line passing through the first intersection from the first vertical lines.
[0048] Specifically, a bitwise AND operation is performed on the first horizontal line and the first vertical line, and the output is as follows: Figure 6 The binary image shown only has the intersection points of horizontal and vertical lines, and finds all the first intersection points.
[0049] Traverse all first intersections, compare the position information of each first intersection with the first horizontal line, determine whether there is a horizontal straight line on the first intersection, and if so, use the horizontal straight line as the second horizontal line passing through the first intersection, and output a binary image of the horizontal line with the first intersection.
[0050] Traverse all first intersections, compare the position information of each first intersection with the first vertical line, determine whether there is a vertical straight line on the first intersection, and if so, use the vertical straight line as the second vertical line passing through the first intersection, and output a vertical line binary image with the first intersection.
[0051] Step S1032: Perform a bitwise OR operation on the second horizontal line and the second vertical line to obtain a plurality of binary contour images where the second horizontal line and the second vertical line intersect, and select at least one reference table contour from the plurality of binary contour images, wherein the at least one reference table contour includes the first intersection point. Figure 7 shown.
[0052] Specifically, a bitwise OR operation is performed on the horizontal line binary image with the second horizontal line and the vertical line binary image with the second vertical line to obtain a binary image where the horizontal and vertical lines intersect, and then an external contour search is performed to obtain all binary contour images, and then the binary contour images that do not contain the first intersection are excluded by checking whether each binary contour image contains the first intersection point, and at least one binary contour image with the first intersection point is used as a reference table contour. The reference table contour can be an outer frame of the table.
[0053] Step S1033: traverse the candidate table outlines, determine the intersection area ratio between the candidate table outline and at least one reference table outline, and select the candidate table outline whose intersection area ratio is greater than the seventh threshold as the target table outline.
[0054] Specifically, all the alternative table contours are traversed and compared with each reference table contour one by one. Specifically, the alternative table contour is intersected with each reference table contour one by one. If the intersection area ratio is greater than the seventh threshold, it means that the alternative table contour has a high degree of match with the reference table contour and can be selected as the target table contour.
[0055] Through the technical solution disclosed in the embodiment of the present application, the reference table outline is obtained by selecting horizontal and vertical lines through the first intersection, without making exclusion rules, so as to ensure its integrity to the greatest extent. In addition, it is not necessary to obtain all the intersections in the table, so the intersections in the table can be obtained by simple horizontal and vertical line extraction and as few exclusion rules as possible. By selecting the horizontal and vertical lines passing through the intersection through the intersection, the external contour can be used to find the table area with the maximum accuracy. In addition, if the alternative table outline is too large or too small, it can also be corrected by referring to the table outline, thereby further improving the accuracy and precision of table positioning.
[0056] In addition, by locating the position of the table area in the document image, the interference of horizontal and vertical lines outside the table has actually been eliminated, saving the time for excluding non-table lines, reducing the time spent on locating the table area, and further improving the detection accuracy and efficiency of the table area.
[0057] In one possible implementation, Figure 8 As shown, extracting data of at least one cell from a table area includes the following steps:
[0058] Step S1041: Acquire a table area, and perform adaptive binarization processing on the table area to obtain a binarized table image.
[0059] Step S1042: extract the third horizontal line of the binary table image in the horizontal direction, obtain the second intersection on the third horizontal line, traverse the second intersection, select the consistent third intersection in the vertical direction, and the vertical direction includes multiple third intersections.
[0060] Specifically, extracting the third horizontal line of the binary table image in the horizontal direction includes:
[0061] The erosion algorithm is used to obtain multiple horizontal line segments in the table area in the horizontal direction, and then the expansion algorithm and the closing algorithm are used to close the horizontal line segments to obtain the third horizontal line.
[0062] In the actual table, every two line segments are actually divided by a vertical line. Based on this, we can first assume that if the leftmost end of a line segment and the rightmost end of a line segment (or the starting end of a line segment and the ending end of a line segment) are close enough, then this position can be considered as an intersection of the horizontal and vertical lines. According to this condition, all endpoints that meet the condition are listed. These endpoints are the reference points for selecting horizontal and vertical lines later. In addition, there may be two line segments that are not what we need, but they also meet the above conditions, and we need to eliminate the redundant endpoints in the next step.
[0063] It should be noted that the left endpoint of the leftmost line segment in the table and the rightmost endpoint of the rightmost line segment have only one intersection point, so they will be ignored if the distance between the two endpoints is compared, so these endpoints must be added to the endpoint list at the end.
[0064] Specifically, the binary table image is input, and the lines in the horizontal direction are obtained through corrosion and expansion methods. There are two outputs in the horizontal direction, namely horizontal line segments and long straight lines. For horizontal line segments, Fig. 9 As shown, a binary line segment graph is output. Use the erosion algorithm to separate the horizontal line segments as much as possible, perform contour search, and output a list of all line segment positions. There will be a large number of line segments here. In order to increase the speed, a closing operation can be used to close the line segments whose vertical line segment width is less than a certain threshold.
[0065] Then use the expansion algorithm and the closing algorithm to close each horizontal line segment, so that all horizontal line segments are connected into a straight line (the third horizontal line), perform contour search, exclude short lines, and output all line binarization images and a list of the positions of all lines.
[0066] Obtaining the second intersection point on the third horizontal line includes: traversing each horizontal line segment, obtaining endpoints at both ends of each horizontal line segment, then obtaining the distance between adjacent endpoints, and selecting the endpoint whose distance is less than an eighth threshold as the second intersection point.
[0067] Specifically, input the binary graph of line segments, find all line segments, traverse all line segments, obtain the endpoints of all line segments, and output the list of endpoints. Traverse all endpoints and determine the distance between two endpoints. If the distance is less than the eighth threshold (very small), it means that this endpoint is the critical point of two horizontal line segments. If the distance is large, it means that this is an isolated line segment and can be screened out. Therefore, if the endpoint is less than the eighth threshold, it is considered to be an intersection of a horizontal line and a vertical line, and the two endpoints of the isolated line segment will be excluded. Fig.10 As shown, a list of all second intersection points is output.
[0068] Traverse the list of the second intersection points and only keep the endpoints that are consistent in the vertical direction. Because the horizontal values of the vertical intersection points are approximately equal, those isolated points in the vertical direction can be excluded, and the list of all the endpoints that are consistent in the vertical direction can be output. There is a special case. If there is only two rows of cells, there is only one endpoint in the vertical direction. After the above filtering, there may be no intersection points. In this case, the points that are consistent in the horizontal direction can be retained.
[0069] Step S1043: Extract the fourth vertical line in the vertical direction of the binary table image, perform a bitwise AND operation on the third horizontal line and the fourth vertical line to obtain the fourth intersection where the third horizontal line and the fourth vertical line intersect, traverse the fourth intersection, and select from the third horizontal lines a line that has multiple fourth intersections on the third horizontal line as the fourth horizontal line.
[0070] Specifically, extracting the fourth vertical line of the binary table image in the vertical direction includes: using a corrosion algorithm to obtain multiple vertical line segments in the vertical direction of the table area, and then using an expansion algorithm and a closing algorithm to close each vertical line segment to obtain a third vertical line; traversing the third vertical line, and selecting a line with multiple third intersections on the third vertical line from the third vertical line as the fourth vertical line, thereby excluding lines that are too short and improving the positioning accuracy of the cell.
[0071] Step S1044: Calculate the minimum and maximum values of the fourth horizontal line, the minimum and maximum values of the fourth vertical line, add a table frame, perform expansion operations and bitwise OR operations on the fourth horizontal line and the fourth vertical line to obtain a binary image of the intersection of horizontal and vertical lines, and then perform a closing operation on the binary image to obtain multiple cell outlines.
[0072] Specifically, calculate the minimum value (minx) and maximum value (maxx) of the fourth horizontal line, the minimum value (miny) and maximum value (maxy) of the fourth vertical line, and add a table frame (minx, miny, maxx, maxy). Dilate the extracted horizontal and vertical binary images respectively, and then perform a bitwise OR operation to output the following: Fig.11 The binary graph of the intersection of horizontal and vertical lines is shown, and then a closing operation is performed on the graph to ensure that each cell in the table is closed.
[0073] Step S1045: Select at least one target cell outline whose outline area is equal to the ninth threshold value from the multiple cell outlines, obtain the position information of the target cell outline, and obtain the data in the corresponding cell based on the position information.
[0074] Specifically, multiple cell outlines are found, and outlines that are smaller than or larger than the ninth threshold are filtered out. Each outline is a cell, and a list of all cell positions is output, and cell data is obtained at the corresponding cell position.
[0075] Through the technical solution disclosed in the embodiment of the present application, only the fine detection and extraction optimization of horizontal and vertical lines are performed on the table area, thereby improving the cell positioning accuracy, reducing the cell positioning time and the accuracy of data extraction. Use more refined rules to extract the horizontal and vertical lines in the table, extract the line segments of the horizontal and vertical lines, determine the intersection, and use the intersection to determine the horizontal and vertical lines again, avoid the loss of cells, and improve the accuracy and completeness of locating cells.
[0076] It is worth noting that the thresholds involved in the above embodiments can determine their values according to actual conditions, and the embodiments of the present application are not limited here. The horizontal lines, vertical lines, table outlines and tables shown in the drawings are only for reference, and the embodiments of the present application are not limited to the styles shown in the drawings.
[0077] It should be noted that the execution subject of the method for extracting tabular data provided in the embodiment of the present application may be a device for extracting tabular data, or a control module in the device for extracting tabular data for executing the method for extracting tabular data. In the embodiment of the present application, a device for extracting tabular data executing a method for extracting tabular data is taken as an example to illustrate the device for extracting tabular data provided in the embodiment of the present application.
[0078] Fig.12 FIG. 1 is a schematic diagram of a structure of a device for extracting table data according to an embodiment of the present invention. Fig.12As shown, a device 1200 for extracting table data includes: an acquisition module 1201, used to acquire a document image in response to a first input from a user; a first selection module 1202, used to select a first horizontal line and a first vertical line in the document image, perform a bitwise AND operation on the first horizontal line and the first vertical line to obtain a plurality of first intersections, and perform a bitwise OR operation on the first horizontal line and the first vertical line to obtain at least one alternative table outline; a second selection module 1203, used to select a target table outline from the alternative table outlines based on the first intersections, and obtain position information of the target table outline in the document image, wherein the target table outline has a plurality of first intersections; a determination module 1204, used to determine a table area of the document image according to the position information, and extract data of at least one cell from the table area.
[0079] In an embodiment of the present application, for different document images, a target table outline that satisfies the requirements of the document image can be selected from the alternative table outlines, so as to determine the table area and extract cell data from the table area. The table data can be extracted more easily and the accuracy of the extracted table data is improved.
[0080] In one implementation, the first selection module 1202 is also used to perform adaptive binarization processing on the document image and output a binarized document image; select all lines in the horizontal direction of the binarized document image through an erosion and expansion algorithm, merge lines with upper and lower spacing less than a first threshold in the horizontal direction, and then select lines with a line length exceeding a second threshold and lines with a line width less than a third threshold as first horizontal lines; select all lines in the vertical direction of the binarized document image through an erosion and expansion algorithm, merge lines with a left and right spacing less than a fourth threshold in the vertical direction, and then select lines with a line length exceeding a fifth threshold and lines with a line width less than a sixth threshold as first vertical lines.
[0081] In one implementation, the second selection module 1203 is also used to select a second horizontal line passing through the first intersection from the first horizontal lines, and to select a second vertical line passing through the first intersection from the first vertical lines; perform a bitwise OR operation on the second horizontal line and the second vertical line to obtain multiple binary contour images where the second horizontal line and the second vertical line intersect, select at least one reference table contour from the multiple binary contour images, and at least one reference table contour contains the first intersection; traverse the alternative table contours, determine the intersection area ratio of the alternative table contour and at least one reference table contour, and select the alternative table contour whose intersection area ratio is greater than the seventh threshold as the target table contour.
[0082] In one implementation, the determination module 1204 is further used to obtain a table area, and perform adaptive binarization processing on the table area to obtain a binary table image; extract a third horizontal line of the binary table image in the horizontal direction, obtain a second intersection on the third horizontal line, traverse the second intersection, select a third intersection that is consistent in the vertical direction, and the vertical direction includes multiple third intersections; extract a fourth vertical line of the binary table image in the vertical direction, perform a bitwise AND operation on the third horizontal line and the fourth vertical line to obtain a fourth intersection where the third horizontal line and the fourth vertical line intersect, traverse the fourth intersection, and select a fourth intersection from the first and second intersections. A line having multiple fourth intersection points on the third horizontal line is selected from the three horizontal lines as the fourth horizontal line; the minimum and maximum values of the fourth horizontal line and the minimum and maximum values of the fourth vertical line are calculated, an outer frame of a table is added, and then an expansion operation and a bitwise OR operation are performed on the fourth horizontal line and the fourth vertical line to obtain a binary image of the intersection of horizontal and vertical lines, and then a closing operation is performed on the binary image to obtain multiple cell outlines; at least one target cell outline whose outline area is equal to a ninth threshold is selected from the multiple cell outlines, position information of the target cell outline is obtained, and data in the corresponding cell is obtained based on the position information.
[0083] In one implementation, the determination module 1204 is also used to use a corrosion algorithm to obtain multiple horizontal line segments in the table area in the horizontal direction, and then use an expansion algorithm and a closing algorithm to close each horizontal line segment to obtain a third horizontal line; traverse each horizontal line segment, obtain the endpoints at both ends of each horizontal line segment, and then obtain the distance between adjacent endpoints, and select the endpoint whose distance is less than the eighth threshold as the second intersection point.
[0084] In one implementation, the determination module 1204 is also used to use a corrosion algorithm to obtain multiple vertical line segments in the vertical direction of the table area, and then use an expansion algorithm and a closing algorithm to close each vertical line segment to obtain a third vertical line; traverse the third vertical line, and select a line with multiple third intersections on the third vertical line from the third vertical line as the fourth vertical line.
[0085] In the embodiment of the present application, a device for extracting table data can be a device, or a component, integrated circuit, or chip in a terminal. The device can be a mobile electronic device or a non-mobile electronic device. Exemplarily, the mobile electronic device can be a mobile phone, a tablet computer, a laptop computer, a PDA, an in-vehicle electronic device, a wearable device, an ultra-mobile personal computer (UMPC), a netbook, or a personal digital assistant (PDA), etc. The non-mobile electronic device can be a server, a network attached storage (NAS), a personal computer (PC), a television (TV), a teller machine or a self-service machine, etc., which is not specifically limited in the embodiment of the present application.
[0086] A device for extracting table data in an embodiment of the present application may be a device having an operating system. The operating system may be an Android operating system, an iOS operating system, or other possible operating systems, which are not specifically limited in the embodiment of the present application.
[0087] A device for extracting table data provided in an embodiment of the present application can implement each process implemented in the above method embodiment, and will not be described again here to avoid repetition.
[0088] Optional, such as Fig.13 As shown, an embodiment of the present application also provides an electronic device 1300, including a processor 1301, a memory 1302, and a program or instruction stored in the memory 1302 and executable on the processor 1301. When the program or instruction is executed by the processor 1301, each process of the above-mentioned method for extracting table data is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.
[0089] It should be noted that the electronic devices in the embodiments of the present application include the above-mentioned mobile electronic devices and non-mobile electronic devices.
[0090] An embodiment of the present application also provides a readable storage medium, on which a program or instruction is stored. When the program or instruction is executed by a processor, each process of the above-mentioned table data extraction method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.
[0091] The processor is the processor in the electronic device in the above embodiment. The readable storage medium includes a computer readable storage medium, such as a computer read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0092] An embodiment of the present application further provides a chip, which includes a processor and a communication interface, wherein the communication interface is coupled to the processor, and the processor is used to run programs or instructions to implement the various processes of the above-mentioned table data extraction method embodiment, and can achieve the same technical effect. To avoid repetition, it will not be repeated here.
[0093] It should be understood that the chip mentioned in the embodiments of the present application can also be called a system-level chip, a system chip, a chip system or a system-on-chip chip, etc.
[0094] It should be noted that, in this article, the terms "comprise", "include" or any other variant thereof are intended to cover non-exclusive inclusion, so that the process, method, article or device including a series of elements includes not only those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise one..." do not exclude the presence of other identical elements in the process, method, article or device including the element. In addition, it should be pointed out that the scope of the methods and devices in the embodiments of the present application is not limited to performing functions in the order shown or discussed, and may also include performing functions in a substantially simultaneous manner or in reverse order according to the functions involved, for example, the described method may be performed in an order different from that described, and various steps may also be added, omitted, or combined. In addition, the features described with reference to certain examples may be combined in other examples.
[0095] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0096] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of the present application, ordinary technicians in this field can also make many forms without departing from the purpose of the present application and the scope of protection of the claims, all of which are within the protection of the present application.
Claims
1. A method for extracting table data, characterized in that: include: In response to a first input from a user, acquiring a document image; Selecting a first horizontal line and a first vertical line in the document image, performing a bitwise AND operation on the first horizontal line and the first vertical line to obtain a plurality of first intersection points, and performing a bitwise OR operation on the first horizontal line and the first vertical line to obtain at least one candidate table outline; Selecting a target table outline from the candidate table outlines based on the first intersection point, and acquiring position information of the target table outline in the document image, wherein there are a plurality of the first intersection points on the target table outline; Determine a table area of the document image according to the position information, and extract data of at least one cell from the table area; The step of extracting data of at least one cell from the table area includes: Acquire the table area, and perform adaptive binarization processing on the table area to obtain a binarized table image; Extracting a third horizontal line of the binary table image in the horizontal direction, obtaining a second intersection on the third horizontal line, traversing the second intersection, and selecting a third intersection that is consistent in the vertical direction, wherein the vertical direction includes a plurality of third intersections; Extracting a fourth vertical line in the vertical direction of the binary table image, performing a bitwise AND operation on the third horizontal line and the fourth vertical line to obtain a fourth intersection point where the third horizontal line and the fourth vertical line intersect, traversing the fourth intersection points, and selecting a line having a plurality of fourth intersection points on the third horizontal line from the third horizontal line as the fourth horizontal line; Calculate the minimum and maximum values of the fourth horizontal line, the minimum and maximum values of the fourth vertical line, add a table frame, perform an expansion operation and a bitwise OR operation on the fourth horizontal line and the fourth vertical line to obtain a binary image of the intersection of horizontal and vertical lines, and then perform a closing operation on the binary image to obtain multiple cell outlines; At least one target cell contour whose contour area is equal to a ninth threshold value is selected from the multiple cell contours, position information of the target cell contour is obtained, and data in a corresponding cell is obtained based on the position information.
2. The method according to claim 1, characterized in that: The selecting the first horizontal line and the first vertical line in the document image comprises: Performing adaptive binarization processing on the document image and outputting a binarized document image; Select all the lines in the horizontal direction of the binary document image by using the corrosion and expansion algorithm, merge the lines whose upper and lower spacing is less than the first threshold in the horizontal direction, and then select the lines whose line length exceeds the second threshold and the lines whose line width is less than the third threshold as the first horizontal lines; All lines in the vertical direction of the binary document image are selected through the corrosion and expansion algorithm, and in the vertical direction, the lines whose left and right spacing is less than the fourth threshold are merged, and then the lines whose line length exceeds the fifth threshold and the lines whose line width is less than the sixth threshold are selected as the first vertical lines.
3. The method according to claim 1, characterized in that The selecting a target table outline from the candidate table outlines based on the first intersection point comprises: Selecting a second horizontal line passing through the first intersection from the first horizontal lines, and selecting a second vertical line passing through the first intersection from the first vertical lines; Performing a bitwise OR operation on the second horizontal line and the second vertical line to obtain a plurality of binary contour images where the second horizontal line and the second vertical line intersect, and selecting at least one reference table contour from the plurality of binary contour images, wherein the at least one reference table contour includes the first intersection point; The candidate table outlines are traversed to determine the intersection area ratio between the candidate table outline and at least one of the reference table outlines, and the candidate table outline whose intersection area ratio is greater than a seventh threshold is selected as the target table outline.
4. The method according to claim 1, characterized in that: The step of extracting a third horizontal line of the binary table image in the horizontal direction comprises: Using an erosion algorithm to obtain multiple horizontal line segments in the table area in the horizontal direction, and then using an expansion algorithm and a closing algorithm to close each of the horizontal line segments to obtain a third horizontal line; The obtaining of the second intersection point on the third horizontal line includes: traversing each of the horizontal line segments, obtaining endpoints at both ends of each of the horizontal line segments, and then obtaining the distance between adjacent endpoints, and selecting the endpoint whose distance is less than an eighth threshold as the second intersection point.
5. The method according to claim 4, characterized in that The extracting of the fourth vertical line of the binary table image in the vertical direction comprises: using an erosion algorithm to obtain a plurality of vertical line segments of the table area in the vertical direction, and then using an expansion algorithm and a closing algorithm to close each of the vertical line segments to obtain a third vertical line; The third vertical lines are traversed, and a line having a plurality of third intersection points on the third vertical lines is selected from the third vertical lines as a fourth vertical line.
6. A device for extracting table data, characterized in that: include: An acquisition module, configured to acquire a document image in response to a first input from a user; A first selection module is used to select a first horizontal line and a first vertical line in the document image, perform a bitwise AND operation on the first horizontal line and the first vertical line to obtain a plurality of first intersection points, and perform a bitwise OR operation on the first horizontal line and the first vertical line to obtain at least one candidate table outline; A second selection module is used to select a target table outline from the candidate table outlines based on the first intersection points, and obtain position information of the target table outline in the document image, where there are multiple first intersection points on the target table outline; A determination module, configured to determine a table area of the document image according to the position information, and extract data of at least one cell from the table area; The determination module is further used to obtain a table area, and perform adaptive binarization processing on the table area to obtain a binarized table image; extract a third horizontal line of the binarized table image in the horizontal direction, obtain a second intersection on the third horizontal line, traverse the second intersection, select a third intersection that is consistent in the vertical direction, and the vertical direction contains multiple third intersections; extract a fourth vertical line of the binarized table image in the vertical direction, perform a bitwise AND operation on the third horizontal line and the fourth vertical line to obtain a fourth intersection where the third horizontal line and the fourth vertical line intersect, traverse the fourth intersection, and select a line with multiple fourth intersections on the third horizontal line from the third horizontal lines as the fourth horizontal line; calculate the minimum and maximum values of the fourth horizontal line and the minimum and maximum values of the fourth vertical line, add a table frame, and then perform an expansion operation and a bitwise OR operation on the fourth horizontal line and the fourth vertical line to obtain a binarized image where horizontal and vertical lines intersect, and then perform a closing operation on the binarized image to obtain multiple cell outlines; select at least one target cell outline whose outline area is equal to the ninth threshold from the multiple cell outlines, obtain position information of the target cell outline, and obtain data in the corresponding cell based on the position information.
7. The device according to claim 6, characterized in that The first selection module is further used for: Performing adaptive binarization processing on the document image and outputting a binarized document image; Select all the lines in the horizontal direction of the binary document image by using the corrosion and expansion algorithm, merge the lines whose upper and lower spacing is less than the first threshold in the horizontal direction, and then select the lines whose line length exceeds the second threshold and the lines whose line width is less than the third threshold as the first horizontal lines; All lines in the vertical direction of the binary document image are selected through the corrosion and expansion algorithm, and in the vertical direction, the lines whose left and right spacing is less than the fourth threshold are merged, and then the lines whose line length exceeds the fifth threshold and the lines whose line width is less than the sixth threshold are selected as the first vertical lines.
8. An electronic device, characterized in that: The method comprises a processor, a memory and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the steps of the method for extracting table data as described in any one of claims 1 to 5.
9. A readable storage medium, characterized in that: The readable storage medium stores a program or instruction, and when the program or instruction is executed by a processor, the steps of the method for extracting table data according to any one of claims 1 to 5 are implemented.
Citation Information
Patent Citations
Image table exporting method and system, storage medium and equipment
CN112183511A