【问题标题】:How do I crop a fits.gz file and then view the cropped image using Python?如何裁剪 fit.gz 文件,然后使用 Python 查看裁剪后的图像?
【发布时间】:2021-12-16 15:35:13
【问题描述】:

我有几个 fit.gz 文件,其中包含我需要围绕特定坐标裁剪的大图像。我有我需要围绕作物中心的点的图像坐标,我想围绕这个点创建一个 2048x2048 图像。我还需要能够查看裁剪后的图像以确保一切正常,最好在中心点周围有一个小圆圈或其他东西。

我尝试使用打开的 fit 文件中的信息

hdu_list = fit.open(filename.fits.gz)

image_data = hdu_list[0].data

然后使用Cutout2D,但我无法将结果保存为FITS图像,也无法查看cutout是否有效。

似乎应该有一个非常简单快捷的方法来做到这一点,但我在任何地方都找不到。

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。
  • 请提供此类图像的代表性来源的链接和一组合适的坐标。
  • 当你写“我不能”时,这两种情况是什么意思?什么东西阻止你?有错误吗?
  • 保存到image_data的数据不是图片,所以不能保存为FITS文件。而且我只是找不到任何可以让我查看图像的代码。下载它很简单,但是我找不到可以让我查看刚刚下载的 FITS 图像的代码。

标签: python image crop astropy fits


【解决方案1】:

您可以将剪切的结果放入fits.PrimaryHDU 对象中,并按照此处的示例将其写出来:

https://docs.astropy.org/en/stable/nddata/utils.html#saving-a-2d-cutout-to-a-fits-file-with-an-updated-wcs

生成的 FITS 图像可以在任何 FITS 查看器中打开。

【讨论】:

    【解决方案2】:

    Cutout2D 类不能直接写入 FITS 文件或查看。

    • fits.writeto 可以使用numpy.ndarray 作为输入方便地写出 FITS 文件
    • Cutout2D.data 属性是带有剪切结果的numpy.ndarray,可用作fits.writeto 的输入
    • 如果你想直接查看matplotlib.pyplot.imshow函数也可以用来查看Cutout2D.data而不用写成FITS文件

    有关更多信息和示例,请参阅 astropy 文档。 https://docs.astropy.org/en/stable/api/astropy.nddata.Cutout2D.html

    以下是使用 Cutout2D 结果并将其写入 FITS 文件的示例。该 FITS 文件与输入一样具有主 HDU 中的图像数据。

    import numpy as np
    from astropy.io import fits
    from astropy.nddata.utils import Cutout2D
    
    # read in the FITS file with image data in the primary HDU
    hdu_list = fits.open('filename.fits.gz')
    image_data = hdu_list[0].data
    
    # perform the cutout with cutout 2d
    coords = (2500, 2600)
    newsize = (2048, 2048)
    cut = Cutout2D(image_data, position=coords, size=newsize)
    
    # IMPORTANT: to write out as fits file cut.data must be used
    fits.writeto('cutout.fits', cut.data)
    

    如果您想在不写入文件的情况下查看 Cutout2D

    import matplotlib.pyplot as plt
    
    # show 2D image using matplotlib
    plt.imshow(cut.data)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 2015-02-15
      • 2017-05-22
      • 1970-01-01
      相关资源
      最近更新 更多