numpy 2D数组:获取所有已连接并共享相同值的条目的索引 [英] numpy 2D array: get indices of all entries that are connected and share the same value

查看:102
本文介绍了numpy 2D数组:获取所有已连接并共享相同值的条目的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维numpy数组,其中填充了从0到N的整数值,如何获取所有直接连接并共享相同值的条目的索引。

I have a 2D numpy Array filled with integer-values from 0 to N, how can i get the indices of all entries that are directly connected and share the same value.

加法:大多数条目为零,可以忽略!

Addition: Most of the entries are zero and can be ignored!

示例输入数组:

[ 0 0 0 0 0 ]
[ 1 1 0 1 1 ]
[ 0 1 0 1 1 ]
[ 1 0 0 0 0 ]
[ 2 2 2 2 2 ]

产量指数:

1: [ [1 0] [1 1] [2 1] [3 0] ] # first 1 cluster
   [ [1 3] [1 4] [2 3] [2 4] ] # second 1 cluster

2: [ [4 0] [4 1] [4 2] [4 3] [4 4] ] # only 2 cluster

输出数组的格式并不重要,我只需要在其中使用分隔值群集有可能解决单个索引

the formating of the output arrays is not important, i just need separated value clusters where it is possible to address the single indices

我首先想到的是:

N = numberClusters
x = myArray

for c in range(N):
   for i in np.where(x==c):
         # fill output array with i

填充输出数组,但这错过了具有相同簇的分离值

but this misses the separation of clusters that have the same value

推荐答案

您可以使用 skimage.measure.label (与为此安装pip install scikit-image :

import numpy as np
from skimage import measure

# Setup some data
np.random.seed(42)
img = np.random.choice([0, 1, 2], (5, 5), [0.7, 0.2, 0.1])
# [[2 0 2 2 0]
#  [0 2 1 2 2]
#  [2 2 0 2 1]
#  [0 1 1 1 1]
#  [0 0 1 1 0]]

# Label each region, considering only directly adjacent pixels connected
img_labeled = measure.label(img, connectivity=1)
# [[1 0 2 2 0]
#  [0 3 4 2 2]
#  [3 3 0 2 5]
#  [0 5 5 5 5]
#  [0 0 5 5 0]]

# Get the indices for each region, excluding zeros
idx = [np.where(img_labeled == label)
       for label in np.unique(img_labeled)
       if label]
# [(array([0]), array([0])),
#  (array([0, 0, 1, 1, 2]), array([2, 3, 3, 4, 3])),
#  (array([1, 2, 2]), array([1, 0, 1])),
#  (array([1]), array([2])),
#  (array([2, 3, 3, 3, 3, 4, 4]), array([4, 1, 2, 3, 4, 2, 3]))]

# Get the bounding boxes of each region (ignoring zeros)
bboxes = [area.bbox for area in measure.regionprops(img_labeled)]
# [(0, 0, 1, 1),
#  (0, 2, 3, 5),
#  (1, 0, 3, 2),
#  (1, 2, 2, 3),
#  (2, 1, 5, 5)]

使用非常有用的方法可以找到边界框函数 skimage.measure.regionprops ,其中包含有关该地区的大量信息。对于边界框,它返回一个元组(min_row,min_col,max_row,max_col),其中属于边界框的像素在半开区间 [min_row; max_row) [min_col; max_col)

The bounding boxes can be found using the very helpful function skimage.measure.regionprops, which contains a plethora of information on the regions. For the bounding box it returns a tuple of (min_row, min_col, max_row, max_col), where pixels belonging to the bounding box are in the half-open interval [min_row; max_row) and [min_col; max_col).

这篇关于numpy 2D数组:获取所有已连接并共享相同值的条目的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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