def bbox(img, margin=20):
    """
    Compute bounding box of a binary mask and add a maring (only in axial plane).
    """

    coords=[0,img.shape[0],0,img.shape[1],0,img.shape[2]]

    # i
    for i in range(img.shape[0]):
        if 1 in img[i,:,:]:
            coords[0]=i
            break
    for i in range(img.shape[0]-1,-1,-1):
        if 1 in img[i,:,:]:
            coords[1]=i
            break
    # j     
    for j in range(img.shape[1]):
        if 1 in img[:,j,:]:
            coords[2]=j - margin
            break
    for j in range(img.shape[1]-1,-1,-1):
        if 1 in img[:,j,:]:
            coords[3]=j + margin
            break
    # k
    for k in range(img.shape[2]):
        if 1 in img[:,:,k]:
            coords[4]=k - margin
            break
    for k in range(img.shape[2]-1,-1,-1):
        if 1 in img[:,:,k]:
            coords[5]=k + margin
            break

    assert coords[0] >= 0 and coords[2] >= 0 and coords[4] >= 0
    assert coords[1] <= img.shape[0]-1 and coords[3] <= img.shape[1]-1 and coords[5] <= img.shape[2]-1

    return coords