import torch


def cxcywh_to_ltrb(boxes, as_tuple=False):
    """(center_x, center_y, width, height) -> (left, top, right, bottom)  (已测试)

    :param boxes: shape(NUM/..., 4). (center_x, center_y, width, height)
    :param as_tuple:
        if True: return len(4 * shape(NUM/...,)). False: return shape(NUM/..., 4)
    :return: len(4 * shape(NUM/...,)) or shape(NUM/..., 4). (left, top, right, bottom)
    """
    center_x, center_y, width, height = boxes[..., 0], boxes[..., 1], boxes[..., 2], boxes[..., 3]
    left, top = center_x - width / 2, center_y - height / 2
    right, bottom = left + width, top + height
    if as_tuple:
        return left, top, right, bottom
    else:
        return torch.stack([left, top, right, bottom], -1)