数据增强的代码

运行顺序:-co-scale-brightness
c0.py:

import os
from PIL import Image
from PIL import ImageEnhance
import math
import numpy as np
imageDir="C:/flower_dealed_dataSet/prepare_pic/train/c0"
saveDir="C:/flower_dealed_dataSet/prepare_pic/newdata"
#水平翻转
def flip(root_path,img_name):
img = Image.open(os.path.join(root_path, img_name))
filp_img = img.transpose(Image.FLIP_LEFT_RIGHT)
return filp_img
#垂直翻转
def flip1(root_path,img_name):
img = Image.open(os.path.join(root_path, img_name))
filp_img = img.transpose(Image.FLIP_TOP_BOTTOM)
return filp_img
#随机裁剪
def randomCrop(root_path,image):
image = Image.open(os.path.join(root_path, image))
image_width = image.size[0]
image_height = image.size[1]
crop_image_width = math.ceil(image_width*2/3)
crop_image_height = math.ceil(image_height*2/3)
x = np.random.randint(0, image_width - crop_image_width)
y = np.random.randint(0, image_height - crop_image_height)
random_region = (x, y, x + crop_image_width, y + crop_image_height)
return image.crop(random_region)

#旋转
def rotation(root_path, img_name):
img = Image.open(os.path.join(root_path, img_name))
rotation_img = img.rotate(180)
return rotation_img
i=0
for name in os.listdir(imageDir):
i=i+1
saveName = "c0.flip_" + str(i) + ".jpg"
saveImage = flip(imageDir, name)
saveImage.save(os.path.join(saveDir, saveName))

saveName1 = "c0.flips_" + str(i) + ".jpg"
saveImage1 = flip1(imageDir, name)
saveImage1.save(os.path.join(saveDir, saveName1))

saveName2 = "c0.crop_" + str(i) + ".jpg"
saveImage2 = randomCrop(imageDir, name)
saveImage2.save(os.path.join(saveDir, saveName2))

saveName3 = "c0.rotation_" + str(i) + ".jpg"
saveImage3 = rotation(imageDir, name)
saveImage3.save(os.path.join(saveDir, saveName3))

scale.py:

import os
import cv2
def ImageScale(img, scale):
size = img.shape
SIZE1 = size[0]
SIZE2 = size[1]
if scale > 1:
size1 = int(SIZE1 * scale) + 1
size2 = int(SIZE2 * scale)
img = cv2.resize(img, (size2, size1), interpolation=cv2.INTER_CUBIC) # 双三次线性插值法.
a1 = (size1 - SIZE1) / 2
b1 = size1 - a1
a2 = (size2 - SIZE2) / 2
b2 = size2 - a2
img = img[a1:b1, a2:b2]
else: # 即scale<1
size1 = int(SIZE1 * scale)
size2 = int(SIZE2 * scale) + 1 # 是否加1,根据具体图像尺寸的奇偶决定
img = cv2.resize(img, (size2, size1), interpolation=cv2.INTER_CUBIC) # 双三次线性插值法.
return img
i=0
for root, dirs, files in os.walk('C:/flower_dealed_dataSet/prepare_pic/train/c1'):
for file in files:
img_path = root+'/'+file
img = cv2.imread(img_path,1)
i=i+1
im=ImageScale(img,0.8)
img_saving_path = img_path.replace(file,'c1.scale_'+str(i)+'.jpg')
cv2.imwrite(img_saving_path, im)

brightness.py:

import os

from PIL import Image, ImageEnhance
imageDir="C:/flower_dealed_dataSet/prepare_pic/train/c1"
saveDir="C:/flower_dealed_dataSet/prepare_pic/train/c1"
#增强亮度
def brightnessEnhancement(root_path,img_name):
image = Image.open(os.path.join(root_path, img_name))
enh_bri = ImageEnhance.Brightness(image)
brightness = 1.5
image_brightened = enh_bri.enhance(brightness)
return image_brightened

i=0
for name in os.listdir(imageDir):
i=i+1
saveName = "c1.brightness_" + str(i) + ".jpg"
saveImage = brightnessEnhancement(imageDir, name)
saveImage.save(os.path.join(saveDir, saveName))

你可能感兴趣的:(数据增强的代码)