python - 使用多个适合图像制作图像立方体
问题描述
我有一堆适合文件,可以使用以下脚本读取
from astropy.io import fits
hdu = fits.open('file.fits')
data = hdu[0].data
我正在尝试使用从多个拟合文件中读取的数据制作图像立方体。(图像立方体是包含来自多个拟合文件的数据的 3D 图像,其中 x 和 y 轴是 2D 图像维度,第 3 轴是时间或频率)
我相信它可以使用spectral _cube 模块来完成,但是大多数文档只讨论如何读取图像立方体,而不是如何使用单独的拟合文件制作图像立方体。
到目前为止,我已经尝试了以下脚本。
#In the below script data is a 3D numpy array
from spectral_cube import SpectralCube
cube = SpectralCube(data=data)
cube.write('new_cube.fits', format='fits')
但是,上面的脚本给出了一个错误,说需要 3 个参数,而只给出了 2 个。
解决方案
最简单的方法是简单地将您想要在立方体中的图像放入一个numpy
数组中,然后将该数组保存为 fit 文件。您也可以直接将它们保存到numpy
数组中,但是如果您在 for 循环中执行附加列表会更容易,而不是像我在这里那样为每个图像显式执行它。
import numpy as np
from astropy import fits
# Read the images you want to concatenate into a cube
img1 = fits.getdata('img1.fits')
img2 = fits.getdata('img2.fits')
# Make a list that will hold all your images
img_list = []
img_list.append(img1)
img_list.append(img2)
# Cast the list into a numpy array
img_array = np.array(img_list)
# Save the array as fits - it will save it as an image cube
fits.writeto('mycube.fits', img_array)
推荐阅读
- javascript - 使用 php 和 javascript 时如何在表单中显示错误消息
- scala - 如何在 scala 中初始化泛型变量
- c++ - 从列表中查找素数的总数
- javascript - 如何使用 flexbox 和 React-Native 使裁剪后的图像填充所有视图?
- python - Python Django:如何将模型的数据传递给管理员默认起始页(index.html)?
- python - 无法设置自定义下载路径 Selenium
- html - 内容超出 div 并进入下一个 div
- javascript - 相同的函数在 chrome javascript 和 nodejs 中返回不同
- google-api - 使用 Google Drive API 时,如何限制我的 Google Cloud Client ID 只能访问一位用户的 Google Drive 数据?
- c# - 使用 Xamarin Forms 获取设备序列号