Python 在AxeImage系列中添加每个像素

Python 在AxeImage系列中添加每个像素,python,numpy,matplotlib,Python,Numpy,Matplotlib,我目前正在尝试使用python为给定的图像数据系列创建颜色图。在使用了一个名为Powderday的软件之后,我在存储器中(在我在代码第18行调用的名为“merge.070.new.rtout.image”的文件中)存储了一系列大约35幅图像,每幅图像都包含某个星系合并的特定波长的通量信息。我想通过这些图像中的每一个循环,创建一个最终的图像,基本上是这些图像中的每一个的叠加,这样我就可以在一个图像中有一系列的波长,而不是几个单波长的图像 要做到这一点,我想循环浏览每幅图像,将波长贴图保存在最终图像

我目前正在尝试使用python为给定的图像数据系列创建颜色图。在使用了一个名为Powderday的软件之后,我在存储器中(在我在代码第18行调用的名为“merge.070.new.rtout.image”的文件中)存储了一系列大约35幅图像,每幅图像都包含某个星系合并的特定波长的通量信息。我想通过这些图像中的每一个循环,创建一个最终的图像,基本上是这些图像中的每一个的叠加,这样我就可以在一个图像中有一系列的波长,而不是几个单波长的图像

要做到这一点,我想循环浏览每幅图像,将波长贴图保存在最终图像中,并继续向最终图像添加后续图像。唯一的问题是,每次我找到单波长图像时,我都会得到一个AxeImage,据我所知,它没有与另一个图像合并的功能。到目前为止,我在网上发现,最好的解决方案是从图像创建一个numpy数组,但我也找不到matplotlib.image中的get_image函数是否接受AxesImage参数,以便将其转换为这样的数组。我的代码如下

重要的行在:42-45,我试图初始化finalImg,以便在循环中“迭代”它;47-61在这里,我对每个图像进行迭代

另请注意:我正在读取的B_Johnson和B_thruput文件包含关于我在.image文件中拥有哪些波长的信息,以及相应的吞吐量。这是因为我想用每个波长的通量乘以它的吞吐量,以便正确地模拟实际的滤波器

希望这些信息能为这个问题提供一个良好的背景!我对python还是很陌生。将所有这些图像相加的最佳方法是什么

import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from hyperion.model import ModelOutput
from astropy.cosmology import Planck13
import astropy.units as u


# ------------------------
# modifiable header
# ------------------------

filters = np.loadtxt('B_Johnson.txt')
thru = np.loadtxt('B_thruput.txt', dtype='double')
第18行: 第42行: 循环中的第47-61行:
首先,不能将Matplotlib AxeImage加载到NumPy数组中

我不在您的字段中,但基于for
ModelOutput.get\u image()
我相信
get\u image()
会从您的代码中以NumPy数组的形式返回图像数据:

image = m.get_image(distance=distance, units='mJy')
查看
类型(图像)
以验证这一点。如果我是正确的,您应该看到
numpy.ndarray

如果是这种情况,那么
finalImg=mpimg.imread(cax)
是多余的…您已经在
image
变量中将图像作为NumPy数组加载

现在,如果您希望将数据作为单独的通道加载到单个ndarray对象中,则可以在
get_image()
中完成。打印
image.ndim
应显示
3
(您有一个三维图像阵列),其
img.shape
(y轴长度、x轴长度、通道数)

根据你提问的措辞,我认为你想通过获取每个通道中的强度总和,将这些通道组合成每个像素的单个强度值。此操作将生成形状
(y\u轴长度,x\u轴长度)
的二维灰度图像。为了理解我的意思,请考虑我为你起草的以下例子:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.cbook import get_sample_data

# load example image file
image_file = get_sample_data("grace_hopper.png", asfileobj=True)

# read example image file to NumPy array
image_array = plt.imread(image_file)

# show `image_array` as color (RGB) image
fig = plt.figure()
plt.imshow(image_array)
plt.tight_layout()
plt.axis('off') # turns off x and y-axis ticks
plt.show()


谢谢!有没有一种方法可以创建一个空数组而不保存任何颜色信息?或者我只是用for循环外的第一个图像初始化它,然后在循环内添加到它?没关系!我可以用np.empty_喜欢,刚刚找到。没问题!是的,np.empty_like将为您初始化一个空数组。我在这个例子中使用了np.zeros_,它返回一个填充了零的数组。我刚刚检查过,我的图像对象实际上是一个Hyperion.model.image.image对象。我将尝试找到一种方法将其转换为numpy数组。那么这行
image=m.get\u image(distance=distance,units='mJy')
返回一个Hyperion.model.image.image对象?回顾一下
get_image()
的文档,它实际上应该返回三个numpy数组。我不理解输出的术语,但是您的Hyperion.model.image.image对象可能将该数据存储在它的一个数据库中。打印
类型(image.wav)
会给你一个NumPy数组吗?
for idx, fil in enumerate(filters):
    wav = fil

    #find nearest wavelength
    iwav = np.argmin(np.abs(wav - image.wav))

    #find the throughput to multiply found flux by throughput
    throughput = thru[idx]

    #plot the beast

    cax = ax.imshow((image.val[0,:, :, iwav])*throughput,
                    cmap = plt.cm.spectral, origin='lower', extent=[-w.value, w.value, -w.value, w.value])

    finalImg += mpimg.imread(cax)

    plt.xlim([-image_width,image_width])
    plt.ylim([-image_width,image_width])


# Finalize the plot
ax.tick_params(axis='both', which='major', labelsize=10)
ax.set_xlabel('x kpc')
ax.set_ylabel('y kpc')

plt.colorbar(cax,label='Flux (mJy)',format='%.0e')

fig.savefig('pd_image_bj.png', bbox_inches='tight',dpi=150)
image = m.get_image(distance=distance, units='mJy')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.cbook import get_sample_data

# load example image file
image_file = get_sample_data("grace_hopper.png", asfileobj=True)

# read example image file to NumPy array
image_array = plt.imread(image_file)

# show `image_array` as color (RGB) image
fig = plt.figure()
plt.imshow(image_array)
plt.tight_layout()
plt.axis('off') # turns off x and y-axis ticks
plt.show()
# since the image is color (RGB), slice into individual channels
# to illustrate adding image arrays
c1 = image_array[:, :, 0]
c2 = image_array[:, :, 1]
c3 = image_array[:, :, 2]

# define empty array with same shape as one image slice
# this will become the final image result
new_array = np.zeros_like(c1)

# visualize empty array
fig0 = plt.figure()
plt.imshow(new_array, cmap='gray')
plt.tight_layout()
plt.axis('off')
plt.show()
# visualize distinct image slices (RGB image has three color channels)
# one at a time to illustrate differences between images
fig1, ax = plt.subplots(nrows=1, ncols=3)
for img, a in zip([c1, c2, c3], ax):
    a.imshow(img, cmap='gray')
    a.axis('off')

    # iteratively add image slices to `new_array`
    # while we are iterating over the slices
    new_array += img

plt.tight_layout()
plt.show()
# visualize end result of iteratively adding image slices to `new_array`
fig2 = plt.figure()
plt.imshow(new_array, cmap='gray')
plt.axis('off')
plt.tight_layout()
plt.show()