解题代码
import cv2
import os
import numpy as np
def ReadAll(dir):
# 请在此添加实现代码 #
# ********** Begin *********#
imgfiles = os.listdir(dir)
# 初始化图像数据矩阵(N行,128*128列)
N = len(imgfiles)
mat = np.zeros((N, 128*128)) # 每个图像对应一行
for i in range(N):
# 图像不在当前目录,因此需要加上目录前缀
imgpath = dir + "/" + imgfiles[i]
img = cv2.imread(imgpath, cv2.IMREAD_GRAYSCALE)
rows, columns = img.shape
vec = img.reshape(rows*columns)
mat[i] = vec
return mat
# ********** End **********#