def half_center_matrix(matrix, dim):
    """
    Cuts out matrix of half size of first 3 dimensions from the center of the original matrix.

    :param matrix: ndarray.
    :param dim: tuple of 3 ints.
    :return: ndarray. Half-sized matrix.
    """
    central_voxel = (dim[0] // 2, dim[1] // 2, dim[2] // 2)
    quarter_size = (dim[0] // 4, dim[1] // 4, dim[2] // 4)
    down = (
        central_voxel[0] - quarter_size[0], central_voxel[1] - quarter_size[1], central_voxel[2] - quarter_size[2])
    up = (
        central_voxel[0] + quarter_size[0], central_voxel[1] + quarter_size[1], central_voxel[2] + quarter_size[2])
    return matrix[down[0]:up[0], down[1]:up[1], down[2]:up[2]]