0

I am not sure how to title my question. Only terms I know are broadcasting and element wise. Please correct my title if is not accurate.

Background:
Previously I was working on text detection. However, detected picture is a bit smaller I can't get the whole information from the cropped image. I understand that I am using English based classifier to do word detection.

Therefore I have to fine tune the cropped coords a bit for overlapping Thai sentence. Here is the example of a bit smaller cropped area. You can see punctuation and tone sign are partially lost

cropped picture

I have 3 squares represented in array called pts

ipdb> pts
array([[[ 436, 3085],
        [2968, 3081],
        [2968, 3227],
        [ 436, 3232]],

       [[1222,  397],
        [1833,  400],
        [1833,  498],
        [1221,  496]],

       [[  86, 3275],
        [2968, 3268],
        [2968, 3421],
        [  87, 3427]]], dtype=int32)
ipdb> pts.shape
(3, 4, 2)

Later on I do a calculation to find the center of them using np.sum(pts, axis=1) / 4.0. I put it to center_pts

ipdb> center_pts
array([[1702.  , 3156.25],
       [1527.25,  447.75],
       [1527.25, 3347.75]])
ipdb> center_pts.shape
(3, 2)

Ultimate Goal:
I want to cover the losing punctuation and tone sign

Attempt:
I am trying to do subtraction to make a vector reference from center of the square and point to each corner. And times with small factor to cover the lost part of cropped image.

My first idea is sacrifice one for loop and the use element wise to take advantage of broadcasting like this.

pts[0] - center_pts[0]
array([[-1266.  ,   -71.25],
       [ 1266.  ,   -75.25],
       [ 1266.  ,    70.75],
       [-1266.  ,    75.75]])

vectors pointing to 4 corners

Questions:
1. Is it possible to do it without for-loop?
2. Are they any more productive to expand my square?

joe
  • 8,383
  • 13
  • 61
  • 109

1 Answers1

0

This could be done with a little help of broadcasting:

pts - center_pts[:,None]

array([[[-1266.  ,   -71.25],
        [ 1266.  ,   -75.25],
        [ 1266.  ,    70.75],
        [-1266.  ,    75.75]],

       [[ -305.25,   -50.75],
        [  305.75,   -47.75],
        [  305.75,    50.25],
        [ -306.25,    48.25]],

       [[-1441.25,   -72.75],
        [ 1440.75,   -79.75],
        [ 1440.75,    73.25],
        [-1440.25,    79.25]]])
yatu
  • 86,083
  • 12
  • 84
  • 139