-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmeans_coins.py
More file actions
25 lines (23 loc) · 845 Bytes
/
Copy pathkmeans_coins.py
File metadata and controls
25 lines (23 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from PIL import Image, ImageOps
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
with Image.open('../Data/coins.jpg') as img:
border = (0, 0, 0, 20)
crop_image = ImageOps.crop(img, border)
gray_image = ImageOps.grayscale(crop_image)
arr_img = np.array(gray_image)
flatten_image = np.ndarray.flatten(arr_img, order='C')
flatten_image = np.expand_dims(flatten_image, axis=1)
ax = plt.subplot(2, 2, 1)
ax.imshow(crop_image)
ax.set_title('Original Coin')
for k in range(2, 4 + 1, 1):
model = KMeans(n_clusters=k)
model.fit(flatten_image)
res = model.labels_
img2 = np.reshape(res, newshape=arr_img.shape)
ax = plt.subplot(2, 2, k)
ax.imshow(img2)
ax.set_title(f'{k} Cluster')
plt.show()