Table image tilt judgment and correction method and device based on k-means clustering
Through the method based on k-means clustering, the image is detected and linear filtered, and the clustering angle is used to achieve accurate rotation correction of table pictures, solving the problems of error and noise interference in the prior art, and improving the accuracy and efficiency of data extraction and analysis.
Patent Information
- Application Number
- CN202410643229.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-23
- Publication Date
- 2025-03-21
- Estimated Expiration
- 2044-05-23
AI Technical Summary
In the prior art, when processing skewed document pictures containing tables, there are errors and noise interferences, resulting in inaccurate correction results, especially in cases where complex table structures and angles are large.
Using a method based on k-means clustering, the images are randomly cropped and text detected, and the average height is calculated to filter the straight line. Through Hough line detection and angular clustering, the clustering results are used to correct the original image in reverse rotation.
Accurate rotation correction of table pictures is achieved, which improves the accuracy and efficiency of data extraction and analysis, avoids noise interference and improves clustering efficiency.
Smart Images

Figure CN118397242B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of image processing, and in particular to a method and device for judging and correcting the tilt of a table image based on k-means clustering. Background Art
[0002] In the task of processing image data containing tables, some challenges are often encountered, including but not limited to document image tilt, deformation, noise interference, etc. These factors will affect the extraction and analysis of data in the table. At present, there are some technologies for processing tilted document images containing tables, such as: 1) Correction based on table feature points, by detecting feature points in the image, such as corner points or line segment intersections, to determine the geometric transformation relationship of the image, and then perform correction. However, for cases with complex table structures and large noise interference, feature point detection may have errors, resulting in inaccurate correction results; 2) Tilt judgment and correction based on Hough line detection, the tilt angle of the document image, and then correct the tilt through rotation operations. However, this method often does not work well when processing complex table structures or large angles, and is prone to problems such as deformation of the table structure and information loss. How to effectively rotate and correct the image containing the table area while avoiding noise interference is one of the current research focuses. Summary of the invention
[0003] The technical problem to be solved by the present invention is how to provide a method and device for judging and correcting the tilt of table images based on k-means clustering, which can achieve accurate rotation correction of images, thereby improving the accuracy and efficiency of data extraction and analysis.
[0004] In order to solve the above technical problems, the technical solution adopted by the present invention is: a table image tilt judgment and correction method based on k-means clustering, comprising the following steps:
[0005] Randomly crop the input image, perform text detection in the cropped small image, count the heights of the N detected text areas, and calculate the average height H;
[0006] Use the average height H as the threshold to perform Hough line detection on the entire image and filter out lines with lengths less than H;
[0007] Calculate the slope of the remaining line and convert it into an angle value;
[0008] All angles are clustered into two categories using the k-means clustering method, and the difference between cluster centers is always kept at 90° during the clustering process;
[0009] Finally, the tilt angle is obtained according to the clustering results, and the original image is reversely rotated and corrected.
[0010] Correspondingly, the present invention also discloses a table image tilt judgment and correction device based on k-means clustering, comprising the following steps:
[0011] Image segmentation and text area height calculation module: used to randomly crop the input image, perform text detection in the cropped small image, count the heights of the N detected text areas, and calculate the average height H;
[0012] Line filtering module: used to perform Hough line detection with the average height H as the threshold, and filter out lines with a length less than H;
[0013] Angle value calculation module: used to calculate the slope of the remaining straight line and convert it into an angle value;
[0014] Angle clustering module: used to cluster all angles into two categories using the k-means clustering method, and always keep the difference between cluster centers at 90° during the clustering process;
[0015] Image correction module: used to obtain the tilt angle according to the clustering results and perform reverse rotation correction on the original image.
[0016] The beneficial effects of the above technical solution are as follows: the method first detects text and performs linear filtering based on the text size, thus ensuring universality while avoiding the possibility of noise interference. The method restricts the k-Means clustering process based on table features, avoiding the shortcomings of sensitivity to the selection of initial cluster centers and easy to fall into local optimal solutions in the initialization stage, while improving clustering efficiency. Accurate rotation correction of table images is achieved, improving the accuracy and efficiency of data extraction and analysis. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] The present invention will be further described in detail below in conjunction with the accompanying drawings and specific embodiments.
[0018] Figure 1 is a flow chart of the method described in an embodiment of the present invention;
[0019] Figure 2 is the original image of the test sample in the embodiment of the present invention;
[0020] Figure 3 is a graph of text detection results in an embodiment of the present invention;
[0021] Figure 4 is a Hough line detection result diagram of the original image in an embodiment of the present invention;
[0022] Figure 5 is a straight line angle definition rule diagram in an embodiment of the present invention;
[0023] Figure 6is a diagram of the rotation correction result in an embodiment of the present invention;
[0024] Figure 7 It is a principle block diagram of the device described in the embodiment of the present invention. DETAILED DESCRIPTION
[0025] The following is a clear and complete description of the technical solutions in the embodiments of the present invention in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0026] In the following description, many specific details are set forth to facilitate a full understanding of the present invention, but the present invention may also be implemented in other ways different from those described herein, and those skilled in the art may make similar generalizations without violating the connotation of the present invention. Therefore, the present invention is not limited to the specific embodiments disclosed below.
[0027] like Figure 1 As shown, the embodiment of the present invention discloses a method for judging and correcting the tilt of a table image based on k-means clustering, comprising the following steps:
[0028] Step S1, randomly crop the input image, and perform text detection in the cropped small image. Count the heights of the N detected text areas and calculate the average height H:
[0029]
[0030] Among them, h i is the height of the i-th text area.
[0031] Step S2, perform Hough line detection on the input image. Set the shortest line length threshold to H, and lines shorter than this length will be filtered out, which can effectively reduce the interference of text on the detection results.
[0032] Hough Line Transform is an algorithm for detecting straight lines in an image. The basic principle is to transform each point in the image space into the parameter space and detect the straight line by finding the concentrated point in the parameter space. The following are the main steps and principles of Hough Line Transform:
[0033] S2-1) Image preprocessing
[0034] First, the input image is preprocessed, usually including grayscale conversion and edge detection (such as using the Canny edge detector). The purpose of this step is to extract the edges in the image and reduce the amount of data that needs to be processed.
[0035] S2-2) Parameter space representation
[0036] The Hough transform maps points in the image space to the parameter space. For line detection, polar coordinate representation is used, that is, each line can be represented by two parameters: polar diameter ρ and polar angle θ. The line equation can be expressed as:
[0037] ρ=xcosθ+ysinθ
[0038] Where x and y are the coordinates of the point in the image.
[0039] S2-3) Accumulator array
[0040] Create a two-dimensional array of accumulators, one for each value of ρ and θ. Each cell of the accumulator array is initialized to zero.
[0041] S2-4) Parameter Space Voting
[0042] For each edge point in the image, calculate all possible corresponding ρ and θ values; each time a ρ and θ pair is calculated, add 1 to the corresponding position in the accumulator array. This process is similar to voting in parameter space.
[0043] S2-5) Detect straight line
[0044] After the voting process, the cells with higher values in the accumulator array indicate that more points voted for these ρ and θ values in the image space, which means that the straight lines corresponding to these parameters exist in the image. By setting a threshold, the cells in the accumulator array that are greater than the threshold can be screened out to determine the straight lines in the image.
[0045] S2-6) The result is mapped back to the image space
[0046] The detected ρ and θ values are converted back to image space and the detected lines are plotted.
[0047] Step S3: The detected straight line format is the starting point coordinate (x0, y0) and the end point coordinate (x1, y1). The slope of the straight line is calculated and converted into an angle with the positive direction of the X axis.
[0048] Step S4, clustering the angles. K-means is a commonly used unsupervised learning algorithm used to divide a data set into K clusters c (k) , the similarity of data points in each cluster is high, while the similarity of data points between different clusters is low. The basic idea is to iteratively find the optimal K cluster centroids so that the sum of the distances between the data points in the cluster and their corresponding centroids is minimized. This method restricts the clustering process based on the characteristics of the table, which can make the clustering results more accurate.
[0049] The input of this method is a set of m unlabeled angle data:
[0050] θ (1) ,θ (2) ,θ (3) ,......,θ (m)
[0051] S4-1: First, K initial centroids need to be randomly selected as the center point of each cluster. Since the object of the present invention is a table image, a standard table area is composed of two types of line segments, horizontal and vertical. Even after rotation, the classification of the line segments remains unchanged. Therefore, the present invention sets the number of initial centroids to 2, that is, c (2) At the same time, the horizontal and vertical line segments will remain vertical after rotation, so the present invention sets the initial cluster centers to 45 and 135, avoiding the shortcomings of the K-means method in the initialization stage, such as sensitivity to the selection of the initial cluster centers and easy to fall into the local optimal solution, and improving the clustering efficiency. The initial cluster centers are:
[0052] μ1=45,μ2=135
[0053] S4-2: For each θ (i) , calculate the value of μ with each cluster center j The distance between (i) Belongs to the cluster center μ that is closest to it j Cluster c (j) :
[0054] c (j) :=argmin j ||θ (i) -μ j || 2
[0055] S4-3: For each cluster c (j) , recalculate the value of the cluster center of the cluster:
[0056]
[0057] S4-4: After obtaining the updated μ1 and μ2, correct them and scale the difference to 90 to obtain μ1' and μ2', that is, |μ1'-μ2'|=90
[0058]
[0059] Repeat steps S4-2-S4-4 until the algorithm converges and output the final clustering result.
[0060] Step S5, performing reverse rotation correction on the original image based on the clustering result.
[0061] In this example, photos of a paper terminal block blueprint of a design institute were taken to test the proposed table image tilt judgment and correction method based on k-means clustering.
[0062] Input original image ( Figure 2 ), randomly intercept the text area and perform text detection ( Figure 3 ), calculate the minimum straight line length threshold based on the text detection result and then perform Hough line detection ( Figure 4 ), calculate the slope of the line and convert it into an angle ( Figure 5 ), the angles are clustered by the improved k-Means clustering method, and the clustering results in Table 1 are obtained. The original image is reversely rotated based on the angle results ( Figure 6 ), which achieves accurate rotation correction of images containing tables.
[0063] Table 1 Clustering results
[0064]
[0065] Correspondingly, such as Figure 7 As shown, the embodiment of the present invention also provides a table image tilt judgment and correction device based on k-means clustering, comprising the following steps:
[0066] Image segmentation and text area height calculation module: used to randomly crop the input image, perform text detection in the cropped small image, count the heights of the N detected text areas, and calculate the average height H;
[0067] Line filtering module: used to perform Hough line detection with the average height H as the threshold, and filter out lines with a length less than H;
[0068] Angle value calculation module: used to calculate the slope of the remaining straight line and convert it into an angle value;
[0069] Angle clustering module: used to cluster all angles into two categories using the k-means clustering method, and always keep the difference between cluster centers at 90° during the clustering process;
[0070] Image correction module: used to obtain the tilt angle according to the clustering results and perform reverse rotation correction on the original image.
[0071] This method first detects text and filters lines based on text size, ensuring universality while avoiding noise interference. This method restricts the k-Means clustering process based on table features, avoiding the shortcomings of sensitivity to the selection of initial cluster centers and easy to fall into local optimal solutions in the initialization phase, while improving clustering efficiency. It achieves accurate rotation correction of table images, improving the accuracy and efficiency of data extraction and analysis.
Claims
1. A method for judging and correcting the tilt of table images based on k-means clustering, characterized in that The steps include: Randomly crop the input image, perform text detection in the cropped small image, count the heights of the N detected text areas, and calculate the average height H; Use the average height H as the threshold to perform Hough line detection on the entire image and filter out lines with lengths less than H; After filtering, the slope of the remaining straight line is calculated and converted into an angle value between 0° and 180°; All angles are clustered into two categories using the k-means clustering method, and the difference between cluster centers is always kept at 90° during the clustering process; Finally, the tilt angle is obtained according to the clustering results, and the original image is reversely rotated and corrected; The Hough line detection comprises the following steps: 1) Image preprocessing: First, the input image is preprocessed, including grayscale conversion and edge detection, to extract the edges in the image and reduce the amount of data that needs to be processed; 2) Parameter space representation: Map the points in the image space to the parameter space. For line detection, polar coordinate representation is used, that is, each line is represented by two parameters: polar diameter ρ and polar angle θ. The line equation is expressed as: ρ=xcosθ+ysinθ Where x and y are the horizontal and vertical coordinates of the point in the image; 3) Accumulator array: Create a two-dimensional accumulator array corresponding to different values of ρ and θ; each cell of the accumulator array is initialized to zero; 4) Parameter space voting: For each edge point in the image, calculate all possible corresponding ρ and θ values. For each ρ and θ pair calculated, add 1 to the corresponding position in the accumulator array. 5) Detect straight line: After the voting process, the cells with higher values in the accumulator array indicate that more points voted for these ρ and θ values in the image space, which means that the straight lines corresponding to these parameters exist in the image. By setting a threshold, cells in the accumulator array with values greater than the threshold are screened out, thereby determining the straight lines in the image. 6) The result is mapped back to image space: Convert the detected ρ and θ values back to image space and draw the detected straight lines; The method for clustering angles includes the following steps: The input is a set of m unlabeled angle data: i (1) ,i (2) ,i (3) ,......,i (m) 1) First, randomly select K initial centroids as the center points of each cluster, and set the number of initial centroids to 2, that is, C (2) , the initial cluster center is: μ1=45,μ2=135 2) For each θ (i) , calculate the value of μ with each cluster center j The distance between (i) Belongs to the cluster center μ that is closest to it j Cluster c (j) : c (j) =argmin j ||θ (i) -m j || 2 3) For each cluster c (j) , recalculate the value of the cluster center of the cluster: 4) After obtaining the updated μ1 and μ2, correct them and scale the difference to 90 to obtain μ1' and μ2', that is, |μ1'-μ2'|=90 Repeat steps 2)-4) until the algorithm converges and output the final clustering result.
2. The method for determining and correcting the tilt of a table image based on k-means clustering according to claim 1, characterized in that: The calculation formula for the average height H is: Among them, h i is the height of the i-th text area.
3. The method for determining and correcting the tilt of a table image based on k-means clustering according to claim 1, characterized in that: The angle value calculation method comprises the following steps: The starting point coordinates of the detected straight line are (x0, y0), and the ending point coordinates are (x1, y1). The slope of the straight line is calculated based on the starting point coordinates and the ending point coordinates, and the slope is converted into an angle with the positive direction of the X-axis to obtain the angle value.
4. A table image tilt judgment and correction device based on k-means clustering, characterized in that The steps include: Image segmentation and text area height calculation module: used to randomly crop the input image, perform text detection in the cropped small image, count the heights of the N detected text areas, and calculate the average height H; Line filtering module: used to perform Hough line detection with the average height H as the threshold, and filter out lines with a length less than H; Angle value calculation module: used to calculate the slope of the remaining straight line and convert it into an angle value; Angle clustering module: used to cluster all angles into two categories using the k-means clustering method, and always keep the difference between cluster centers at 90° during the clustering process; Image correction module: used to obtain the tilt angle according to the clustering results and perform reverse rotation correction on the original image; The straight line filtering module performs Hough straight line detection in the following way: 1) Image preprocessing: First, the input image is preprocessed, including grayscale conversion and edge detection, to extract the edges in the image and reduce the amount of data that needs to be processed; 2) Parameter space representation: Map the points in the image space to the parameter space. For line detection, polar coordinate representation is used, that is, each line is represented by two parameters: polar diameter ρ and polar angle θ. The line equation is expressed as: ρ=xcosθ+ysinθ Where x and y are the horizontal and vertical coordinates of the point in the image; 3) Accumulator array: Create a two-dimensional accumulator array corresponding to different values of ρ and θ; each cell of the accumulator array is initialized to zero; 4) Parameter space voting: For each edge point in the image, calculate all possible corresponding ρ and θ values. For each ρ and θ pair calculated, add 1 to the corresponding position in the accumulator array. 5) Detect straight line: After the voting process, the cells with higher values in the accumulator array indicate that more points voted for these ρ and θ values in the image space, which means that the straight lines corresponding to these parameters exist in the image. By setting a threshold, cells in the accumulator array with values greater than the threshold are screened out, thereby determining the straight lines in the image. 6) The result is mapped back to image space: Convert the detected ρ and θ values back to image space and draw the detected straight lines; The method for clustering angles by the angle clustering module includes the following steps: The input is a set of m unlabeled angle data: i (1) ,i (2) ,i (3) ,......,i (m) 1) First, randomly select K initial centroids as the center points of each cluster, and set the number of initial centroids to 2, that is, c (2) , the initial cluster center is: μ1=45,μ2=135 2) For each θ (i) , calculate the value of μ with each cluster center j The distance between (i) Belongs to the cluster center μ that is closest to it j Cluster c (j) : c (j) =argmin j ||θ (i) -m j || 2 3) For each cluster c (j) , recalculate the value of the cluster center of the cluster: 4) After obtaining the updated μ1 and μ2, correct them and scale the difference to 90 to obtain μ1' and μ2', that is, |μ1'-μ2'|=90 Repeat steps 2)-4) until the algorithm converges and output the final clustering result.
5. The table image tilt judgment and correction device based on k-means clustering as claimed in claim 4, characterized in that: The calculation formula for the average height H calculated by the image segmentation and text area height calculation module is: Among them, h i is the height of the i-th text area.
6. The table image tilt judgment and correction device based on k-means clustering as claimed in claim 4, characterized in that: The method for calculating the angle value by the angle value calculation module comprises the following steps: The starting point coordinates of the detected straight line are (x0, y0), and the ending point coordinates are (x1, y1). The slope of the straight line is calculated based on the starting point coordinates and the ending point coordinates, and the slope is converted into an angle with the positive direction of the X-axis to obtain the angle value.
Citation Information
Patent Citations
Text image tilt correction method, system and device and storage medium
CN111553344A