标签:__ img AI 切图 cy outsize Pytorch cx gdal
比赛地址: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训练。
这样的数据的切图就算准备完了。如下图:
标签:__,img,AI,切图,cy,outsize,Pytorch,cx,gdal 来源: https://www.cnblogs.com/yjphhw/p/11076903.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。