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 0 0 0 6 6 0 0 6 6]
 [0 1 1 0 0 0 0 0 0 7 0 0 0 3 3 0 0 0 4 4 0 0 0 0 5 5 5 5 5 0 2 2 0 2 2 2 0 0 0 2 2 2 0 6 6 0 0 0 6 6 0 0 6 6]]

The following is a connected components list of lists (50 x 10), let's call it labels , what i want is to create a dictionary where the numbers except 0 are a key of that dictionary, and the value correspondent to that key should be the coordinates for example [xmin,xmax,ymin,ymax] , 以下是连接组件列表(50 x 10),我们称之为labels ,我想要的是创建一个字典,其中0以外的数字是该字典的键,并且与该键对应的值应该是坐标例如[xmin,xmax,ymin,ymax]

Let's take 1 for example: 我们以1为例:

{'1': [0,10,1,9], '2': [......} 

Being new to python, I found it hard to write a program to do that from scratch (not the dictionary creation and filling but finding the coordinates). 作为python的新手,我发现很难编写一个程序来从头开始(不是字典创建和填充,而是找到坐标)。 Is there a way inside numpy that can do that for example np.unique(labels) would help me identify unique labels values. 有没有一种方法可以做到这一点,例如np.unique(labels)可以帮助我识别唯一的标签值。

You need to use np.where . 你需要使用np.where It returns two arrays: 它返回两个数组:
- the first contains row indeces y - 第一个包含行indeces y
- the second contains column indeces x . - 第二个包含列indeces x

import nupmy as np

arr = np.array(labels)

stats = dict()
for i in np.unique(arr):
    if i == 0:
        continue
    else:
        wh = np.where(arr == i)
        stats[i] = [wh[1].min(), wh[1].max(), wh[0].min(), wh[0].max()]

> {1: [0, 10, 1, 9],
   2: [29, 41, 2, 9],
   3: [13, 14, 4, 9],
   4: [18, 19, 4, 9],
   5: [21, 28, 4, 9],
   6: [43, 53, 4, 9],
   7: [9, 9, 9, 9]}

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

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