python 讀入圖片成固定大小 並存入h5文件

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文件就可以載入訓練集了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章