def center_crop(data, crop_shape, labels=None):
    """
    Perform random cropping after optional padding
    Args:
        data: data array representing 1 image [h, w]
        labels: labels array [h, w, 2] (unique values limited to (0, 1)), could be None
        crop_shape: target shape after cropping

    Returns:
        Randomly cropped data and labels
    """
    data_shape = data.shape[0:2]
    assert (crop_shape[0] <= data_shape[0])
    assert (crop_shape[1] <= data_shape[1])

    nh = int((data_shape[0] - crop_shape[0]) / 2)
    nw = int((data_shape[1] - crop_shape[1]) / 2)
    data = data[nh:nh + crop_shape[0], nw:nw + crop_shape[1]]
    if labels is not None:
        labels = labels[nh:nh + crop_shape[0], nw:nw + crop_shape[1], :]
        return data, labels
    return data