机器学习——图像训练

图片经过处理后图片会变成黑白无色彩的图像,但可以大概观察到图片中主体的轮廓信息,而还原后的图片的主体对象会被保留,图片中其他内容会变模糊,,主体对象得以突出,通过机器学习完成对图片的信息的提取,图片信息可以保存到本地像素查询本或数据库中

导入类库

1 import numpy as np
2 import cv2
3 import matplotlib.pyplot as plt
4 from sklearn.cluster import KMeans
5 from sklearn.utils import shuffle
6 from time import time
7 from skimage import io

提取和存储图像数据

复制代码
 1 n_colors = 64
 2 # 读取图片像素数据
 3 tiger = cv2.imread("tiger.jpg")
 4 # print('tiger >>>>',tiger)
 5 login = cv2.imread('login.png')
 6 # 将图片像素数据标准化为0-1的数据并保存至数组中
 7 china = np.array(tiger, dtype=np.float64) / 255
 8 # print('china >>>>',china)
 9 w, h, d = original_shape = tuple(china.shape)
10 print('original_shape >>>>', original_shape)
11 print('w,h,d >>>>', w, h, d)  # w:层数,h行数,d列数
12 
13 image_array = np.reshape(china, (w * h, d))
14 # 每个点作为一个样本,维数为3,将三维数组china化为二维数组,列数不变,行数变为原行数乘层数
15 # print('image_array >>>>', image_array)
16 print('image_array shape>>>>', image_array.shape)
复制代码

训练图像数据

复制代码
1 t0 = time()
2 # 将所有点打乱顺序,取前1000个点
3 # 不使用所有点主要是为了训练模型的速度
4 image_array_sample = shuffle(image_array, random_state=0)[:1000]
5 
6 # 训练图片像素数据,将像素数据分为64类
7 # random_state = 0用来重现同样的结果,不设置则每次都是不同的随机结果
8 kmeans = KMeans(n_clusters=n_colors, random_state=0).fit(image_array_sample)
9 print("done in %0.3fs." % (time() - t0))  # 查看训练时间
复制代码

预测

复制代码
 1 print("Predicting color indices on the full image (k-means)")
 2 t0 = time()
 3 # 预测数据分类,image_array(921600,3)二维数组预测完毕后的结果labels是一维数组
 4 labels = kmeans.predict(image_array)
 5 print("done in %0.3fs." % (time() - t0))  # 查看预测时间
 6 print(labels)
 7 print(labels.shape)
 8 # 将labels从一维数组化为二维数组
 9 labels = labels.reshape(w, h)
10 print(labels)
11 print(labels.shape)
复制代码

保存像素查询本和处理后的图像

1 def save_compress_data():
2     np.save('codebook_tiger.npy', kmeans.cluster_centers_)
3     io.imsave('compressed_tiger.png', labels)

还原图像

复制代码
 1 def recreate_image(codebook, labels, w, h):
 2     # Recreate the (compressed) image from the code book & labels
 3     # 每个像素查询码本(对应0~63),取得其对应的像素值
 4     d = codebook.shape[1]
 5     image = np.zeros((w, h, d))
 6     label_idx = 0
 7     for i in range(w):
 8         for j in range(h):
 9             image[i][j] = codebook[labels[i, j]]
10             label_idx += 1
11     print('还原出的图像 >>>>', image)
12     return image
复制代码

执行代码

复制代码
 1 # save_compress_data()
 2 centers = np.load('codebook_tiger.npy')  # 像素查询码本
 3 c_image = io.imread('compressed_tiger.png')  # 这张图片里的像素已经过分类
 4 print('像素查询本 >>>>', centers)
 5 print(centers.shape)
 6 print(centers.shape[1])  # 0是行数,1是列数
 7 print('压缩的图像 >>>>', c_image)
 8 print(c_image.shape)
 9 
10 cv2.imshow("new", recreate_image(centers, c_image, w, h))
11 cv2.waitKey(0)
复制代码
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'''
取出压缩后图像的每一个数据即像素分类id(labels[i, j]),在像素查询本中查找该分类对应的三位像素一行数据(codebook[labels[i, j]]),
赋予新的image对象(无需指定列数,三位像素即3列)
original_shape >>>> (720, 1280, 3)
w,h,d >>>> 720 1280 3
image_array shape>>>> (921600, 3)
done in 0.583s.
Predicting color indices on the full image (k-means)
done in 0.740s.
[10 10 10 ... 60 60 60]
(921600,)
[[10 10 10 ... 42  8 42]
 [10 10 10 ... 42 42  8]
 [10 10 10 ... 42 42  8]
 ...
 [ 3  3 36 ... 60 60 60]
 [ 3  3 36 ... 60 60 60]
 [ 3  3 36 ... 60 60 60]]
(720, 1280)
像素查询本 >>>> [[0.26901961 0.27607843 0.35686275]
 [0.67189542 0.66887883 0.66747109]
 [0.09971989 0.09271709 0.10028011]
 [0.38901961 0.38184874 0.39383754]
 [0.83529412 0.8285205  0.83333333]
 [0.4402852  0.57468806 0.71764706]
 [0.46849673 0.49947712 0.47712418]
 [0.19622926 0.26651584 0.24494721]
 [0.30539216 0.45       0.60441176]
 [0.14444444 0.50359477 0.22581699]
 [0.64325609 0.63850267 0.62923351]
 [0.75148874 0.74524328 0.7405955 ]
 [0.18221289 0.18585434 0.20770308]
 [0.55294118 0.69233512 0.8631016 ]
 [0.09656863 0.28039216 0.43823529]
 [0.59155354 0.58924082 0.57797888]
 [0.53202614 0.38039216 0.7124183 ]
 [0.90756303 0.90364146 0.90140056]
 [0.3713555  0.46683717 0.54339301]
 [0.31215686 0.54352941 0.43372549]
 [0.23504902 0.32254902 0.30563725]
 [0.29694989 0.37342048 0.42461874]
 [0.19117647 0.32622549 0.4495098 ]
 [0.18431373 0.55757576 0.30516934]
 [0.70718954 0.76601307 0.83529412]
 [0.51328976 0.50544662 0.51568627]
 [0.36705882 0.23921569 0.61098039]
 [0.44416027 0.42983802 0.43631714]
 [0.10053476 0.16363636 0.23600713]
 [0.15022624 0.14434389 0.15806938]
 [0.40452489 0.50497738 0.59457014]
 [0.51265597 0.63030303 0.78324421]
 [0.29215686 0.30392157 0.30056022]
 [0.06876751 0.10294118 0.15644258]
 [0.0455243  0.05268542 0.06479113]
 [0.95294118 0.95294118 0.95803922]
 [0.34232026 0.35065359 0.33970588]
 [0.56254902 0.55431373 0.54196078]
 [0.2745098  0.49063181 0.27973856]
 [0.78676471 0.7814951  0.78112745]
 [0.21411765 0.20627451 0.43176471]
 [0.34196078 0.55843137 0.35803922]
 [0.36470588 0.5027451  0.66352941]
 [0.47189542 0.67973856 0.56078431]
 [0.49084967 0.55294118 0.63300654]
 [0.10889894 0.20301659 0.32488688]
 [0.65228758 0.78823529 0.92222222]
 [0.3372549  0.53411765 0.7427451 ]
 [0.71036415 0.70672269 0.69677871]
 [0.17019608 0.26431373 0.3696732 ]
 [0.39063181 0.44814815 0.40217865]
 [0.57303922 0.34656863 0.81617647]
 [0.28039216 0.37385621 0.49477124]
 [0.33440285 0.42816399 0.49411765]
 [0.43137255 0.61019608 0.81882353]
 [0.25743945 0.24705882 0.24313725]
 [0.27088989 0.34901961 0.36651584]
 [0.52990196 0.59215686 0.54166667]
 [0.39607843 0.55196078 0.51470588]
 [0.87511312 0.87179487 0.87722474]
 [0.41137255 0.46196078 0.46745098]
 [0.23627451 0.26470588 0.29362745]
 [0.1254902  0.44248366 0.18431373]
 [0.61265597 0.6197861  0.60463458]]
(64, 3)
3
压缩的图像 >>>> [[10 10 10 ... 42  8 42]
 [10 10 10 ... 42 42  8]
 [10 10 10 ... 42 42  8]
 ...
 [ 3  3 36 ... 60 60 60]
 [ 3  3 36 ... 60 60 60]
 [ 3  3 36 ... 60 60 60]]
(720, 1280)
还原出的图像 >>>> [[[0.64325609 0.63850267 0.62923351]
  [0.64325609 0.63850267 0.62923351]
  [0.64325609 0.63850267 0.62923351]
  ...
  [0.36470588 0.5027451  0.66352941]
  [0.30539216 0.45       0.60441176]
  [0.36470588 0.5027451  0.66352941]]
 
 [[0.64325609 0.63850267 0.62923351]
  [0.64325609 0.63850267 0.62923351]
  [0.64325609 0.63850267 0.62923351]
  ...
  [0.36470588 0.5027451  0.66352941]
  [0.36470588 0.5027451  0.66352941]
  [0.30539216 0.45       0.60441176]]
 
 [[0.64325609 0.63850267 0.62923351]
  [0.64325609 0.63850267 0.62923351]
  [0.64325609 0.63850267 0.62923351]
  ...
  [0.36470588 0.5027451  0.66352941]
  [0.36470588 0.5027451  0.66352941]
  [0.30539216 0.45       0.60441176]]
 
 ...
 
 [[0.38901961 0.38184874 0.39383754]
  [0.38901961 0.38184874 0.39383754]
  [0.34232026 0.35065359 0.33970588]
  ...
  [0.41137255 0.46196078 0.46745098]
  [0.41137255 0.46196078 0.46745098]
  [0.41137255 0.46196078 0.46745098]]
 
 [[0.38901961 0.38184874 0.39383754]
  [0.38901961 0.38184874 0.39383754]
  [0.34232026 0.35065359 0.33970588]
  ...
  [0.41137255 0.46196078 0.46745098]
  [0.41137255 0.46196078 0.46745098]
  [0.41137255 0.46196078 0.46745098]]
 
 [[0.38901961 0.38184874 0.39383754]
  [0.38901961 0.38184874 0.39383754]
  [0.34232026 0.35065359 0.33970588]
  ...
  [0.41137255 0.46196078 0.46745098]
  [0.41137255 0.46196078 0.46745098]
  [0.41137255 0.46196078 0.46745098]]]
 
'''

  

posted @   BO00097  阅读(2842)  评论(0编辑  收藏  举报
编辑推荐:
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 程序员常用高效实用工具推荐,办公效率提升利器!
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
点击右上角即可分享
微信分享提示