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)
对于遥感图像而言,将小样本切割重复。