CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hukaixuan19970627

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: hukaixuan19970627/yolov5_obb
Path: blob/master/DOTA_devkit/SplitOnlyImage.py
Views: 475
1
import os
2
import numpy as np
3
import cv2
4
import copy
5
import dota_utils as util
6
7
class splitbase():
8
def __init__(self,
9
srcpath,
10
dstpath,
11
gap=100,
12
subsize=1024,
13
ext='.png'):
14
self.srcpath = srcpath
15
self.outpath = dstpath
16
self.gap = gap
17
self.subsize = subsize
18
self.slide = self.subsize - self.gap
19
self.srcpath = srcpath
20
self.dstpath = dstpath
21
self.ext = ext
22
def saveimagepatches(self, img, subimgname, left, up, ext='.png'):
23
subimg = copy.deepcopy(img[up: (up + self.subsize), left: (left + self.subsize)])
24
outdir = os.path.join(self.dstpath, subimgname + ext)
25
cv2.imwrite(outdir, subimg)
26
27
def SplitSingle(self, name, rate, extent):
28
img = cv2.imread(os.path.join(self.srcpath, name + extent))
29
assert np.shape(img) != ()
30
31
if (rate != 1):
32
resizeimg = cv2.resize(img, None, fx=rate, fy=rate, interpolation = cv2.INTER_CUBIC)
33
else:
34
resizeimg = img
35
outbasename = name + '__' + str(rate) + '__'
36
37
weight = np.shape(resizeimg)[1]
38
height = np.shape(resizeimg)[0]
39
40
left, up = 0, 0
41
while (left < weight):
42
if (left + self.subsize >= weight):
43
left = max(weight - self.subsize, 0)
44
up = 0
45
while (up < height):
46
if (up + self.subsize >= height):
47
up = max(height - self.subsize, 0)
48
subimgname = outbasename + str(left) + '___' + str(up)
49
self.saveimagepatches(resizeimg, subimgname, left, up)
50
if (up + self.subsize >= height):
51
break
52
else:
53
up = up + self.slide
54
if (left + self.subsize >= weight):
55
break
56
else:
57
left = left + self.slide
58
59
def splitdata(self, rate):
60
61
imagelist = util.GetFileFromThisRootDir(self.srcpath)
62
imagenames = [util.custombasename(x) for x in imagelist if (util.custombasename(x) != 'Thumbs')]
63
for name in imagenames:
64
self.SplitSingle(name, rate, self.ext)
65
if __name__ == '__main__':
66
split = splitbase(r'example/images',
67
r'example/imagesSplit')
68
split.splitdata(1)
69