Lookuper的个人博客分享 http://blog.sciencenet.cn/u/Lookuper

博文

使用Python包MontagePy拼接两张二维图片

已有 1754 次阅读 2022-9-20 22:57 |个人分类:软件|系统分类:科研笔记

天文学家、天文观测者往往手中有多张不同时间拍摄的图片,这些图片有时在天空中坐标接近,甚至有重合之处。为了对整片大区域有一个更加系统的了解,我们往往需要将多片区域进行拼接(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://blog.sciencenet.cn/blog-3420944-1356183.html

上一篇:如何使用Matplotlib.axes绘制DS9定义的Regions对象
下一篇:申请Hubble Fellow以及其他天文机构博士后笔记
收藏 IP: 124.205.78.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-18 01:00

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部