合并不重叠的数组块 [英] Merging non-overlapping array blocks

查看:91
本文介绍了合并不重叠的数组块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此功能将(512x512)二维数组划分为2x2块.

I divided a (512x512) 2-dimensional array to 2x2 blocks using this function.

skimage.util.view_as_blocks (arr_in, block_shape)
array([[ 0,  1,  2,  3],
   [ 4,  5,  6,  7],
   [ 8,  9, 10, 11],
   [12, 13, 14, 15]])
   >>> B = view_as_blocks(A, block_shape=(2, 2))
   >>> B[0, 0]
   array([[0, 1],
          [4, 5]])
   >>> B[0, 1]
   array([[2, 3],
          [6, 7]])

现在,我需要在操纵后将相同的块放置到其原始位置,但是为此我在skimage中看不到任何功能.

Now I need to put the same blocks to their original places after manipulation but I couldn't see any function in skimage for that.

像以前那样合并不重叠数组的最佳方法是什么?

What's the best way to merge the non-overlapping arrays as it was before?

谢谢!

推荐答案

使用转置/交换来交换第二个和第三个轴,然后重塑形状以合并最后两个轴-

Use transpose/swapaxes to swap the second and third axes and then reshape to have the last two axes merged -

B.transpose(0,2,1,3).reshape(-1,B.shape[1]*B.shape[3])
B.swapaxes(1,2).reshape(-1,B.shape[1]*B.shape[3])

样品运行-

In [41]: A
Out[41]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

In [42]: B = view_as_blocks(A, block_shape=(2, 2))

In [43]: B
Out[43]: 
array([[[[ 0,  1],
         [ 4,  5]],

        [[ 2,  3],
         [ 6,  7]]],


       [[[ 8,  9],
         [12, 13]],

        [[10, 11],
         [14, 15]]]])

In [44]: B.transpose(0,2,1,3).reshape(-1,B.shape[1]*B.shape[3])
Out[44]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

这篇关于合并不重叠的数组块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆