def bbox_3d(img3):
    """calculates a minimal rectangular cube which includes all the non-zero values of mask.
    This is known as a bbox.
    Parameters
    ----------
    img3: 'numpy.ndarray', (d1, d2, d3)
        the mask including ones and zeros
    Returns
    -------
    mask_bbox: 'numpy.ndarray', (e1, e2, e3)
    """
    import numpy as np
    a_t = np.where(img3 != 0)
    bbox = np.min(a_t[0]), np.max(a_t[0]) + 1, np.min(a_t[1]), np.max(a_t[1]
        ) + 1, np.min(a_t[2]), np.max(a_t[2]) + 1
    mask_bbox = img3[bbox[0]:bbox[1], bbox[2]:bbox[3], bbox[4]:bbox[5]]
    return mask_bbox
