首先,我们比赛用的图片是7200*6800的大图像,我们首先对他进行了400份分割,得到3200份图像

  • 分割图像,这里是采用了PIL库,由于图像要求,无法采用opencv进行处理
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))