针对语义分割样本不平衡的算法调整

from PIL import Image
from tqdm import tqdm
import os
path = "D:/rssrai2019_semantic_segmentation/before_data/colormap_png/"#color图判断
path_img="D:/rssrai2019_semantic_segmentation/before_data/Image/Image_png_3/" #切割原图路径
path_class = "D:/rssrai2019_semantic_segmentation/before_data/colormap_png/class/" #切割灰度图路径
path_save = "D:/rssrai2019_semantic_segmentation/before_data/colormap_png/area_strength/" #保存路径
w=360
h=340
stride_w=180
stride_h=170
x1=0
y1=0
x2 = w
y2 = h
num=4000
for i in tqdm(range(1,11)):
    img = Image.open(path+str(i)+"_label.png")
    x1 = 0
    y1 = 0
    x2 = w
    y2 = h
    while x2<=7200:
        while y2<=6800:
            img1 = img.crop((x1,y1,x2,y2))
            img2 =Image.open(path_img+str(i)+".png")
            img3 = Image.open(path_class+str(i)+"_class.png")
            #判断
            count_plus=0
            for flag_x in range(360):
                for flag_y in range(340):
                    t = img1.getpixel((flag_x,flag_y))
                    if t in [(200,0,200),(150,0,250),(150,150,250),(200,200,0),(200,0,0),(250,150,150),(0,0,200),(0,150,200),(0,200,250)]:
                        count_plus+=1
                    if t in [(0,200,0),(150,250,0),(150,200,150),(250,200,0),(250,0,150),(200,150,150)]:
                        count_plus-=1
            if count_plus>0:
                img2_crop = img2.crop((x1,y1,x2,y2))
                img3_crop = img3.crop((x1,y1,x2,y2))
                num+=1
                path_save_image = path_save +"image/"+str(num)+".png"
                path_save_label = path_save + "label/" + str(num) + ".png"
                path_save_color = path_save +"color/"+str(num)+".png"
                img1.save(path_save_color)
                img2_crop.save(path_save_image)
                img3_crop.save(path_save_label)
            y1+=stride_h
            y2=y1+h
        x1+=stride_w
        x2=x1+w
        y1 = 0
        y2 = h
    print("获得图片个数",num)

对于遥感图像而言,将小样本切割重复。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值