计数相邻的numpy区域的单元格 [英] Count cells of adjacent numpy regions

查看:91
本文介绍了计数相邻的numpy区域的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求解决以下问题.我有一个numpy数组,标记为从1到n的区域.假设这是数组:

I'm looking to solve the following problem. I have a numpy array which is labeled to regions from 1 to n. Let's say this is the array:

x = np.array([[1, 1, 1, 4], [1, 1, 2, 4], [1, 2, 2, 4], [5, 5, 3, 4]], np.int32)

array([[1, 1, 1, 4],
       [1, 1, 2, 4],
       [1, 2, 2, 4],
       [5, 5, 3, 4]])

一个区域是numpy数组中具有唯一值的组合单元格.因此,在此示例中,x具有5个区域.区域1由5个单元格组成,区域2由3个单元格组成,依此类推. 现在,我用以下代码行确定每个区域的相邻区域:

A region are the combined cells in the numpy array with a unique value. So in this example x has 5 regions; region 1 which consists of 5 cells, region 2 which consists of 3 cells etc. Now, I determine the adjacent regions of each region with the following lines of code:

n = x.max()
tmp = np.zeros((n+1, n+1), bool)

# check the vertical adjacency
a, b = x[:-1, :], x[1:, :]
tmp[a[a!=b], b[a!=b]] = True

# check the horizontal adjacency
a, b = x[:, :-1], x[:, 1:]
tmp[a[a!=b], b[a!=b]] = True

# register adjacency in both directions (up, down) and (left,right)
result = (tmp | tmp.T)

result = result.astype(int)
np.column_stack(np.nonzero(result))
resultlist = [np.flatnonzero(row) for row in result[1:]]

哪个给我一个每个区域及其相邻区域的列表:

Which gives me a list of each region with its adjacent regions:

[array([2, 4, 5], dtype=int64),
 array([1, 3, 4, 5], dtype=int64),
 array([2, 4, 5], dtype=int64),
 array([1, 2, 3], dtype=int64),
 array([1, 2, 3], dtype=int64)]

哪个效果很好.但是,我想计算每个相邻区域的像元数量并返回此输出.因此,对于区域2,在此示例中将意味着总共7个相邻区域(三个1,两个4,一个3和一个5).因此:

Which works really well. However, I would like to count the amount of cells of each adjacent region and return this output. So, for region 2, in this example would mean a total of 7 adjacent regions (three 1s, two 4s, one 3 and one 5). Therefore:

  • 2被1包围43%
  • 2被5环绕14%
  • 2被3包围了14%
  • 2被4包围29%

如何最好地调整上面的代码以包括每个相邻区域的像元数量? 非常感谢你们!

How could I best adjust the above code to include the amount of cells for each adjacent region? Many thanks guys!

推荐答案

这里是使用 numpy_indexed 包(请注意;它不是在区域上矢量化的,而是在像素上矢量化的,这在假设n_pixels >> n_regions的情况下很有用):

Here is a vectorized solution using the numpy_indexed package (note; it isn't vectorized over the regions, but it is vectorized over the pixels, which is the useful thing to do assuming n_pixels >> n_regions):

neighbors = np.concatenate([x[:, :-1].flatten(), x[:, +1:].flatten(), x[+1:, :].flatten(), x[:-1, :].flatten()])
centers   = np.concatenate([x[:, +1:].flatten(), x[:, :-1].flatten(), x[:-1, :].flatten(), x[+1:, :].flatten()])
valid = neighbors != centers

import numpy_indexed as npi
regions, neighbors_per_regions = npi.group_by(centers[valid], neighbors[valid])
for region, neighbors_per_region in zip(regions, neighbors_per_regions):
    print(region)
    unique_neighbors, neighbor_counts = npi.count(neighbors_per_region)
    print(unique_neighbors, neighbor_counts / neighbor_counts.sum() * 100)

或者对于在像素和区域上都完全矢量化的解决方案:

Or for a solution which is fully vectorized over both pixels and regions:

(neighbors, centers), counts  = npi.count((neighbors[valid], centers[valid]))
region_group = group_by(centers)
regions, neighbors_per_region = region_group.sum(counts)
fractions = counts / neighbors_per_region[region_group.inverse]
for q in zip(centers, neighbors, fractions): print(q)

这篇关于计数相邻的numpy区域的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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