机器学习——图像训练
图片经过处理后图片会变成黑白无色彩的图像,但可以大概观察到图片中主体的轮廓信息,而还原后的图片的主体对象会被保留,图片中其他内容会变模糊,,主体对象得以突出,通过机器学习完成对图片的信息的提取,图片信息可以保存到本地像素查询本或数据库中
导入类库
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)
''' 取出压缩后图像的每一个数据即像素分类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]]] '''