import cv2
import h5py
import numpy as np
ROOT_VOC_DIRECTORY = './VOC2012/'
# 读取所有图片的名字
def readImageName():
namesFile = ROOT_VOC_DIRECTORY+'train_image_names.txt'
allImageNames = []
fd = open(namesFile, "r")
for line in fd.readlines():
allImageNames.append(line.strip().lstrip())
fd.close()
return allImageNames
# 添加椒盐噪声
def SaltAndPepper(src, percetage):
NoiseImg = src.copy()
NoiseNum = int(percetage*src.shape[0]*src.shape[1])
for i in range(NoiseNum):
randX = np.random.random_integers(0, src.shape[0]-1)
randY = np.random.random_integers(0, src.shape[1]-1)
if np.random.random_integers(0, 1) == 0:
NoiseImg[randX, randY] = 0
else:
NoiseImg[randX, randY] = 255
return NoiseImg
# 添加高斯噪声
def GaessNoisy(src, sigma):
NoiseImg = src.copy()
s = np.random.normal(0, 1, size=src.shape)*sigma
NoiseImg = np.add(NoiseImg, s)
NoiseImg.astype(dtype=np.uint8)
return NoiseImg
def readImage2Data(pathcImageNmaes, patchSize, stride):
patchImageInput = np.empty(shape=[0, patchSize, patchSize, 1])
patchImageLabel = np.empty(shape=[0, patchSize, patchSize, 1])
for i in range(len(pathcImageNmaes)):
img = cv2.imread(ROOT_VOC_DIRECTORY+'JPEGImages/'+pathcImageNmaes[i], 0)
# 添加椒盐噪声
# noisyImage = SaltAndPepper(img,0.2)
# 添加高斯噪声
noisyImage = GaessNoisy(img, 20)
#re to 0-1
img = img/255.0
noisyImage = noisyImage/255.0
# cv2.imshow('i', img)
# cv2.imshow('img', noisyImage)
# cv2.waitKey()
row = (img.shape[0]-patchSize)//stride
line = (img.shape[1]-patchSize)//stride
imgPatch = np.zeros(shape=[row*line, patchSize, patchSize, 1])
imgPatchLabel = np.zeros(shape=[row*line, patchSize, patchSize, 1])
for r in range(row):
for l in range(line):
imgPatch[r*line+l, ..., 0] = img[r*stride:r*stride+patchSize, l*stride:l*stride+patchSize]
imgPatchLabel[r*line+l, ..., 0] = noisyImage[r*stride:r*stride+patchSize, l*stride:l*stride+patchSize]
patchImageInput = np.vstack((patchImageInput, imgPatch))
patchImageLabel = np.vstack((patchImageLabel, imgPatchLabel))
return patchImageInput, patchImageLabel
def writeData2H5(data, label, batchSize, i, i11):
data = data[0:(data.shape[0] - data.shape[0] % batchSize), ...]
label = label[0:(label.shape[0] - label.shape[0] % batchSize), ...]
if i == 0:
file = h5py.File('./VOC2012/train/train1/train0.h5'.format(0), 'w')
carh5data = file.create_dataset('data', data=data, shape=data.shape, maxshape=(None, 120, 120, 1), dtype=np.float32)
carh5label = file.create_dataset('label', data=label, shape=label.shape, maxshape=(None, 120, 120, 1), dtype=np.float32)
i11 = data.shape[0]
else:
h5f = h5py.File("./VOC2012/train/train1/train0.h5", "a") # add mode
x = h5f["data"]
y = h5f["label"]
x.resize([i11+data.shape[0], 120, 120, 1])
y.resize([i11+data.shape[0], 120, 120, 1])
x[i11:i11+data.shape[0]] = data
y[i11:i11+data.shape[0]] = label
i11 = i11+data.shape[0]
if i == 29:
h5f.close()
return i11
if __name__ == '__main__':
allImageNames = readImageName()
PATCH_SIZE = 120 # 片段大小
STRIDE_SIZE = 80 # 步长大小
BATCH_SIZE = 128 # 训练一个批次的大小
NUM_ONE_H5 = 100 # 一个h5文件放图片的数量
NUM_H5 = 30 # h5文件数量
NUM_BEGIN = 0 # 第几个h5文件开始
i = len(allImageNames)
NUM_H5_MAX = len(allImageNames)//NUM_ONE_H5
i11 = 0
for i in range(NUM_BEGIN, NUM_H5_MAX, 1):
patchImageName = allImageNames[i*NUM_ONE_H5:(i+1)*NUM_ONE_H5]
data, label = readImage2Data(patchImageName, PATCH_SIZE, STRIDE_SIZE)
print(i)
if i == 0:
i11 = writeData2H5(data, label, BATCH_SIZE, i, i11)
else:
i11 = writeData2H5(data, label, BATCH_SIZE, i, i11)
这是我做毕设的时候,对图片的预处理。由于我的图片是大小规格不一的,而我要把图片输入我的神经网络,我把网络的输入格式设定为是固定的,就需要我对图片做一些大小的处理,并把图片存入h5文件,这就是作为我的训练集了,后面直接读入h5文件就可以载入训练集了。