在numpy数组上重复函数

2024-04-25 12:35:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着使两个图像的形状匹配。你知道吗

我的方法是在较小图像的底部添加空白切片,直到它们的Z轴堆栈数匹配(假设两个图像的X和Y都具有相同的形状)。首先将图像转换为numpy数组,然后使用np.连接向数组末尾添加带零的数组。你知道吗

我的代码如下所示:

    x = 0
    difference = model.shape[1] - image.shape[1]

    # This line takes the difference between the larger image's Z stack 
    # slice number with the smaller image and get the difference between
    # their z stack slice number.

    while x <= difference: 
        new_np_array = np.concatenate((the_image_np_array, zeros_np_array), axis=0)
        x += 1

然而,这是行不通的,因为程序基本上是定义同一个变量三次。我的问题是,如何重复这个函数(np.连接)在同一个数组上执行X次?你知道吗


Tags: the方法图像imagenumberstacknpslice
3条回答

附加到一个列表,并在最后连接一个列表,效率更高。更容易纠正

alist = []
while x <= difference: 
    alist.append(zeros_np_array)
    x += 1
arr = np.array(alist)
# arr = np.vstack(alist) # alternative

我不确定我是否完全理解你想要实现的目标,但是你是否考虑过将图像转换成PIL图像并使用图像.调整大小功能?你知道吗

PIL Image docs

如果我正确理解了这个问题,您必须将连接结果赋给数组本身。你知道吗

  while x <= difference: 
        the_image_np_array = np.concatenate((the_image_np_array, zeros_np_array), axis=0)
        x += 1

相关问题 更多 >