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]]]];
 ListLinePlot[data, PlotRange -> All, PlotStyle -> Red]]

And I have such images:





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

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

enter image description here

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

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

enter image description here

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.

| improve this answer | |
  • $\begingroup$ 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... $\endgroup$ – yode Jan 3 '19 at 10:54
  • $\begingroup$ @yode: Sure it can, you just need to adjust the criteria function in ComponentMeasurements. $\endgroup$ – Niki Estner Jan 3 '19 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.

| improve this answer | |
  • $\begingroup$ Yes, as your try, the Tesseract not very stable.. $\endgroup$ – yode Dec 28 '18 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.