加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SplitOnlyImage.py 2.38 KB
一键复制 编辑 原始数据 按行查看 历史
dingjiansw101 提交于 2018-03-14 11:39 . add SplitOnlyImage
import os
import numpy as np
import cv2
import copy
import dota_utils as util
class splitbase():
def __init__(self,
srcpath,
dstpath,
gap=100,
subsize=1024,
ext='.png'):
self.srcpath = srcpath
self.outpath = dstpath
self.gap = gap
self.subsize = subsize
self.slide = self.subsize - self.gap
self.srcpath = srcpath
self.dstpath = dstpath
self.ext = ext
def saveimagepatches(self, img, subimgname, left, up, ext='.png'):
subimg = copy.deepcopy(img[up: (up + self.subsize), left: (left + self.subsize)])
outdir = os.path.join(self.dstpath, subimgname + ext)
cv2.imwrite(outdir, subimg)
def SplitSingle(self, name, rate, extent):
img = cv2.imread(os.path.join(self.srcpath, name + extent))
assert np.shape(img) != ()
if (rate != 1):
resizeimg = cv2.resize(img, None, fx=rate, fy=rate, interpolation = cv2.INTER_CUBIC)
else:
resizeimg = img
outbasename = name + '__' + str(rate) + '__'
weight = np.shape(resizeimg)[1]
height = np.shape(resizeimg)[0]
left, up = 0, 0
while (left < weight):
if (left + self.subsize >= weight):
left = max(weight - self.subsize, 0)
up = 0
while (up < height):
if (up + self.subsize >= height):
up = max(height - self.subsize, 0)
subimgname = outbasename + str(left) + '___' + str(up)
self.saveimagepatches(resizeimg, subimgname, left, up)
if (up + self.subsize >= height):
break
else:
up = up + self.slide
if (left + self.subsize >= weight):
break
else:
left = left + self.slide
def splitdata(self, rate):
imagelist = util.GetFileFromThisRootDir(self.srcpath)
imagenames = [util.custombasename(x) for x in imagelist if (util.custombasename(x) != 'Thumbs')]
for name in imagenames:
self.SplitSingle(name, rate, self.ext)
if __name__ == '__main__':
split = splitbase(r'example/images',
r'example/imagesSplit')
split.splitdata(1)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化