语义分割数据扩增

from PIL import Image

def cut(i,vx,vy,t):
    name1 = "D:/rssrai2019_semantic_segmentation/val/"+str(i)+".tif"
    name2 = "D:/rssrai2019_semantic_segmentation/val_cut/cut/"
    im = Image.open(name1)

    dx=340
    dy=360
    n=1+t

    x1=0
    y1=0
    x2=vx
    y2=vy
    while x2 <=6800:
        while y2<=7200:
            name3 = name2 + str(n) + ".tif"
            im2 = im.crop((y1,x1,y2,x2))
            im2.save(name3)
            y1+=dy
            y2 = y1+vy
            n+=1
        x1=x1+dx
        x2=x1+vx
        y1=0
        y2=vy
    print ("图片切割成功,切割得到的字图片数为")
    return n-1
if __name__=="__main__":
    t=0
    for i in range(9,11):
        res = cut(i,340,360,t)
        t+=400
        print(res)
  • 对分割好的图像,将classmap转换成相对应有标签的labelmap的灰度图
    这里采用的是对所有图片逐像素进行判断,巨废内存好吧。而且这个if判断也太蠢了吧,有没有大佬教我一下怎么简便一下啊。
import os
from PIL import Image
import numpy as np
path = 'D:/rssrai2019_semantic_segmentation/train_cut/cut_label/'
savedpath = 'D:/rssrai2019_semantic_segmentation/train_cut/cut_label_2/'
filelist = os.listdir(path)

for item in filelist:
    im = Image.open(path+item)
    width = im.size[0]
    height = im.size[1]

    im2=im.convert('L')
    p=np.array(im.convert('L'))
    p=p.T
    n=0
    for x in range(width):
        for y in range(height):
            r,g,b = im.getpixel((x,y))
            n+=1
            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)
    p.save(savedpath + item)
    print('item of %s is saved ' % (item))

 上一篇
基于PIL库的图像拼接处理操作 基于PIL库的图像拼接处理操作
包括对图像的旋转,镜像,加椒盐噪声,加高斯噪声,以及对图片的切分和语义分割的label图片的转换
2019-07-08
下一篇 
Windows环境安装tensorflow++++不需要安装Anaconda Windows环境安装tensorflow++++不需要安装Anaconda
本来我的深度学习框架是Pytorch,由于比赛原因,就要装一波tensorflow,以前也是装过tensorflow,但是需要Anaconda软件支撑就巨麻烦,并且它自带的python还会和之前自己电脑安装的python版本发生冲突问题。
2019-06-30
  目录