import torch


def xyxy2xcycwh(xyxy, inplace=False):
    """Convert boxes in (x1,y1,x2,y2) to (xc,yc,w,h)
    Args:
        xyxy(Tensor[N, 4]): N boxes in (x1,y1,x2,y2)
        inplace(bool): whether to modify the input inplace as output or make the results a new copy
    Returns:
        xcycwh(Tensor[N, 4]): N boxes in (xc,yc,w,h)
    """
    xcycwh = torch.zeros_like(xyxy)
    xcycwh[:, 0] = (xyxy[:, 0] + xyxy[:, 2]).div(2, rounding_mode='floor')  # xc
    xcycwh[:, 1] = (xyxy[:, 1] + xyxy[:, 3]).div(2, rounding_mode='floor')  # yc
    xcycwh[:, 2] = xyxy[:, 2] - xyxy[:, 0] + 1     # width
    xcycwh[:, 3] = xyxy[:, 3] - xyxy[:, 1] + 1     # height
    if inplace:
        xyxy.copy_(xcycwh)
        return xyxy
    return xcycwh