Если два центра расположены настолько близко, что соответствующие кластеры перекрываются, я бы например, массивы меток, чтобы отдать приоритет нижнему значению метки. Другими словами, если кластер 2 перекрывает кластер 1, то я хочу, чтобы размер кластера 1 был 5x5, а кластер 2 был меньше.
Мне удалось закодировать эту процедуру следующим образом:
Мне удалось закодировать эту процедуру следующим образом: p>
Код: Выделить всё
import numpy as np
labels = np.zeros((10,20))
centers = [(4,4), (7,7), (5,10), (5,17)]
for i, center in enumerate(centers, start=1):
# find the coordinates for the 5x5 cluster centered in center.
cluster = np.ix_(np.arange(center[0]-2, center[0]+3,1),
np.arange(center[1]-2, center[1]+3,1))
# create a temporary label array with all zeros
temp_label = np.zeros_like(labels)
# set the label value in the temporary array in the position corresponding to the cluster
temp_label[cluster] = i
# apply some boolean algebra
# (labels == 0) is a bool array corresponding to all positions that are not yet belonging to a label
# (labels == 0) * temp_label is like the temp_label, but only where the labels array is still free
# adding the labels back is ensuring that all previous labels are also counted.
labels = (labels == 0) * temp_label + labels
print(labels)
Код: Выделить всё
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 1. 1. 0. 3. 3. 3. 3. 3. 0. 0. 4. 4. 4. 4. 4.]
[0. 0. 1. 1. 1. 1. 1. 0. 3. 3. 3. 3. 3. 0. 0. 4. 4. 4. 4. 4.]
[0. 0. 1. 1. 1. 1. 1. 2. 2. 2. 3. 3. 3. 0. 0. 4. 4. 4. 4. 4.]
[0. 0. 1. 1. 1. 1. 1. 2. 2. 2. 3. 3. 3. 0. 0. 4. 4. 4. 4. 4.]
[0. 0. 0. 0. 0. 2. 2. 2. 2. 2. 3. 3. 3. 0. 0. 4. 4. 4. 4. 4.]
[0. 0. 0. 0. 0. 2. 2. 2. 2. 2. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 2. 2. 2. 2. 2. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
Какое решение было бы лучшим?
Подробнее здесь: https://stackoverflow.com/questions/790 ... umpy-array