Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/DOTA_devkit/SplitOnlyImage.py
Views: 475
import os1import numpy as np2import cv23import copy4import dota_utils as util56class splitbase():7def __init__(self,8srcpath,9dstpath,10gap=100,11subsize=1024,12ext='.png'):13self.srcpath = srcpath14self.outpath = dstpath15self.gap = gap16self.subsize = subsize17self.slide = self.subsize - self.gap18self.srcpath = srcpath19self.dstpath = dstpath20self.ext = ext21def saveimagepatches(self, img, subimgname, left, up, ext='.png'):22subimg = copy.deepcopy(img[up: (up + self.subsize), left: (left + self.subsize)])23outdir = os.path.join(self.dstpath, subimgname + ext)24cv2.imwrite(outdir, subimg)2526def SplitSingle(self, name, rate, extent):27img = cv2.imread(os.path.join(self.srcpath, name + extent))28assert np.shape(img) != ()2930if (rate != 1):31resizeimg = cv2.resize(img, None, fx=rate, fy=rate, interpolation = cv2.INTER_CUBIC)32else:33resizeimg = img34outbasename = name + '__' + str(rate) + '__'3536weight = np.shape(resizeimg)[1]37height = np.shape(resizeimg)[0]3839left, up = 0, 040while (left < weight):41if (left + self.subsize >= weight):42left = max(weight - self.subsize, 0)43up = 044while (up < height):45if (up + self.subsize >= height):46up = max(height - self.subsize, 0)47subimgname = outbasename + str(left) + '___' + str(up)48self.saveimagepatches(resizeimg, subimgname, left, up)49if (up + self.subsize >= height):50break51else:52up = up + self.slide53if (left + self.subsize >= weight):54break55else:56left = left + self.slide5758def splitdata(self, rate):5960imagelist = util.GetFileFromThisRootDir(self.srcpath)61imagenames = [util.custombasename(x) for x in imagelist if (util.custombasename(x) != 'Thumbs')]62for name in imagenames:63self.SplitSingle(name, rate, self.ext)64if __name__ == '__main__':65split = splitbase(r'example/images',66r'example/imagesSplit')67split.splitdata(1)6869