将矩阵的标量值替换为向量值,并转换为imag

2024-09-30 14:28:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个numpy数组形式的图像。图像大小为3900X3000像素。每个像素都有R、G、B值。我对图像进行了kmeans聚类以获得聚类编号,并将其转换为3900行和3000列的矩阵。在完成某些操作后,我想用簇形心值替换簇编号,并将其渲染为图像

def extractDominantColor(图像,颜色数=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

预期的输出是渲染图像


Tags: oftheto图像imagetrueimg聚类