How can I get the top-right box having top-left and bottom-right coordinates?

Viewed 495

Let's put some context. I'm trying to reach some values for my app and I need to take the red box of the img.

Words

I've got all the top_left and bottom_right coordinates of these words.

[Word(top_left=(608, 11), bottom_right=(821, 38), confidence=99.45719909667969, text='PANELBOARD'), 
Word(top_left=(25, 12), bottom_right=(106, 39), confidence=99.0506591796875, text='CH1L'), 
Word(top_left=(112, 12), bottom_right=(282, 44), confidence=97.45893859863281, text='(EXISTING)'), 
Word(top_left=(1264, 39), bottom_right=(1346, 55), confidence=83.25164031982422, text='208Y/120V,'), 
Word(top_left=(1350, 39), bottom_right=(1410, 55), confidence=94.61625671386719, text='3PH,4W'), 
Word(top_left=(1379, 60), bottom_right=(1412, 75), confidence=98.55554962158203, text='MLO'), 
Word(top_left=(92, 61), bottom_right=(215, 75), confidence=99.64352416992188, text='REFRIGERATED'), 
Word(top_left=(219, 61), bottom_right=(262, 75), confidence=99.87834930419922, text='CASE'), 
Word(top_left=(265, 61), bottom_right=(318, 75), confidence=99.78636932373047, text='LIGHTS'), 
Word(top_left=(320, 61), bottom_right=(331, 75), confidence=99.71800231933594, text='&'), 
Word(top_left=(19, 61), bottom_right=(87, 76), confidence=99.207763671875, text='SERVES:'), 
Word(top_left=(334, 61), bottom_right=(402, 76), confidence=99.7181625366211, text='RECEPTS'), 
Word(top_left=(1335, 61), bottom_right=(1376, 76), confidence=98.63665771484375, text='250A,'), 
Word(top_left=(18, 83), bottom_right=(104, 97), confidence=98.90849304199219, text='LOCATION:'), 
Word(top_left=(108, 83), bottom_right=(155, 97), confidence=99.3512954711914, text='COMP'), 
Word(top_left=(157, 83), bottom_right=(211, 97), confidence=99.74064636230469, text='HOUSE'), 
Word(top_left=(214, 83), bottom_right=(221, 97), confidence=97.91343688964844, text='1'), 
Word(top_left=(1376, 103), bottom_right=(1408, 118), confidence=98.71705627441406, text='BUS'), 
Word(top_left=(18, 104), bottom_right=(110, 118), confidence=99.58409118652344, text='MOUNTING:'), 
Word(top_left=(112, 104), bottom_right=(183, 118), confidence=99.85911560058594, text='SURFACE'), 
Word(top_left=(1210, 104), bottom_right=(1304, 118), confidence=99.44200134277344, text='EQUIPMENT'), 
Word(top_left=(1307, 104), bottom_right=(1374, 118), confidence=99.8482666015625, text='GROUND')]

The problem comes when "MLO" has a x bigger than "3PH, 4W" so sorting by x and then by y doesn't work (Yes the OCR detects "3PH, 4W" as one hehe)

I tried to follow the rule "top-right point will have the smallest difference" but it didn't work too.

Any solution?

Thanks for your time.

1 Answers

This question is easy to answer if you know either of those two information:

  • The coordinates of the upper-right corner of the overall surrounding box;
  • The direction vector of the lowerleft - upperright diagonal of the overall surrounding box.

In the former case, calling B the point in the upper-right corner, then the "most top-right" point is the closest point to the upper-right corner; which is the point P who minimizes the Euclidean distance math.dist(B, P).

In the latter case, calling v the direction vector of the lowerleft - upperright diagonal, and fixing O = (0,0) as the origin, the "most top-right" point is the point P which maximizes the dot-product v.OP (where OP stands for "vector OP" and has same coordinates as point P).

You didn't give the coordinates of the upper-right corner in your question; however, approximating the direction vector of the diagonal as v = (1, -1), the dot-product between v and a point P = (x,y) is just x-y; hence you can get the box with the "most top-right" top-right corner this way:

>>> def toprightness(w):
...   return w.bottom_right[0] - w.top_left[1]
... 
>>> max(data, key=toprightness).text
'3PH,4W'
Related