1

我需要标记一个已经分类的 img。问题是,图像是非二进制的,我需要分别计算不同值的相邻补丁。

考虑以下数据集:

import numpy as np 

data = np.zeros((6,6), dtype=np.uint16)
data[2:4, 2:4] = 10
data[4, 4] = 10
data[:2, :3] = 22
data[0, 5] = 22
data

>>>
array([[22, 22, 22,  0,  0, 22],
       [22, 22, 22,  0,  0,  0],
       [0,  0,  10, 10,  0,  0],
       [0,  0,  10, 10,  0,  0],
       [0,  0,   0,  0, 10,  0],
       [0,  0,   0,  0,  0,  0]], dtype=uint16)

我想获得(使用 8 个 neigbours 结构元素)以下内容:

array([[1, 1, 1, 0, 0, 3],
       [1, 1, 1, 0, 0, 0],
       [0, 0, 2, 2, 0, 0],
       [0, 0, 2, 2, 0, 0],
       [0, 0, 0, 0, 2, 0],
       [0, 0, 0, 0, 0, 0]], dtype=uint16)

但是使用 scipy.label 函数我得到了完全不同的结果:

from scipy import ndimage as ndi

s = ndi.generate_binary_structure(2,2)

labeled_array, num_features = ndi.label(data, structure=s)
labeled_array

>>>
array([[1, 1, 1, 0, 0, 2],
       [1, 1, 1, 0, 0, 0],
       [0, 0, 1, 1, 0, 0],
       [0, 0, 1, 1, 0, 0],
       [0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 0]], dtype=int16)

那么有没有一个技巧来分离不同价值的补丁?

4

1 回答 1

1

获取唯一值列表,uv然后将每个唯一值替换为其订单号(第一个值为 0,第二个值为 1 等)

uv = np.unique(data)
res = np.select([data==i for i in uv], range(len(uv)))

例子:

import numpy as np 

data = np.zeros((6,6), dtype=np.uint16)
data[2:4, 2:4] = 10
data[4, 4] = 10
data[:2, :3] = 22
data[0, 5] = 32

结果:

array([[2, 2, 2, 0, 0, 3],
       [2, 2, 2, 0, 0, 0],
       [0, 0, 1, 1, 0, 0],
       [0, 0, 1, 1, 0, 0],
       [0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 0]])

更新我已经看到您更改了问题中的数据。在这种情况下,它不再是已分类的图像,因为如果它没有与所有其他s链接,则data[0,5]不可能。 因此,在这种情况下,我想您需要分别为数据中的每个唯一条目进行标记,如下所示:2222

import numpy as np 
from scipy import ndimage as ndi

data = np.zeros((6,6), dtype=np.uint16)
data[2:4, 2:4] = 10
data[4, 4] = 10
data[:2, :3] = 22
data[0, 5] = 22

uv = np.unique(data)
s = ndi.generate_binary_structure(2,2)
cum_num = 0
result = np.zeros_like(data)
for v in uv[1:]:
    labeled_array, num_features = ndi.label((data==v).astype(int), structure=s)
    result += np.where(labeled_array > 0, labeled_array + cum_num, 0).astype(result.dtype)
    cum_num += num_features

数据:

[[22 22 22  0  0 22]
 [22 22 22  0  0  0]
 [ 0  0 10 10  0  0]
 [ 0  0 10 10  0  0]
 [ 0  0  0  0 10  0]
 [ 0  0  0  0  0  0]]

结果:

[[2 2 2 0 0 3]
 [2 2 2 0 0 0]
 [0 0 1 1 0 0]
 [0 0 1 1 0 0]
 [0 0 0 0 1 0]
 [0 0 0 0 0 0]]
于 2021-01-08T16:33:19.677 回答