How to find the rectangle with those text

In this image:

I want to find this rectangle with all contents(without those grid's lines)

I think the total of the pixels can help a little like:

img = Import["http://i.stack.imgur.com/lnd5w.png"];
data = Total[ImageData[ImageRotate[ColorNegate[img]]]];
Show[ImageRotate[img],
ListLinePlot[data, PlotRange -> All, PlotStyle -> Red]]


And I have such images:

image2

image3

image4

image5

I have no idea to do this thing. Does anyone have an idea?

• Have you asked this question before? It seems very familiar to me... – Carl Lange Dec 28 '18 at 14:36
• Ah, I was thinking of this and this. Just for context purposes :) – Carl Lange Dec 28 '18 at 14:37
• @CarlLange It's not very similar... – yode Dec 28 '18 at 14:37
• The question isn't but the images are, so I wanted to make sure it wasn't a duplicate somehow. That's all! – Carl Lange Dec 28 '18 at 15:10

Binarization and ComponentMeasurements seems the straightforward solution:

img = Import["https://i.stack.imgur.com/lnd5w.png"];


Selct all components that are less than half as long as the image:

comp = ComponentMeasurements[MorphologicalBinarize[ColorNegate@img],
"BoundingBox", #CaliperLength < Min[ImageDimensions[img]]*0.5 &];


This selects all digits and boxes, but not the grid lines, because they are longer than height/2:

HighlightImage[img, Rectangle @@@ comp[[All, 2]]]


Then combine the individual bounding boxes to one big bounding box:

HighlightImage[img,
Rectangle @@
Transpose[MinMax /@ Transpose[Flatten[comp[[All, 2]], 1]]]]


You might have to adjust binarization and the criteria in ComponentMeasurements, but for me, it worked on all the images you posted on the first try.

• Sorry for catching a cold to delay response. Your method is very simple to understand. But your method cannot deal with the image5. This is why I want to do it with signal-processing method... – yode Jan 3 at 10:54
• @yode: Sure it can, you just need to adjust the criteria function in ComponentMeasurements. – Niki Estner Jan 3 at 11:13

This works quite well:

findRegion[img_] := TextRecognize[img, "BoundingBox", RecognitionPrior -> "SparseText"]

HighlightImage[#, findRegion@#] & /@ imgs


As you can see, the result for the first image is not perfect unfortunately, but hopefully this gives you some ideas.

• Yes, as your try, the Tesseract not very stable.. – yode Dec 28 '18 at 14:40