圖片經過處理后圖片會變成黑白無色彩的圖像,但可以大概觀察到圖片中主體的輪廓信息,而還原后的圖片的主體對象會被保留,圖片中其他內容會變模糊,,主體對象得以突出,通過機器學習完成對圖片的信息的提取,圖片信息可以保存到本地像素查詢本或數據庫中
導入類庫
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]]] '''