遥感影像语义分割——数据增强(图像和原图同时增强)
Posted Gendan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了遥感影像语义分割——数据增强(图像和原图同时增强)相关的知识,希望对你有一定的参考价值。
from PIL import Image, ImageFont, ImageDraw, ImageEnhance
import matplotlib.pyplot as plt
import numpy as np
import random
import random
import os
def image_rotate(image,label):
"""
对图像进行一定角度的旋转
:param image_path: 图像路径
:param save_path: 保存路径
:param angle: 旋转角度
:return:
"""
image_rotated = image.transpose(Image.ROTATE_90).convert(\'RGB\')
label_rotated = label.transpose(Image.ROTATE_90)
return image_rotated,label_rotated
def image_rotate1(image,label):
"""
对图像进行一定角度的旋转
:param image_path: 图像路径
:param save_path: 保存路径
:param angle: 旋转角度
:return:
"""
image_rotated = image.transpose(Image.ROTATE_270).convert(\'RGB\')
label_rotated = label.transpose(Image.ROTATE_270)
return image_rotated,label_rotated
def bright(image):
enh_bri = ImageEnhance.Brightness(image)
brightness = 1.2
image_brightened = enh_bri.enhance(brightness)
return image_brightened.convert(\'RGB\')
def ruidu(image):
enh_sha = ImageEnhance.Sharpness(image)
sharpness = 2.3
image_sharped = enh_sha.enhance(sharpness)
return image_sharped.convert(\'RGB\')
def sedu(image):
enh_col = ImageEnhance.Color(image)
color = 1.2
image_colored = enh_col.enhance(color)
return image_colored.convert(\'RGB\')
def duibidu(image):
enh_con = ImageEnhance.Contrast(image)
contrast = 1.3
image_contrasted = enh_con.enhance(contrast)
return image_contrasted.convert(\'RGB\')
def image_flip(image,label):
image_transpose = image.transpose(Image.FLIP_LEFT_RIGHT).convert(\'RGB\')
label_transpose = label.transpose(Image.FLIP_LEFT_RIGHT)
return image_transpose,label_transpose
def image_color(Skrill下载image,label):
image_transpose = image.transpose(Image.FLIP_TOP_BOTTOM).convert(\'RGB\')
label_transpose = label.transpose(Image.FLIP_TOP_BOTTOM)
return image_transpose,label_transpose
path_img = r\'E:\\torch-deeplabv3\\pytorch-deeplab-xception-master\\Waste2021\\JPEGImages\'
path_label = r\'E:\\torch-deeplabv3\\pytorch-deeplab-xception-master\\Waste2021\\SegmentationClass\'
path_new_img = r\'E:\\aa\\torch-deeplabv3\\pytorch-deeplab-xception-master\\Waste2021\\JPEGImages\'
path_new_label = r\'E:\\aa\\torch-deeplabv3\\pytorch-deeplab-xception-master\\Waste2021\\SegmentationClass\'
img_list = os.listdir(path_img)
label_list = os.listdir(path_label)
k=0
for i in range(len(img_list)):
img = Image.open(path_img + \'/\' + img_list[i])
label =Image.open(path_label + \'/\' + img_list[i][0:-4] + \'.png\')
#保存原图
img.convert(\'RGB\').save(path_new_img + \'/\' + str(("%05d" % (k))) + \'.jpg\')
label.save(path_new_label + \'/\' + str(("%05d" % (k))) + \'.png\')
k += 1
#角度旋转第一次
img1,mask = image_rotate(img,label)
img1.save(path_new_img + \'/\' + str(("%05d" % (k))) + \'.jpg\')
mask.save(path_new_label + \'/\' + str(("%05d" % (k))) + \'.png\')
k+=1
#角度旋转第二次
img1,mask = image_rotate1(img,label)
img1.save(path_new_img + \'/\' + str(("%05d" % (k))) + \'.jpg\')
mask.save(path_new_label + \'/\' + str(("%05d" % (k))) + \'.png\')
k+=1
#调整亮度
img1 = bright(img)
img1.save(path_new_img + \'/\' + str(("%05d" % (k))) + \'.jpg\')
label.save(path_new_label + \'/\' + str(("%05d" % (k))) + \'.png\')
k+=1
#调整对比度
img1 = duibidu(img)
img1.save(path_new_img + \'/\' + str(("%05d" % (k))) + \'.jpg\')
label.save(path_new_label + \'/\' + str(("%05d" % (k))) + \'.png\')
k+=1
#调整锐度
img1 = ruidu(img)
img1.save(path_new_img + \'/\' + str(("%05d" % (k))) + \'.jpg\')
label.save(path_new_label + \'/\' + str(("%05d" % (k))) + \'.png\')
k+=1
#调整色度
img1 = sedu(img)
img1.save(path_new_img + \'/\' + str(("%05d" % (k))) + \'.jpg\')
label.save(path_new_label + \'/\' + str(("%05d" % (k))) + \'.png\')
k+=1
#左右翻转
img1,mask = image_flip(img,label)
img1.save(path_new_img + \'/\' + str(("%05d" % (k))) + \'.jpg\')
mask.save(path_new_label + \'/\' + str(("%05d" % (k))) + \'.png\')
k+=1
#上下翻转
img1,mask = image_color(img,label)
img1.save(path_new_img + \'/\' + str(("%05d" % (k))) + \'.jpg\')
mask.save(path_new_label + \'/\' + str(("%05d" % (k))) + \'.png\')
k += 1
print(img_list[i] + \'is finished\')
以上是关于遥感影像语义分割——数据增强(图像和原图同时增强)的主要内容,如果未能解决你的问题,请参考以下文章
遥感数字图像处理实验:遥感影像增强方法大全处理看过来(Erdas版)
语义分割中的数据增强Augmentor基本使用方法(全随机生成和对每张图片均进行一次增强)