【问题标题】:Stacking two 3D numpy arrays along the median of the z-axis and keep the size of the smaller array沿 z 轴的中位数堆叠两个 3D numpy 数组并保持较小数组的大小
【发布时间】:2021-01-19 08:59:45
【问题描述】:

我想根据 z 轴的中位数堆叠两组图像(im-series1 = 32x32x16 和 im_series2 = 32x32x21),并保持相对于 im-series1 1 形状的相邻值。

【问题讨论】:

    标签: python image numpy stack concat


    【解决方案1】:

    你应该做的是先裁剪im_series2,然后堆叠。请注意,有两种方法可以裁剪 im_series 以适合 im_series1,两者都是“中位数”。

    import numpy as np
    im_series2 = np.ones((32, 32, n)) # this is im_series2 as an example
    mid = n // 2
    im_series2 = im_series2[:, :, mid-8:mid+8] # this is the cropping. [:,:,2:-3] is also valid
    print(im_series2.shape)
    im_series1 = np.ones((32, 32, 16)) # this is im_series1 as an example
    print(im_series1 .shape)
    c = np.concatenate((im_series1 , im_series2), axis=-1) # this concatenates them on the z_axis
    print(c.shape)
    

    这个输出:

    (32, 32, 16)
    (32, 32, 16)
    (32, 32, 32)
    

    【讨论】:

    • 谢谢你有没有更通用的方法来处理成堆的图像?像 im_series1(32x32x16), im_series2 (32x32x21), im_series3 (32x32x26), ... ->im_series1(32x32x16), im_series2 (32x32x16), im_series3 (32x32x16), ...
    • 你注意到有两种方法可以裁剪im_series2(见我在第三行的评论),都可以吗?在您更一般的情况下,我可以假设最后一个频道始终采用16 + 5 * x 的形式,即 16,26,31,36,41...?
    • 他们都适用于这个例子。没有最后一个通道是任意长度的。理想情况下,我想要一个函数,它采用最后一个通道的中值并以两种方式将其扩展一些数字。
    • 好的,请参阅我编辑的答案。这也适用于一般情况。如果它解决了您的问题,请将其标记为 :)
    猜你喜欢
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    相关资源
    最近更新 更多