python讀取numpy影象資料時將灰度影象轉為3通道並更改尺寸的方法
阿新 • • 發佈:2019-01-30
在用深度網路訓練時,大部分網路都要求輸入為3通道,而有時現有的資料為單通道的灰度圖,並且尺寸也不符合網路輸入,可用下面的函式轉換,以minist資料集為例。
import numpy as np
from keras.datasets import mnist
from keras.utils import to_categorical
image_size = 224
def load_mnist(image_size):
(x_train,y_train),(x_test,y_test) = mnist.load_data()
train_image = [cv2.cvtColor(cv2.resize(img,(image_size,image_size)),cv2.COLOR_GRAY2BGR) for img in x_train]
test_image = [cv2.cvtColor(cv2.resize(img,(image_size,image_size)),cv2.COLOR_GRAY2BGR) for img in x_test]
train_image = np.asarray(train_image)
test_image = np.asarray(test_image)
train_label = to_categorical(y_train)
test_label = to_categorical(y_test)
print('finish loading data!' )
return train_image, train_label, test_image, test_label