-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path11.3.py
38 lines (33 loc) · 1 KB
/
11.3.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
37
38
# 图像的聚类分割
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import PIL.Image as Image
# 打开图像并显示
# %matplotlib inline
img = Image.open('./chapter5/lena.tiff')
plt.axis('off')
plt.imshow(img)
# 显示图像的基本信息和图像大小
print(img.info)
row,col = img.size
print('图像的大小:', row, col)
# 显示图像的颜色模式
print('数据类型:',type(img))
print('图像的颜色模式:',img.mode)
# 对图像数据进行聚类并显示每个像素的簇标号
imgData = np.array(img.getdata())
type(imgData)
pixel_vals = imgData.reshape(-1,4)
# pixel_vals
km_cluster = KMeans(n_clusters=3)
label = km_cluster.fit_predict(pixel_vals)
print('每个像素的簇标号:\n', label)
# 显示分割后的图像
label = label.reshape([row, col]).T
pic_new = Image.new('L', (row, col))
for i in range(row):
for j in range(col):
pic_new.putpixel((i, j), int(int(256/(label[i][j] + 1))))
plt.imshow(pic_new)
plt.waitforbuttonpress(99)