遥感影像语义分割——数据增强(图像和原图同时增强)
8位图像与24位图像
? Labelme标注图像生成的标签图为8位彩色图,在Python中用PIL查看图片模式为**‘P’**。在深度学习做训练时, 输入的训练图像需要8位彩色图或者8位灰度图。下面演示24位与这两种模式的转换。
? 24位彩色图转8位色彩图。在转8位彩色图之后,可能会出现像素值的变化,博主目前也没有找到原因和解决方法。如果有知道解决方法的也欢迎交流。
from PIL import Image
img = Image.open('000.png')
new_img = img.convert('P')
? 24位彩色图转8位灰度图。
from PIL import Image
img = Image.open('000.png')
new_img = img.convert('L')
数据增强
? 如果用opencv来做数据增强会导致输入的8位彩色图变成24位彩色图。这样会导致数据集出现问题无法训练。因此下面介绍使用PIL做数据增强,包括旋转角度、翻转、色度、对比度、亮度的改变。
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(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')
str(("%05d" % (k))) + ‘.jpg’) mask.save(path_new_label + ‘/’ + str(("%05d" % (k))) + ‘.png’) k += 1
print(img_list[i] + 'is finished')
|