我有一个图像作为一个numpy数组。图像的大小是3900X3000像素。每个像素都有R,G,B值。我对图像进行了kmeans聚类以获得聚类数并将其转换为一个矩阵3900行和3000列。完成某些操作后,我想用群集质心值替换群集编号,并将其呈现为图像。
def extractDominantColor(图像,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
预期的输出是渲染图像