-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk-means.py
36 lines (29 loc) · 843 Bytes
/
k-means.py
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
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import cv2
import numpy as np
from utils import *
img = 'Images/NL.jpg'
clusters = 6
# load the image and convert it from BGR to RGB so that
# we can dispaly it with matplotlib
image = cv2.imread(img)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# show our image
plt.figure()
plt.axis("off")
plt.imshow(image)
# reshape the image to be a list of pixels
image = image.reshape((image.shape[0] * image.shape[1], 3))
# cluster the pixel intensities
clt = KMeans(n_clusters = clusters)
clt.fit(image)
# build a histogram of clusters and then create a figure
# representing the number of pixels labeled to each color
hist = centroid_histogram(clt)
bar = plot_colors(hist, clt.cluster_centers_)
# show our color bart
plt.figure()
plt.axis("off")
plt.imshow(bar)
plt.show()