包括对图像的旋转,镜像,加椒盐噪声,加高斯噪声,以及对图片的切分和语义分割的label图片的转换
from PIL import Image
import numpy as np
from skimage import io,data,util
def rotate(img,op):
if op == 1: ##旋转90度
img = img.transpose(Image.ROTATE_90)
return img
elif op == 2: ##旋转180度
img = img.transpose(Image.ROTATE_180)
return img
elif op == 3: ##旋转270度
img = img.transpose(Image.ROTATE_270)
return img
def mirror(img,op):
if op == 1: ##左右镜像
img = img.transpose(Image.FLIP_LEFT_RIGHT)
return img
elif op == 2: ##上下镜像
img = img.transpose(Image.FLIP_TOP_BOTTOM)
return img
elif op == 3:
img1 =mirror(img,1)
img = mirror(img1,2)
return img
def salt_noise(img,num_noise): ##只可以处理三通道的图像,加椒盐噪声
rows = img.size[0]
cols = img.size[1]
for noise in range(num_noise):
x = np.random.randint(0, rows)
y = np.random.randint(0,cols)
if noise % 2:
img.putpixel((x,y),(255,255,255))
else:
img.putpixel((x,y),(0,0,0))
return img
def gauss_noise(img):
img = util.random_noise(img,mode='gaussian',seed=None,clip=True)
return img
def cut(img,cut_x,cut_y,path,t): #输入待切割的图片和需要切割的大小
dx = cut_x
dy = cut_y
width = img.size[0]
height = img.size[1]
x1 = 0
y1 = 0
x2 = cut_x
y2 = cut_y
num = 0+t
while x2 <= height:
while y2 <=width:
img2 = img.crop((y1,x1,y2,x2))
num+=1
path1 = path + str(num) + ".png"
img2.save(path1)
y1+=dy
y2 = y1+cut_y
x1 = x1+dx
x2 = x1+cut_x
y1 = 0
y2 = cut_y
print("切割图片个数",num)
return (height//dx)*(width//dy)
def label_translate(img,op): #op=1,colormap转label
#op=2,label转colormap
width = img.size[0]
height = img.size[1]
if op == 1:
p = np.array(img.convert('L'))
p = p.T
for x in range(width):
for y in range(height):
r,g,b = img.getpixel((x,y))
if (r == 0 and g == 0 and b == 0):
p[x,y]=0
if(r == 0 and g == 200 and b == 0):
p[x,y]=1
if(r == 150 and g == 250 and b == 0):
p[x,y]=2
if(r == 150 and g == 200 and b == 150):
p[x,y]=3
if(r == 200 and g == 0 and b == 200):
p[x,y]=4
if(r == 150 and g == 0 and b == 250):
p[x,y]=5
if(r == 150 and g == 150 and b == 250):
p[x, y] = 6
if(r == 250 and g == 200 and b == 0):
p[x,y]=7
if(r == 200 and g == 200 and b == 0):
p[x,y]=8
if(r == 200 and g == 0 and b == 0):
p[x,y]=9
if(r == 250 and g == 0 and b == 150):
p[x,y]=10
if(r == 200 and g == 150 and b == 150):
p[x,y]=11
if(r == 250 and g == 150 and b == 150):
p[x,y]=12
if(r == 0 and g == 0 and b == 200):
p[x,y]=13
if(r == 0 and g == 150 and b == 200):
p[x,y]=14
if(r == 0 and g == 200 and b == 250):
p[x,y]=15
p=p.T
p=Image.fromarray(p)
return p
elif op == 2:
p = np.array(img.convert('RGB'))
p = p.T
for x in range(width):
for y in range(height):
gray = img.getpixel((x, y))
if (gray == 0):
p[:, x, y] = [0, 0, 0]
if (gray == 1):
p[:, x, y] = [0, 200, 0]
if (gray == 2):
p[:, x, y] = [150, 250, 0]
if (gray == 3):
p[:, x, y] = [150, 200, 150]
if (gray == 4):
p[:, x, y] = [200, 0, 200]
if (gray == 5):
p[:, x, y] = [150, 0, 250]
if (gray == 6):
p[:, x, y] = [150, 150, 250]
if (gray == 7):
p[:, x, y] = [250, 200, 0]
if (gray == 8):
p[:, x, y] = [200, 200, 0]
if (gray == 9):
p[:, x, y] = [200, 0, 0]
if (gray == 10):
p[:, x, y] = [250, 0, 150]
if (gray == 11):
p[:, x, y] = [200, 150, 150]
if (gray == 12):
p[:, x, y] = [250, 150, 150]
if (gray == 13):
p[:, x, y] = [0, 0, 200]
if (gray == 14):
p[:, x, y] = [0, 150, 200]
if (gray == 15):
p[:, x, y] = [0, 200, 250]
p = p.T
p = Image.fromarray(p)
return p
if __name__=="__main__":
path = "D:/rssrai2019_semantic_segmentation/before_data/colormap_png/"
path_save ="G:/label++/"
t = 0
for i in range(1,11):
img = Image.open(path+str(i)+"_label.png")
img = label_translate(img,1)
img_1=img_2=img_3=img_4=img_5=img_6=img
t+=cut(img,256,256,path_save,t)
#img2 = salt_noise(img, 500000)
t+= cut(img, 256, 256, path_save, t)
# img3 = gauss_noise(img)
# t += cut(img3, 256, 256, path_save, t)
#img = Image.open(path + str(i) + "_label.png")
img4 = rotate(img_1,1)
t+=cut(img4,256,256,path_save,t)
#img5 = salt_noise(img4, 500000)
t += cut(img4, 256, 256, path_save, t)
# img6 = gauss_noise(img4)
# t += cut(img6, 256, 256, path_save, t)
#img = Image.open(path+str(i)+"_label.png")
img7 = rotate(img_2,2)
t += cut(img7, 256, 256, path_save, t)
#img8 = salt_noise(img7, 500000)
t += cut(img7, 256, 256, path_save, t)
# img9 = gauss_noise(img7)
# t += cut(img9, 256, 256, path_save, t)
#img = Image.open(path+str(i)+"_label.png")
img10 = rotate(img_3, 3)
t += cut(img10, 256, 256, path_save, t)
#img11 = salt_noise(img10, 500000)
t += cut(img10, 256, 256, path_save, t)
# img12 = gauss_noise(img10)
# t += cut(img12, 256, 256, path_save, t)
#img = Image.open(path+str(i)+"_label.png")
img13 = mirror(img_4, 1)
t += cut(img13, 256, 256, path_save, t)
#img14 = salt_noise(img13, 500000)
t += cut(img13, 256, 256, path_save, t)
# img15 = gauss_noise(img13)
# t += cut(img13, 256, 256, path_save, t)
#img = Image.open(path+str(i)+"_label.png")
img16 = mirror(img_5, 2)
t += cut(img16, 256, 256, path_save, t)
#img17 = salt_noise(img16, 500000)
t += cut(img_5, 256, 256, path_save, t)
# img18 = gauss_noise(img16)
# t += cut(img18, 256, 256, path_save, t)
#img = Image.open(path+str(i)+"_label.png")
img19 = mirror(img_6, 3)
t += cut(img19, 256, 256, path_save, t)
#img20 = salt_noise(img19, 500000)
t += cut(img19, 256, 256, path_save, t)
# img21 = gauss_noise(img18)
# t += cut(img21, 256, 256, path_save, t)
print("第%d张图片加强已完成"%i)