Python 基于邻居值查找元素
我想知道是否有一种有效的方法来查找Numpy数组中特定值旁边的元素索引 如何在数组a中找到所有等于1且紧挨着0的元素的索引?没有循环检查每个元素的8个包围元素的值Python 基于邻居值查找元素,python,arrays,numpy,edge-detection,Python,Arrays,Numpy,Edge Detection,我想知道是否有一种有效的方法来查找Numpy数组中特定值旁边的元素索引 如何在数组a中找到所有等于1且紧挨着0的元素的索引?没有循环检查每个元素的8个包围元素的值 A = [[ 0., 0., 0., 0., 0., 0.], [ 0., 1., 1., 1., 1., 0.], [ 0., 1., 1., 1., 1., 0.], [ 0., 1., 1., 1., 1.,
A = [[ 0., 0., 0., 0., 0., 0.],
[ 0., 1., 1., 1., 1., 0.],
[ 0., 1., 1., 1., 1., 0.],
[ 0., 1., 1., 1., 1., 0.],
[ 0., 1., 1., 1., 1., 0.],
[ 0., 0., 0., 0., 0., 0.]]
我希望从这样的结果中获得索引:
[[ False, False, False, False, False, False],
[ False, True, True, True, True, False],
[ False, True, False, False, True, False],
[ False, True, False, False, True, False],
[ False, True, True, True, True, False],
[ False, False, False, False, False, False]]
我使用了canny边缘检测,但它并不总是适用于在北/西南或北/东南邻居上只有一个0的元素。例如:
B = [[ 1., 0., 0.],
[ 1., 0., 0.],
[ 1., 1., 1.]]
可能导致
[[ True, False, False],
[ True, False, False],
[ False, True, True]]
而不是
[[ True, False, False],
[ True, False, False],
[ True, True, True]]
谢谢
更新1:我尝试了第一个canny边缘检测表单scikit.image,但它忽略了元素。然后我尝试了np.gradient,得到了相同的结果
更新2:示例:
B=np.array([[1,1,1,0,0,0,0],
[1,1,1,0,0,0,0],
[1,1,1,1,0,0,0],
[1,1,1,1,0,0,0],
[1,1,1,1,1,0,0],
[1,1,1,1,1,0,0],
[1,1,1,0,0,0,0],
[1,1,1,1,0,0,0]])
在本例中,canny边缘检测、梯度法和ndimage.laplace(下面的答案中提到的方法)都会导致相同的结果,缺少元素(图中的黄色)
更新2:
下面是循环方法
def check_8neigh_Value(arr,eltvalue, neighvalue):
"checking if the element of value=eltvalue is surrounded
by the value=neighvalue and returning the corresponding grid"
l, c = np.shape(arr)
contour = np.zeros(np.shape(arr))
for i in np.arange(1,l-1):
for j in np.arange(1,c-1):
window = arr[i-1:i+2, j-1:j+2]
if np.logical_and(arr[i,j]==eltvalue,neighvalue in window):
contour[i,j]=1
return contour
image=check_8neigh_Value(B,1,0)
它为我提供了我所需要的,但是它在大型阵列上效率不高
由于我不知道如何使用结果,因此我一直坚持采用“步履蹒跚”的方法:
对于使用数组B的3乘3窗口,我能够获得所需的B,但无法获得更多
window_h=3
window_w=3
l, c = image.shape
l_new, c_new = l - window_h + 1, c - window_w + 1
shape=[c_new, l_new, window_w, window_h]
strides=B.strides + B.strides
strided_image = np.lib.stride_tricks.as_strided(B,shape=shape,strides=strides)
这里有一种使用二进制腐蚀的方法:
import numpy as np
from scipy import ndimage
eroded = ndimage.binary_erosion(A, np.eye(3))
diff = (A - eroded).astype(np.bool)
print(repr(diff))
# array([[False, False, False, False, False, False],
# [False, True, True, True, True, False],
# [False, True, False, False, True, False],
# [False, True, False, False, True, False],
# [False, True, True, True, True, False],
# [False, False, False, False, False, False]], dtype=bool)
您还可以获取输入数组的拉普拉斯算子,并找到负的位置:
lap = ndimage.laplace(A)
print(repr(lap < 0))
# array([[False, False, False, False, False, False],
# [False, True, True, True, True, False],
# [False, True, False, False, True, False],
# [False, True, False, False, True, False],
# [False, True, True, True, True, False],
# [False, False, False, False, False, False]], dtype=bool)
lap=ndimage.laplace(A)
打印(重复(圈数<0))
#数组([[False,False,False,False,False,False],
#[假,真,真,真,真,假],
#[假,真,假,假,真,假],
#[假,真,假,假,真,假],
#[假,真,真,真,真,假],
#[False,False,False,False,False]],dtype=bool)
您是否尝试过任何方法,或者只是在寻找答案?请查看此网站,以了解代码中使用的“滑动”或“移动”或“滚动”窗口功能。这是一个记录相对完整的领域。在你的情况下,八个邻居的总和将是一个很好的开始感谢信息南,有一个链接到你提到的网站吗?聪明的想法与侵蚀!对于一些性能改进,我想我们可以坚持使用布尔数组并执行类似的操作:A.astype(bool)&~discreated
。感谢各位的回答,但不幸的是,这种方法没有提供我想要的。我用一个新的例子更新了上面的问题