You were so kind to share your insights with me that I thought you may be interested in a slightly different but usable approach
to the problem you addressed in recognizing the table on the ballot, and then putting the cells of the table into a matrix that can be
analyzed to determine its contents.
This article is from a computer science group in Brasil. They approached the problem by identifying that a table has 9 "structured elements". These elements correspond to the four corners of the table, the four sides of the table and the intersection of the lines of the cells that do not lie along the borders of the table. By recognizing these structured elements, one can identify and segment the cells of a table, then read the cells' contents.
I have still not been able to get your solution to work, but it has been helpful. I am still trying to understand what your code does in some points. Thanks again.