比賽地址:https://tianchi.aliyun.com/competition/entrance/231717/introduction
這次比賽給的圖非常大5萬x5萬,在訓練之前必須要進行數據的切割。通常切割后的大小為512x512,或者1024x1024.
按照512x512切完后的結果如下:
切圖時需要注意的幾點是:
gdal的二進制安裝包wheels在:https://www.lfd.uci.edu/~gohlke/pythonlibs/ 這里獲取
圖像是4個channel,前三個是RGB,第四個是alpha通道(透明)丟掉
圖像的區域很多事空白的需要濾掉,不處理。
切割的時候需要有冗余。
大小不能按照完全的512,1024等切割,切割的要大一點數據在后期需要增強:弱縮放,旋轉等。
上代碼:
from osgeo import gdal from PIL import Image import os if __name__=='__main__': name=input("input the image number 1 or 2 you want clip:") imagepath='./data/image_{}.png'.format(name) n=os.path.basename(imagepath)[:-4] labelname='./data/'+n+'_label.png' dslb=gdal.Open(labelname) ds=gdal.Open(imagepath) wx=ds.RasterXSize wy=ds.RasterYSize stx=0 sty=0 step=900 outsize=1500 nullthresh=outsize*outsize*0.7 cx=0 cy=0 while cy+outsize<wy: cx=0 while cx+outsize<wx: img=ds.ReadAsArray(cx,cy,outsize,outsize) img2=img[:3,:,:].transpose(1,2,0) if (img2[:,:,0]==0).sum()>nullthresh: cx+=step print('kongbai...',cx,cy) continue img2=Image.fromarray(img2,'RGB') img2.save('./data/train/data1500/'+n+'_{}_{}.bmp'.format(cx,cy)) #deal with label img=dslb.ReadAsArray(cx,cy,outsize,outsize) img=Image.fromarray(img).convert('L') img.save('./data/train/label1500/'+n+'_{}_{}.bmp'.format(cx,cy)) cx+=step cy+=step
路徑需要修改,就可使用。
這里我按照1500x1500大小切割的,打算用1024訓練。
這樣的數據的切圖就算准備完了。如下圖: