# A Proposal of Printed Table Digitization Algorithm with Image Processing
## 5 Proposed Algorithm
### Procedure in Algorithm  
Next, we describe the procedure of the algorithm. 1. Make the color image \(I\) of each table by scanning the paper. 2. Transform the color image \(I\) into the black-and-white binary image \(I_{bin}\) by applying the _thresholds_ function in _OpenCV_ to \(I\). 3. Reference [9], obtain the vertical kernel \(k_{ver}\) and the horizontal kernel \(k_{hor}\) by applying the _getStructuringElement_ function in _OpenCV_ to \(I_{bin}\). 4. Obtain the binary image \(I_{ver}\) that only has vertical lines by applying the _erode_ function in _OpenCV_ to \(I_{bin}\). Figure 5 shows the binary image with only vertical lines for the printed table in Figure 3.
5. Extract the vertical lines \(L_{ver}\) by applying the _dilate_ function in _OpenCV_ to \(I_{ver}\). 6. Reference [9], obtain the binary image \(I_{hor}\) that only has horizontal lines by applying the _erode_ function in _OpenCV_ to \(I_{bin}\). Figure 6 shows the binary image with only horizontal lines for the printed table in Figure 3.
7. Extract the horizontal lines \(L_{hor}\) by applying the _dilate_ function in _OpenCV_ to \(I_{hor}\). 8. Make the binary image \(I_{bin}\) that represents the table frame by combining the vertical lines \(L_{ver}\) and the horizontal lines \(L_{hor}\) into one image, applying the _addWeighted_ function in _OpenCV_ to \(L_{ver}\) and \(L_{hor}\). Figure 7 shows the table frame image. 9. Obtain the contour information of each cell in the table and store them in the array \(L_{cuts}\) by applying the _findContours_ function in _OpenCV_ to \(I_{bin}\). 10. Find the rectangular \(r_{i}\) for each contour \(l_{i}\) of \(L_{cuts}\) by applying the _boundingRect_ function in _OpenCV_ to every contour of \(L_{cuts}\), and make the array \(R\) of \(r_{i}\). 11. Sort \(r_{i}\) in \(R\) from the leftmost one to the rightmost one according to the coordinates of \(r_{i}\) by applying the _zip_ method and the _sort_ method in _Python_ to \(R\), and make the array \(L_{cuts1}\) and the array \(R_{1}\). 12. Remove all excessively large or small rectangles in \(R_{1}\), to avoid rectangles that contain multiple cells or have no cells. Produce the \(R_{2}\) with every rectangle containing a cell. 13. Create _Cell_. Create class \(c_{i}\) for each rectangle in \(R_{2}\), collect \(x_{i}\), \(y_{i}\), and \(w_{i}\) of each rectangle in \(R_{2}\), and put them into \(c_{i}\) as member variables. 14. Obtain the character recognition result _name\({}_{i}\)_ by applying the _imageostring_ function in _pytesseract_ to each cell. 15. Sort the \(c_{i}\), in which the \(x_{i}\) and \(y_{i}\) of each \(c_{i}\) are arranged from left to right and from top to bottom. Arrange \(c_{i}\) with similar \(x_{i}\) into a column in the order of \(y_{i}\) from small to large to output _row\({}_{ci}\)_
16. Create the multi-tree class _Tree_ to describe the structure of the table with the root node _Root_. The child nodes of this root node are all \(c_{i}\) with a row equal to 1. 17. Confirm the child node of _row\(2_{ci}\)_; if _row\(1_{ci}\)_'s \(h_{i}\) is approximately equal to _row\(2_{ci}\)_'s \(h_{i}\), the child node of _row\(1_{ci}\) is _row\(2_{ci}\)_. Otherwise, let _row\(2_{ci}\)_'s \(h_{i}\) plus _row\(2_{ci+1}\)_'s \(h_{i+1}\), and make the sum of _row\(2_{ci}\)_'s \(h_{i}\) and _row\(2_{ci+1}\)_'s \(h_{i+1}\)_ compare with _row\(1_{ci}\)_'s \(h_{i}\). If the two are approximately equal, the child node of _row\(1_{ci}\)_ is _row\(2_{ci}\)_ and _row\(2_{ci+1}\)_. If the two are not approximately equal, it can be done again in the above way. 18. Follow the previous step to get the child node of _row\(2_{ci}\)_, the child node of _row\(2_{ci}\)_, up to the last column, and obtain the tree _Tree_ composed of \(c_{i}\), which represents the structure of the table cell structure. Figure 8 shows the tree. 19. Preorder the _Tree_ and put each \(c_{i}\)'s \(name_{i}\) in the leaf node's \(name_{i}\) in the order from the root node to the leaf node.