У меня есть изображение в виде массива. Размер изображения составляет 3900X3000 пикселей. Каждый пиксель имеет значения R, G, B. Я сделал кластеризацию kmeans на изображении, чтобы получить номер кластера, и превратил его в матрицу3900 строк и 3000 столбцов.После выполнения определенной операции я хотел бы заменить номера кластеров на значения центроидов кластеров и отобразить их в виде изображения.
def extractDominantColor (image, number_of_colors = 5, hasThresholding = False):
# Quick Fix Increase cluster counter to neglect the black(Read Article)
if hasThresholding == True:
number_of_colors += 1
# Taking Copy of the image
img = image.copy()
# Convert Image into RGB Colours Space
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Reshape Image
img = img.reshape((img.shape[0]*img.shape[1]), 3)
# Initiate KMeans Object
estimator = KMeans(n_clusters=number_of_colors, random_state=0)
# Fit the image
estimator.fit(img)
# Get Colour Information
#colorInformation = getColorInformation(estimator.labels_, estimator.cluster_centers_, hasThresholding)
#return colorInformation
return estimator.labels_
clust_label = extractDominantColor(skin, hasThresholding=True)
#which has the cluster label,then turned it to a matrix
cluster_matrix=clust_label.reshape((3900,3000))
#I would like to replace this with cluster centroid RGB values and render as an image
Ожидаемый вывод для рендеринга изображения