Numpy,计算2d数组中的唯一邻居

时间:2018-01-14 10:45:00

标签: python arrays python-3.x numpy

我试图计算2d numpy数组中与元素本身不同的每个元素的邻居数(在这种情况下为4-neighborhood,但8-neighborhood也很有趣)。

这样的事情:

input labels:
 [[1 1 1 2 2 2 2]
 [1 1 1 2 2 2 2]
 [1 1 1 2 2 2 2]
 [1 1 3 3 3 5 5]
 [4 4 4 3 3 5 5]
 [4 4 4 3 3 5 5]] (6, 7)

count of unique neighbour labels:
 [[0 0 1 1 0 0 0]
 [0 0 1 1 0 0 0]
 [0 0 2 2 1 1 1]
 [1 2 2 1 2 2 1]
 [1 1 1 1 1 1 0]
 [0 0 1 1 1 1 0]] (6, 7)

我有下面的代码,出于好奇,我想知道是否有更好的方法来实现这一点,也许没有for循环?

import numpy as np
import cv2


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

print('input labels:\n', labels_image, labels_image.shape)

# Make a border, otherwise neighbours are counted as wrapped values from the other side
labels_image = cv2.copyMakeBorder(labels_image, 1, 1, 1, 1, cv2.BORDER_REPLICATE)

offsets = [(-1, 0), (0, -1), (0, 1), (1, 0)] # 4 neighbourhood

# Stack labels_image with one shifted per offset so we get a 3d array
# where each z-value corresponds to one of the neighbours
stacked = np.dstack(np.roll(np.roll(labels_image, i, axis=0), j, axis=1) for i, j in offsets)

# count number of unique neighbours, also take the border away again
labels_image = np.array([[(len(np.unique(stacked[i,j])) - 1)
                        for j in range(1, labels_image.shape[1] - 1)]
                        for i in range(1, labels_image.shape[0] - 1)])

print('count of unique neighbour labels:\n', labels_image, labels_image.shape)

我尝试将np.unique与return_countsaxis参数一起使用,但无法使其工作。

1 个答案:

答案 0 :(得分:1)

这是一种方法 -

import itertools

def count_nunique_neighbors(ar):
    a = np.pad(ar, (1,1), mode='reflect')
    c = a[1:-1,1:-1]

    top = a[:-2,1:-1]
    bottom = a[2:,1:-1]
    left = a[1:-1,:-2] 
    right = a[1:-1,2:]

    ineq = [top!= c,bottom!= c, left!= c, right!= c]
    count = ineq[0].astype(int) + ineq[1] + ineq[2] + ineq[3] 

    blck = [top, bottom, left, right]
    for i,j in list(itertools.combinations(range(4), r=2)):
        count -= ((blck[i] == blck[j]) & ineq[j])
    return count

示例运行 -

In [22]: a
Out[22]: 
array([[1, 1, 1, 2, 2, 2, 2],
       [1, 1, 1, 2, 2, 2, 2],
       [1, 1, 1, 2, 2, 2, 2],
       [1, 1, 3, 3, 3, 5, 5],
       [4, 4, 4, 3, 3, 5, 5],
       [4, 4, 4, 3, 3, 5, 5]])

In [23]: count_nunique_neighbors(a)
Out[23]: 
array([[0, 0, 1, 1, 0, 0, 0],
       [0, 0, 1, 1, 0, 0, 0],
       [0, 0, 2, 2, 1, 1, 1],
       [1, 2, 2, 1, 2, 2, 1],
       [1, 1, 1, 1, 1, 1, 0],
       [0, 0, 1, 1, 1, 1, 0]])