相邻小区的在一个numpy的阵列计数 [英] Counting of adjacent cells in a numpy array

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

问题描述

过了午夜,也许有人有一个想法如何解决我的问题。我想算相邻细胞的数量(这意味着与其他值如阵列领域零的数组值附近的数。)作为总和的每一个有效的价值!

例如:

 进口numpy的,SciPy的
S = ndimage.generate_binary_structure(2,2)#结构可以变化
A = numpy.zeros((6,6),DTYPE = numpy.int)#示例阵列
一个[2:4,2:4] = 1; [2,4] = 1#与示例值结构
打印一
> [0 0 0 0 0 0]
  [0 0 0 0 0 0]
  [0 0 1 1 1 0]
  [0 0 1 1 0 0]
  [0 0 0 0 0 0]
  [0 0 0 0 0 0]]
#位的值[2,4]是由6个零包围,而一个在
#位置[2,2]在附近的5个零,如果'S'是假设二元结构。
因此,围绕零的#总和总和(5 + 4 + 6 + 4 + 5)= = 24

我怎么能算零的数量这样的方式,如果我的价值观的结构发生变化?
不知何故,我相信一定要把使用 binary_dilation SciPy的功能,它能够放大价值结构,但简单重叠计数不能带我到正确的总和或不是吗?

 打印ndimage.binary_dilation(A,S).astype(a.dtype)
[[0 0 0 0 0 0]
 [0 1 1 1 1 1]
 [0 1 1 1 1 1]
 [0 1 1 1 1 1]
 [0 1 1 1 1 0]
 [0 0 0 0 0 0]]


解决方案

使用的回旋的计数邻居:

 进口numpy的
进口scipy.signalA = numpy.zeros((6,6),DTYPE = numpy.int)#示例阵列
一个[2:4,2:4] = 1; [2,4] = 1#与示例值结构B = 1-一个
C = scipy.signal.convolve2d(B,numpy.ones((3,3)),MODE ='同')打印numpy.sum(C * A)

B = 1-A 让我们来算每个零而忽略了人。

我们卷积用的3×3全1的内核,其中设置每个元素到它和它的8个相邻的值的总和(其他内核是可能的,如 + 内核只有垂直相邻值)。有了这些汇总值,我们屏蔽掉了零的原始输入(因为我们不关心他们的邻居),总结整个数组。

Past midnight and maybe someone has an idea how to tackle a problem of mine. I want to count the number of adjacent cells (which means the number of array fields with other values eg. zeroes in the vicinity of array values) as sum for each valid value!.

Example:

import numpy, scipy
s = ndimage.generate_binary_structure(2,2) # Structure can vary
a = numpy.zeros((6,6), dtype=numpy.int) # Example array
a[2:4, 2:4] = 1;a[2,4] = 1 # with example value structure
print a 
>[[0 0 0 0 0 0]
  [0 0 0 0 0 0]
  [0 0 1 1 1 0]
  [0 0 1 1 0 0]
  [0 0 0 0 0 0]
  [0 0 0 0 0 0]]
# The value at position [2,4] is surrounded by 6 zeros, while the one at
# position [2,2] has 5 zeros in the vicinity if 's' is the assumed binary structure. 
# Total sum of surrounding zeroes is therefore sum(5+4+6+4+5) == 24

How can i count the number of zeroes in such way if the structure of my values vary? I somehow believe to must take use of the binary_dilation function of SciPy, which is able to enlarge the value structure, but simple counting of overlaps can't lead me to the correct sum or does it?

print ndimage.binary_dilation(a,s).astype(a.dtype)
[[0 0 0 0 0 0]
 [0 1 1 1 1 1]
 [0 1 1 1 1 1]
 [0 1 1 1 1 1]
 [0 1 1 1 1 0]
 [0 0 0 0 0 0]]

解决方案

Use a convolution to count neighbours:

import numpy
import scipy.signal

a = numpy.zeros((6,6), dtype=numpy.int) # Example array
a[2:4, 2:4] = 1;a[2,4] = 1 # with example value structure

b = 1-a
c = scipy.signal.convolve2d(b, numpy.ones((3,3)), mode='same')

print numpy.sum(c * a)

b = 1-a allows us to count each zero while ignoring the ones.

We convolve with a 3x3 all-ones kernel, which sets each element to the sum of it and its 8 neighbouring values (other kernels are possible, such as the + kernel for only orthogonally adjacent values). With these summed values, we mask off the zeros in the original input (since we don't care about their neighbours), and sum over the whole array.

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

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