def _change_bb_reference()

in ocr/utils/iam_dataset.py [0:0]


    def _change_bb_reference(self, bb, relative_bb, bb_reference_size, relative_bb_reference_size, output_size, operator):
        ''' Helper function to convert bounding boxes relative into another bounding bounding box.
        Parameter
        --------
        bb: [[int, int, int, int]]
            Bounding boxes (x, y, w, h) in percentages to be converted.

        relative_bb: [int, int, int, int]
            Reference bounding box (in percentages) to convert bb to 

        bb_reference_size: (int, int)
            Size (h, w) in pixels of the image containing bb

        relative_bb_reference_size: (int, int)
            Size (h, w) in pixels of the image containing relative_bb

        output_size: (int, int)
            Size (h, w) in pixels of the output image

        operator: string
            Options ["plus", "minus"]. "plus" if relative_bb is within bb and "minus" if bb is within relative_bb

        Returns
        -------
        bb: [[int, int, int, int]]
            Bounding boxes (x, y, w, h) in percentages that are converted
        
        '''        
        (x1, y1, x2, y2) = (bb[:, 0], bb[:, 1], bb[:, 0] + bb[:, 2], bb[:, 1] + bb[:, 3])
        (x1, y1, x2, y2) = (x1 * bb_reference_size[1], y1 * bb_reference_size[0],
                            x2 * bb_reference_size[1], y2 * bb_reference_size[0])

        if operator == "plus":
            new_x1 = (x1 + relative_bb[0] * relative_bb_reference_size[1]) / output_size[1]
            new_y1 = (y1 + relative_bb[1] * relative_bb_reference_size[0]) / output_size[0]
            new_x2 = (x2 + relative_bb[0] * relative_bb_reference_size[1]) / output_size[1]
            new_y2 = (y2 + relative_bb[1] * relative_bb_reference_size[0]) / output_size[0]
        else:
            new_x1 = (x1 - relative_bb[0] * relative_bb_reference_size[1]) / output_size[1]
            new_y1 = (y1 - relative_bb[1] * relative_bb_reference_size[0]) / output_size[0]
            new_x2 = (x2 - relative_bb[0] * relative_bb_reference_size[1]) / output_size[1]
            new_y2 = (y2 - relative_bb[1] * relative_bb_reference_size[0]) / output_size[0]

        new_bbs = np.zeros(shape=bb.shape)
        new_bbs[:, 0] = new_x1
        new_bbs[:, 1] = new_y1
        new_bbs[:, 2] = new_x2 - new_x1
        new_bbs[:, 3] = new_y2 - new_y1
        return new_bbs