-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdemo_kmeans_image_segmentation.py
More file actions
70 lines (50 loc) · 1.72 KB
/
Copy pathdemo_kmeans_image_segmentation.py
File metadata and controls
70 lines (50 loc) · 1.72 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
## import libraries
import numpy as np
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from mpl_toolkits.mplot3d import Axes3D
from func_Kmeans import func_Kmeans
from skimage import transform
## Generate dataset
IMG = plt.imread('test_image.jpg')
IMG = np.uint8(255*transform.resize(IMG, [512, 768]))
X = IMG.reshape((-1, 3))
## User defined parameters
ndata = X.shape[0] # The number of training data
nfeature = X.shape[1] # The number of features in the data
ncls = 2 # The number of classes (clusters)
# ncls = 3 # The number of classes (clusters)
# ncls = 4 # The number of classes (clusters)
init = 'k-means++' # 'k-means++', 'random'
max_iter = 300
## Plot the ground truth
fig = plt.figure(1)
ax = fig.add_subplot(131)
plt.imshow(IMG)
ax.set_title('Ground Truth')
plt.axis('off')
## K-means in sci-kit Learn
Kmean = KMeans(n_clusters=ncls, init=init, max_iter=max_iter)
Kmean.fit(X)
centers = Kmean.cluster_centers_
clusters = Kmean.labels_
Y_pred_sci = np.uint8(centers[clusters]).reshape(IMG.shape)
# Plot the data along the classes
fig = plt.figure(1)
ax = fig.add_subplot(132)
plt.imshow(Y_pred_sci)
ax.set_title('K-means in Sci-kit (%d clusters)' % ncls)
plt.axis('off')
## K-means from scratch
clusters, centers = func_Kmeans(X, ncls, init=init, max_iter=max_iter)
Y_pred_scratch = np.uint8(centers[clusters]).reshape(IMG.shape)
# Plot the data along the classes
fig = plt.figure(1)
ax = fig.add_subplot(133)
plt.imshow(Y_pred_scratch)
ax.set_title('K-means from Scratch (%d clusters)' % ncls)
plt.axis('off')
plt.show()