Я запускаю кластеризацию kmeans на изображении, которое я надеюсь классифицировать. Когда я запускаю программу, я получаю тот же результат, ожидая, что мои цвета не соответствуют друг другу, что означает, что kmeans не повторяет точно такой же процесс. Как я могу поддерживать одинаковые классы при каждом запуске программы?
Вот два примера. Первое изображение в наборе - это результат кластера kmeans, а второе - карта классификации на изображении.
SET 1
![enter image description here](https://i.stack.imgur.com/jJSXj.png)
Комплект 2
![enter image description here](https://i.stack.imgur.com/HJL9d.png)
Код:
#Set a 6 KMeans clustering
kmeans = KMeans(n_clusters = 4, n_jobs = -2)
#Compute cluster centers and predict cluster indices
X_clustered = kmeans.fit_predict(x_3d)
# Display scatter of kmeans
plt.scatter(X[:, 0], X[:, 1], c=X_clustered, s=5, cmap='viridis')
plt.show()
# Create a temp dataframe from our PCA projection data "x_3d"
df = pd.DataFrame(x_3d)
df['X_cluster'] = X_clustered
#create an greyscale image and remap the color pixel based on the df file given
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
tempImage = img
row,col = img.shape[:2]
count = 0
for i in range(row):
for j in range(col):
if X_clustered[count]==0:
tempImage[i,j] = (255,255,255,1)
elif X_clustered[count]==1:
tempImage[i,j] = (0,255,0,1)
elif X_clustered[count]==2:
tempImage[i,j] = (0,0,255,1)
elif X_clustered[count]==3:
tempImage[i,j] = (125,125,0,1)
elif X_clustered[count]==4:
tempImage[i,j] = (0,125,125,1)
elif X_clustered[count]==5:
tempImage[i,j] = (125,0,125,1)
elif X_clustered[count]==6:
tempImage[i,j] = (255,255,0,1)
elif X_clustered[count]==7:
tempImage[i,j] = (255,0,255,1)
elif X_clustered[count]==8:
tempImage[i,j] = (0,255,255,1)
elif X_clustered[count]==9:
tempImage[i,j] = (125,125,125)
count+= 1
return tempImage