Skip to main content
2 of 5
edited title
István Zachar
  • 47.2k
  • 20
  • 145
  • 293

Detecting grid lines in a raster image

I need to process a large number of images for a non-profit organization report. The images contain a grid with borders and cells. The cells B2 and C2 (spreadsheet coordinates) can be hand written or can be empty, and that is what needs to be detected.

Here is an example of a filled form:

filled form

And this is an example of an empty form:

enter image description here

My plan is to detect the coordinates of the following points:

enter image description here

and then compute to total amount o black pixels in the area defined by them.

So my question is: What strategy would you recommend to reliably detect the location of those points indicated in red?

I have already tried using ImageLines, Radon, and FindGeometricTransform without much success. I think that the best approach is not to look for independent lines but instead look for the grid as a whole.

This is what I am trying to do:

figWithoutSideBorders = 
  ColorNegate @
    ImageAdd[fig, 
             ColorNegate @ Erosion[#, 3] & @ MeanFilter[#, 1] & @ MaxDetect[fig, 0.95]
    ]

enter image description here

I careful crafted this matrix so that it has the same proportions as the target grid:

formMatrix = SparseArray[{Band[{ 1, 1 }, Automatic, {0,1}] -> 1,
                          Band[{15, 1 }, Automatic, {0,1}] -> 1,
                          Band[{29, 1 }, Automatic, {0,1}] -> 1,
                          Band[{52, 1 }, Automatic, {0,1}] -> 1,
                          Band[{66, 1 }, Automatic, {0,1}] -> 1,
                          Band[{ 1, 1 }, Automatic, {1,0}] -> 1,
                          Band[{ 1,105}, Automatic, {1,0}] -> 1,
                          Band[{ 1,146}, Automatic, {1,0}] -> 1,
                          Band[{ 1,265}, Automatic, {1,0}] -> 1},
                          {66,265}];
formFigure = ColorNegate @ ArrayPlot[formMatrix, AspectRatio -> Automatic, Frame -> False]

enter image description here

But when I try to use FindGeometricTransform, it fails. Maybe it does not work with hollow objects?

enter image description here

As I last resort, I am thinking about doing horizontal and vertical histograms and look for proportionally spaced peaks, but I want to ask the community before I over engineer a solution. Thanks in advance.

Gustavo Delfino
  • 8.7k
  • 1
  • 29
  • 60