Если два центра расположены настолько близко, что соответствующие кластеры перекрываются, тогда я хотелось бы, чтобы массивы меток отдавали приоритет нижнему значению метки. Другими словами, если кластер 2 перекрывает кластер 1, то я хочу, чтобы размер кластера 1 был 5x5, а кластер 2 был меньше.
Мне удалось закодировать эту процедуру следующим образом:
Мне удалось закодировать эту процедуру следующим образом: p>
Код: Выделить всё
import numpy as np
labels = np.zeros((10,20))
n_rows, n_cols = labels.shape
centers = [(4,4), (7,7), (5,10), (5,18)]
for i, center in enumerate(centers, start=1):
# find the coordinates for the 5x5 cluster centered in center.
cluster = np.ix_(np.arange(max(center[0]-2,0), min(center[0]+3,n_rows),1),
np.arange(max(center[1]-2, 0), min(center[1]+3,n_cols),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. 0. 4. 4. 4. 4.]
[0. 0. 1. 1. 1. 1. 1. 0. 3. 3. 3. 3. 3. 0. 0. 0. 4. 4. 4. 4.]
[0. 0. 1. 1. 1. 1. 1. 2. 2. 2. 3. 3. 3. 0. 0. 0. 4. 4. 4. 4.]
[0. 0. 1. 1. 1. 1. 1. 2. 2. 2. 3. 3. 3. 0. 0. 0. 4. 4. 4. 4.]
[0. 0. 0. 0. 0. 2. 2. 2. 2. 2. 3. 3. 3. 0. 0. 0. 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