1

と呼ばれるリストに保存された複数の2D配列がありますimage_concat. このリストは 100 を超えるこれらの配列で構成されますが、今のところ、2 つだけのリストに対してコードを実行しようとしています。これらの配列はすべて異なる形状を持っています。すべての配列から最大の x 次元と最大の y 次元を見つけて、他のすべての配列の端に十分なゼロを埋め込んで、最終的にすべての配列を埋めたいと思います。同じ形をしています。最大の x 次元と最大の y 次元は、別々の配列に属している場合もあれば、同じ配列に属している場合もあることに注意してください。これまでに書いてみたことは、何らかの理由で小さい方の配列の形状をうまく変更できませんでした。ただし、形状の要素が偶数または奇数であるために、一部の配列が最終的に1つずれている可能性があるため、形状を変更した後でもいくつかの問題が発生すると思います。

import astropy
import numpy as np
import math
import matplotlib.pyplot as plt
from astropy.utils.data import download_file
from astropy.io import fits

images = ['http://irsa.ipac.caltech.edu/ibe/data/wise/allsky/4band_p1bm_frm/9a/02729a/148/02729a148-w2-int-1b.fits?center=89.353536,37.643864deg&size=0.6deg', 'http://irsa.ipac.caltech.edu/ibe/data/wise/allsky/4band_p1bm_frm/2a/03652a/123/03652a123-w4-int-1b.fits?center=294.772333,-19.747157deg&size=0.6deg']

image_list = []
for url in images:
    image_list.append(download_file(url, cache=True))

image_concat = [fits.getdata(image) for image in image_list]

# See shapes in the beginning
print(np.shape(image_concat[0]))
print(np.shape(image_concat[1]))

def pad(image_concat):

    # Identify largest x and y dimensions
    xdims, ydims = np.zeros(len(image_concat)), np.zeros(len(image_concat))
    for i in range(len(xdims)):
        x, y = np.shape(image_concat[i])
        xdims[i] = x
        ydims[i] = y
    x_max = int(np.max(xdims))
    y_max = int(np.max(ydims))

    # Pad all arrays except the largest dimensions
    for A in image_concat:
        x_len, y_len = np.shape(A)
        print(math.ceil((y_max-y_len)/2))
        print(math.ceil((x_max-x_len)/2))

        np.pad(A, ((math.ceil((y_max-y_len)/2), math.ceil((y_max-y_len)/2)), (math.ceil((x_max-x_len)/2), math.ceil((x_max-x_len)/2))), 'constant', constant_values=0)

    return image_concat

image_concat = pad(image_concat)

# See shapes afterwards (they haven't changed for some reason)
print(np.shape(image_concat[0]))
print(np.shape(image_concat[1]))

この場合、形状が変化しない理由がわかりません。また、これを簡単に一般化して、次元が偶数か奇数かに関係なく、多くの配列で機能する方法はありますか?

4

1 に答える 1