3
$\begingroup$

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["https://i.sstatic.net/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?

$\endgroup$
4
  • 1
    $\begingroup$ Have you asked this question before? It seems very familiar to me... $\endgroup$
    – Carl Lange
    Commented Dec 28, 2018 at 14:36
  • $\begingroup$ Ah, I was thinking of this and this. Just for context purposes :) $\endgroup$
    – Carl Lange
    Commented Dec 28, 2018 at 14:37
  • $\begingroup$ @CarlLange It's not very similar... $\endgroup$
    – yode
    Commented Dec 28, 2018 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
    Commented Dec 28, 2018 at 15:10

2 Answers 2

5
$\begingroup$

Binarization and ComponentMeasurements seems the straightforward solution:

img = Import["https://i.sstatic.net/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:

HighlightImage[img, 
 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.

$\endgroup$
2
  • $\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
    Commented Jan 3, 2019 at 10:54
  • $\begingroup$ @yode: Sure it can, you just need to adjust the criteria function in ComponentMeasurements. $\endgroup$ Commented Jan 3, 2019 at 11:13
3
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Yes, as your try, the Tesseract not very stable.. $\endgroup$
    – yode
    Commented Dec 28, 2018 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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