简体   繁体   English

使用numpy“旋转”数组排序

[英]"Rotate" array ordering with numpy

I'm trying to figure out how to order in numpy this kind of situation:我试图弄清楚如何在numpy这种情况下订购:

First I created image blocks with two for loops (just for example):首先,我创建了带有两个 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)

Then I add those "image blocks" to array with names like "img_1_1, img_1_2, img_1_3, img_2_1, img_2_2, img_2_3..." in that order.然后我将这些“图像块”添加到名称为“img_1_1、img_1_2、img_1_3、img_2_1、img_2_2、img_2_3...”的数组中。 So it looks like this in array (example with numbers):所以它在数组中看起来像这样(以数字为例):

[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]

Is there somekind of method to order those blocks to like this with numpy:是否有某种方法可以使用 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]

And here is image to show it better what I'm looking for:这是图像以更好地展示我正在寻找的内容:

订购示例图片

I'm not sure if there is somekind of term for this kind of problem, so apologize cannot use right tems.我不确定这种问题是否有某种术语,所以抱歉不能使用正确的术语。 Problem seems to be like we need to "rotate clockwise" that ordering as you see in that blue line in example image.问题似乎是我们需要“顺时针旋转”该顺序,如示例图像中的蓝线所示。 So the question is, how to order that in numpy?所以问题是,如何在 numpy 中排序? Also if it could changed in those for loops would be nice to know.此外,如果它可以在那些 for 循环中改变,那就很高兴知道了。

Usenumpy.rot90 :使用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]]]

Even though this is not done with a numpy, still can be useful for someone.尽管这不是用 numpy 完成的,但仍然对某人有用。 I got the correct order by looping them like this.我通过像这样循环它们得到了正确的顺序。 I'm sure that this could be done better in code but it seems this works now:我确信这可以在代码中做得更好,但现在看来这可行:

# 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)])

Output before :之前的输出:

['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']

前

Output after :输出

['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']

后

And if interested more about where I use this.如果对我在哪里使用它更感兴趣。 I created this to blender add-on called RenderFarts .我为搅拌机插件创建了这个,称为RenderFarts There I need this ordering in image merge function where I render parts and need to put those to the correct order.在那里我需要在图像合并功能中进行此排序,我在其中渲染零件并需要将它们按正确的顺序排列。 Merge process not work yet, but this ordering seems to work properly.合并过程还没有工作,但这个排序似乎工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM