def bg_mask(in_file, in_mask, out_file=None):
    """
    Rough mask of background from brain masks
    """
    import nibabel as nb
    import numpy as np
    from scipy.ndimage import binary_dilation
    import scipy.ndimage as nd
    import os.path as op
    if out_file is None:
        fname, ext = op.splitext(op.basename(in_file))
        if ext == '.gz':
            fname, ext2 = op.splitext(fname)
            ext = ext2 + ext
        out_file = op.abspath('%s_bgmask%s' % (fname, ext))
    im = nb.load(in_file)
    hdr = im.header.copy()
    hdr.set_data_dtype(np.uint8)
    hdr.set_xyzt_units('mm')
    msk = nb.load(in_mask).get_data()
    msk = 1 - binary_dilation(msk, structure=np.ones((20, 20, 20)))
    nb.Nifti1Image(msk.astype(np.uint8), im.affine, hdr).to_filename(out_file)
    return out_file
