【问题标题】:Repeating a function on a numpy array在 numpy 数组上重复一个函数
【发布时间】:2018-07-18 08:50:41
【问题描述】:

我正在尝试使两个 .tif 图像的图像形状匹配。

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

我的代码如下所示:

    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

但是,这行不通,因为程序基本上定义了相同的变量 3 次。我的问题是,如何在同一个数组上重复函数(np.concatenate)X 次?

【问题讨论】:

    标签: python numpy tiff


    【解决方案1】:

    如果我对问题的理解正确,您必须将连接结果分配给数组本身。

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

    【讨论】:

      【解决方案2】:

      我不确定我是否完全理解您要完成的工作,但您是否考虑过将图像转换为 PIL 图像并使用 Image.resize 函数?

      PIL Image docs

      【讨论】:

      • 我会考虑将链接中的相关内容添加到您的帖子正文中,或者考虑将其作为评论而不是答案发布。
      【解决方案3】:

      追加到列表中并在最后进行连接会更有效。而且更容易做对

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

      【讨论】:

        猜你喜欢
        • 2018-10-25
        • 1970-01-01
        • 1970-01-01
        • 2021-05-07
        • 1970-01-01
        • 2021-02-10
        • 2017-05-06
        • 2017-12-27
        • 2020-07-11
        相关资源
        最近更新 更多