许峰玮
使用Python包MontagePy拼接两张二维图片
2022-9-20 22:57
阅读:1712

天文学家、天文观测者往往手中有多张不同时间拍摄的图片,这些图片有时在天空中坐标接近,甚至有重合之处。为了对整片大区域有一个更加系统的了解,我们往往需要将多片区域进行拼接(mosaic)。

该文档关注使用Python已有的MontagePy包实现对于多个FITS文件的拼接

(Montage官网:http://montage.ipac.caltech.edu/)

具体代码如下:

# include packages
from MontagePy.main import mGetHdr, mHdr, mImgtbl, mMakeHdr, mProjectQL, mAdd
import os
import shutil
import glob

def mkdir(filePath):
    '''
    Delete the original file first and then initate a new one
    '''
    try:
        shutil.rmtree(filePath)
    except:
        pass
    os.mkdir(filePath)

def make_mosaic(pre_mosaic_path='./', proj_path = './proj/', 
    final_path = './final/', output_name = 'final_mosaic.fits'):
    '''
    
    '''
    # create pre-mosaic image list
    rtn =  mImgtbl(pre_mosaic_path, 'pre_mosaic.tbl')
    # update the process
    print("mImgtbl (pre-mosaic image table):  " + str(rtn), flush=True)
    # create the header for the mosaic
    mMakeHdr('pre_mosaic.tbl', 'mosaic_template.hdr') 
    mkdir(proj_path)
    for each in glob.glob('*.fits'):
        # reproject all the pre-mosaic fields into the same template header
        mProjectQL(each, proj_path + each[:-5] + '_proj.fits', 
            pre_mosaic_path + 'mosaic_template.hdr')
    rtn = mImgtbl(proj_path, 'reprojected.tbl')
    # update the process
    print("mImgtbl (reprojected image table):  " + str(rtn), flush=True)
    mkdir(final_path)
    # Add the reprojected images into one mosaic image
    rtn = mAdd('./', 'reprojected.tbl', 'mosaic_template.hdr', 
        final_path+'final_mosaic.fits',debug=1)
    print("mAdd:  " + str(rtn), flush=True)

使用方法:

在某目录下统一放置一系列待拼接的FITS文件和内容为上述的.py文件(.py自行创建即可,或者下载https://github.com/XFengwei/python-code/blob/master/Mosaic/mosaic2D.py)。然后,在该目录下运行此Python代码。最终图像将在final/final_mosaic.fits找到。

转载本文请联系原作者获取授权,同时请注明本文来自许峰玮科学网博客。

链接地址:https://wap.sciencenet.cn/blog-3420944-1356183.html?mobile=1

收藏

分享到:

当前推荐数:0
推荐到博客首页
网友评论0 条评论
确定删除指定的回复吗?
确定删除本博文吗?