機器學習——影象訓練
圖片經過處理後圖片會變成黑白無色彩的影象,但可以大概觀察到圖片中主體的輪廓資訊,而還原後的圖片的主體物件會被保留,圖片中其他內容會變模糊,,主體物件得以突出,通過機器學習完成對圖片的資訊的提取,圖片資訊可以儲存到本地畫素查詢本或資料庫中
匯入類庫
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(): 2np.save('codebook_tiger.npy', kmeans.cluster_centers_) 3io.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),取得其對應的畫素值 4d = codebook.shape[1] 5image = np.zeros((w, h, d)) 6label_idx = 0 7for i in range(w): 8for j in range(h): 9image[i][j] = codebook[labels[i, j]] 10label_idx += 1 11print('還原出的影象 >>>>', image) 12return 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 ... 428 42] [10 10 10 ... 42 428] [10 10 10 ... 42 428] ... [ 33 36 ... 60 60 60] [ 33 36 ... 60 60 60] [ 33 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.82852050.83333333] [0.44028520.57468806 0.71764706] [0.46849673 0.49947712 0.47712418] [0.19622926 0.26651584 0.24494721] [0.30539216 0.450.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.37135550.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.04552430.05268542 0.06479113] [0.95294118 0.95294118 0.95803922] [0.34232026 0.35065359 0.33970588] [0.56254902 0.55431373 0.54196078] [0.27450980.49063181 0.27973856] [0.78676471 0.78149510.78112745] [0.21411765 0.20627451 0.43176471] [0.34196078 0.55843137 0.35803922] [0.36470588 0.50274510.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.33725490.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.12549020.44248366 0.18431373] [0.61265597 0.61978610.60463458]] (64, 3) 3 壓縮的影象 >>>> [[10 10 10 ... 428 42] [10 10 10 ... 42 428] [10 10 10 ... 42 428] ... [ 33 36 ... 60 60 60] [ 33 36 ... 60 60 60] [ 33 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.50274510.66352941] [0.30539216 0.450.60441176] [0.36470588 0.50274510.66352941]] [[0.64325609 0.63850267 0.62923351] [0.64325609 0.63850267 0.62923351] [0.64325609 0.63850267 0.62923351] ... [0.36470588 0.50274510.66352941] [0.36470588 0.50274510.66352941] [0.30539216 0.450.60441176]] [[0.64325609 0.63850267 0.62923351] [0.64325609 0.63850267 0.62923351] [0.64325609 0.63850267 0.62923351] ... [0.36470588 0.50274510.66352941] [0.36470588 0.50274510.66352941] [0.30539216 0.450.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]]] '''