[英]"Rotate" array ordering with numpy
我试图弄清楚如何在numpy这种情况下订购:
首先,我创建了带有两个 for 循环的图像块(仅作为示例):
cuts = 9 # This is for example, there could be much more blocks
for row in range(cuts):
for column in range(cuts):
block_min_x = (1 / cuts) * row
block_max_x = (1 / cuts) * (row + 1)
block_min_y = (1 / cuts) * column
block_max_y = (1 / cuts) * (column + 1)
然后我将这些“图像块”添加到名称为“img_1_1、img_1_2、img_1_3、img_2_1、img_2_2、img_2_3...”的数组中。 所以它在数组中看起来像这样(以数字为例):
[6,7,8] [15,16,17] [24,25,26]
[3,4,5] [12,13,14] [21,22,23]
[0,1,2] [9,10,11] [18,19,20]
是否有某种方法可以使用 numpy 对这些块进行排序:
[0,1,2] [3,4,5] [6,7,8]
[9,10,11] [12,13,14] [15,16,17]
[18,19,20] [21,22,23] [24,25,26]
这是图像以更好地展示我正在寻找的内容:
我不确定这种问题是否有某种术语,所以抱歉不能使用正确的术语。 问题似乎是我们需要“顺时针旋转”该顺序,如示例图像中的蓝线所示。 所以问题是,如何在 numpy 中排序? 此外,如果它可以在那些 for 循环中改变,那就很高兴知道了。
使用numpy.rot90
:
In [11]: a = np.array([[6,7,8],[3,4,5], [0,1,2]])
In [12]: b = np.array([[15,16,17], [12,13,14], [9,10,11]])
In [13]: c = np.array([[24,25,26], [21,22,23], [18,19,20]])
In [14]: print(np.array([a, b, c]))
[[[ 6 7 8]
[ 3 4 5]
[ 0 1 2]]
[[15 16 17]
[12 13 14]
[ 9 10 11]]
[[24 25 26]
[21 22 23]
[18 19 20]]]
In [15]: print(np.rot90([a, b, c]))
[[[ 0 1 2]
[ 9 10 11]
[18 19 20]]
[[ 3 4 5]
[12 13 14]
[21 22 23]]
[[ 6 7 8]
[15 16 17]
[24 25 26]]]
尽管这不是用 numpy 完成的,但仍然对某人有用。 我通过像这样循环它们得到了正确的顺序。 我确信这可以在代码中做得更好,但现在看来这可行:
# Order rendered images (to 1, 2, 3, 4 from 3, 1, 2, 4)
rendered_images_ordered = []
for y in range(parts_count, 0, -1):
for x in range(parts_count):
rendered_images_ordered.append(rendered_images[(y-1)+(x*parts_count)])
之前的输出:
['Fart_1_1.png', 'Fart_1_2.png', 'Fart_1_3.png', 'Fart_2_1.png', 'Fart_2_2.png', 'Fart_2_3.png', 'Fart_3_1.png', 'Fart_3_2.png', 'Fart_3_3.png']
输出后:
['Fart_1_3.png', 'Fart_2_3.png', 'Fart_3_3.png', 'Fart_1_2.png', 'Fart_2_2.png', 'Fart_3_2.png', 'Fart_1_1.png', 'Fart_2_1.png', 'Fart_3_1.png']
如果对我在哪里使用它更感兴趣。 我为搅拌机插件创建了这个,称为RenderFarts 。 在那里我需要在图像合并功能中进行此排序,我在其中渲染零件并需要将它们按正确的顺序排列。 合并过程还没有工作,但这个排序似乎工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.