-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcrop.py
287 lines (254 loc) · 12.2 KB
/
crop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 8 09:25:17 2018
裁剪
@author: zxl
"""
import cv2
import os
import random
import voc_xml
import utils
from voc_xml import CreateXML
def crop_img(src,top_left_x,top_left_y,crop_w,crop_h):
'''裁剪图像
Args:
src: 源图像
top_left,top_right:裁剪图像左上角坐标
crop_w,crop_h:裁剪图像宽高
return:
crop_img:裁剪后的图像
None:裁剪尺寸错误
'''
rows,cols,n_channel = src.shape
row_min,col_min = int(top_left_y), int(top_left_x)
row_max,col_max = int(row_min + crop_h), int(col_min + crop_w)
if row_max > rows or col_max > cols:
print("crop size err: src->%dx%d,crop->top_left(%d,%d) %dx%d"%(cols,rows,col_min,row_min,int(crop_w),int(crop_h)))
return None
crop_img = src[row_min:row_max,col_min:col_max]
return crop_img
def crop_xy(x,y,top_left_x,top_left_y,crop_w,crop_h):
''' 坐标平移变换
Args:
x,y:待变换坐标
top_left_x,top_left_y:裁剪图像左上角坐标
crop_w,crop_h:裁剪部分图像宽高
return:
crop_x,crop_y
'''
crop_x = int(x - top_left_x)
crop_y = int(y - top_left_y)
crop_x = utils.confine(crop_x,0,crop_w-1)
crop_y = utils.confine(crop_y,0,crop_h-1)
return crop_x,crop_y
def crop_box(box,top_left_x,top_left_y,crop_w,crop_h,iou_thr=0.5):
'''目标框坐标平移变换
Args:
box:目标框坐标[xmin,ymin,xmax,ymax]
top_left_x,top_left_y:裁剪图像左上角坐标
crop_w,crop_h:裁剪部分图像宽高
iou_thr: iou阈值,去除裁剪后过小目标
return:
crop_box:平移变换结果[xmin,ymin,xmax,ymax]
'''
xmin,ymin = crop_xy(box[0],box[1],top_left_x,top_left_y,crop_w,crop_h)
xmax,ymax = crop_xy(box[2],box[3],top_left_x,top_left_y,crop_w,crop_h)
croped_box = [xmin,ymin,xmax,ymax]
if utils.calc_iou([0,0,box[2]-box[0],box[3]-box[1]],[0,0,xmax-xmin,ymax-ymin]) < iou_thr:
croped_box = [0,0,0,0]
return croped_box
def crop_xml(crop_img_name,xml_tree,top_left_x,top_left_y,crop_w,crop_h,iou_thr=0.5):
'''xml目标框裁剪变换
Args:
crop_img_name:裁剪图片命名
xml_tree:待crop的xml ET.parse()
top_left_x,top_left_y: 裁剪图像左上角坐标
crop_w,crop_h: 裁剪图像宽高
iou_thr: iou阈值
return:
createdxml : 创建的xml CreateXML对象
'''
root = xml_tree.getroot()
size = root.find('size')
depth = int(size.find('depth').text)
createdxml = CreateXML(crop_img_name,int(crop_w),int(crop_h),depth)
for obj in root.iter('object'):
obj_name = obj.find('name').text
xml_box = obj.find('bndbox')
xmin = int(xml_box.find('xmin').text)
ymin = int(xml_box.find('ymin').text)
xmax = int(xml_box.find('xmax').text)
ymax = int(xml_box.find('ymax').text)
box = crop_box([xmin,ymin,xmax,ymax],top_left_x,top_left_y,crop_w,crop_h,iou_thr)
if (box[0] >= box[2]) or (box[1] >= box[3]):
continue
createdxml.add_object_node(obj_name,box[0],box[1],box[2],box[3])
return createdxml
def crop_img_xml(img,xml_tree,crop_img_name,top_left_x,top_left_y,crop_w,crop_h,iou_thr):
'''裁剪图像和xml目标框
Args:
img:源图像
crop_img_name:裁剪图片命名
xml_tree:待crop的xml ET.parse()
top_left_x,top_left_y: 裁剪图像左上角坐标
crop_w,crop_h: 裁剪图像宽高
iou_thr: iou阈值
return:
croped_img,croped_xml : 裁剪完成的图像和xml文件
None:裁剪尺寸错误
'''
croped_img = crop_img(img,top_left_x,top_left_y,crop_w,crop_h)
if croped_img is None:
return None
croped_xml = crop_xml(crop_img_name,xml_tree,top_left_x,top_left_y,crop_w,crop_h,iou_thr)
return croped_img,croped_xml
def crop_img_xml_from_dir(imgs_dir,xmls_dir,imgs_save_dir,xmls_save_dir,img_suffix,name_suffix,\
crop_type='RANDOM_CROP',crop_n=1,dsize=(0,0),fw=1.0,fh=1.0,random_wh=False,iou_thr=0.5):
'''随机裁剪指定路径下的图片和xml
Args:
imgs_dir,xmls_dir: 待放缩图片、原始xml文件存储路径
imgs_save_dir,xmls_save_dir: 处理完成的图片、xml文件存储路径
img_suffix: 图片可能的后缀名['.jpg','.png','.bmp',..]
name_suffix: 处理完成的图片、xml的命名标识
crop_type:裁剪风格 ['RANDOM_CROP','CENTER_CROP','FIVE_CROP']
crop_n: 每原图生成裁剪图个数
dsize:指定crop宽高(w,h),与random_wh==True互斥生效
fw,fh: 当random_wh==False时为crop比例,否则为随机crop的宽高比例下限
random_wh:随机选定裁剪宽高
iou_thr: iou阈值
'''
for root,dirs,files in os.walk(xmls_dir):
for xml_name in files:
xml_file = os.path.join(xmls_dir,xml_name)
#print(xml_file)
img_file = None
for suffix in img_suffix:
#print(os.path.join(imgs_dir,xml_name.split('.')[0]+suffix))
if os.path.exists(os.path.join(imgs_dir,xml_name.split('.')[0]+suffix)):
img_file = os.path.join(imgs_dir,xml_name.split('.')[0]+suffix)
break
if img_file is None:
print("there has no image for ",xml_name)
continue
img = cv2.imread(img_file)
imgh,imgw,n_channels = img.shape
if crop_type == 'CENTER_CROP':
crop_n = 1
elif crop_type == 'FIVE_CROP':
crop_n = 5
for i in range(crop_n):
crop_imgw,crop_imgh = dsize
if dsize == (0,0) and not random_wh:
crop_imgw = int(imgw*fw)
crop_imgh = int(imgh*fh)
elif random_wh:
crop_imgw = int(imgw*(fw + random.random()*(1-fw)))
crop_imgh = int(imgh*(fh + random.random()*(1-fh)))
if crop_type == 'RANDOM_CROP':
crop_top_left_x,crop_top_left_y = random.randint(0,imgw-crop_imgw-1),random.randint(0,imgh-crop_imgh-1)
elif crop_type == 'CENTER_CROP':
crop_top_left_x,crop_top_left_y = int(imgw/2-crop_imgw/2),int(imgh/2-crop_imgh/2)
elif crop_type == 'FIVE_CROP':
if i == 0:
crop_top_left_x,crop_top_left_y = 0,0
elif i == 1:
crop_top_left_x,crop_top_left_y = imgw-crop_imgw-1,0
elif i == 2:
crop_top_left_x,crop_top_left_y = 0,imgh-crop_imgh-1
elif i == 3:
crop_top_left_x,crop_top_left_y = imgw-crop_imgw-1,imgh-crop_imgh-1
else:
crop_top_left_x,crop_top_left_y = int(imgw/2-crop_imgw/2),int(imgh/2-crop_imgh/2)
else:
print('crop type wrong! expect [RANDOM_CROP,CENTER_CROP,FIVE_CROP]' )
croped_img_name = xml_name.split('.')[0]+'_'+name_suffix +\
str(crop_top_left_x)+'_'+str(crop_top_left_y)+\
'_wh'+str(crop_imgw)+'x'+str(crop_imgh)+\
'.'+img_file.split('.')[-1]
croped = crop_img_xml(img,voc_xml.get_xml_tree(xml_file),croped_img_name,crop_top_left_x,crop_top_left_y,crop_imgw,crop_imgh,iou_thr)
imgcrop,xmlcrop = croped[0],croped[1]
cv2.imwrite(os.path.join(imgs_save_dir,croped_img_name),imgcrop)
xmlcrop.save_xml(xmls_save_dir,croped_img_name.split('.')[0]+'.xml')
def crop_imgs_without_label(imgs_dir,imgs_save_dir,name_suffix,crop_type='RANDOM_CROP',\
crop_n=1,dsize=(0,0),fw=1.0,fh=1.0,random_wh=False):
'''仅裁剪图片,不带标签
Args:
imgs_dir: 待放缩图片、原始xml文件存储路径
imgs_save_dir: 处理完成的图片、xml文件存储路径
name_suffix: 处理完成的图片、xml的命名标识
crop_type:裁剪风格 ['RANDOM_CROP','CENTER_CROP','FIVE_CROP']
crop_n: 每原图生成裁剪图个数
dsize:指定crop宽高(w,h),与random_wh==True互斥生效
fw,fh: 当random_wh==False时为crop比例,否则为随机crop的宽高比例下限
random_wh:随机选定裁剪宽高
'''
imgcount = utils.fileCountIn(imgs_dir)
count = 0
for root,dirs,files in os.walk(imgs_dir):
for file in files:
img_file = os.path.join(imgs_dir,file)
img = cv2.imread(img_file)
imgh,imgw,n_channels = img.shape
if crop_type == 'CENTER_CROP':
crop_n = 1
elif crop_type == 'FIVE_CROP':
crop_n = 5
for i in range(crop_n):
crop_imgw,crop_imgh = dsize
if dsize == (0,0) and not random_wh:
crop_imgw = int(imgw*fw)
crop_imgh = int(imgh*fh)
elif random_wh:
crop_imgw = int(imgw*(fw + random.random()*(1-fw)))
crop_imgh = int(imgh*(fh + random.random()*(1-fh)))
if crop_type == 'RANDOM_CROP':
crop_top_left_x,crop_top_left_y = random.randint(0,imgw-crop_imgw-1),random.randint(0,imgh-crop_imgh-1)
elif crop_type == 'CENTER_CROP':
crop_top_left_x,crop_top_left_y = int(imgw/2-crop_imgw/2),int(imgh/2-crop_imgh/2)
elif crop_type == 'FIVE_CROP':
if i == 0:
crop_top_left_x,crop_top_left_y = 0,0
elif i == 1:
crop_top_left_x,crop_top_left_y = imgw-crop_imgw-1,0
elif i == 2:
crop_top_left_x,crop_top_left_y = 0,imgh-crop_imgh-1
elif i == 3:
crop_top_left_x,crop_top_left_y = imgw-crop_imgw-1,imgh-crop_imgh-1
else:
crop_top_left_x,crop_top_left_y = int(imgw/2-crop_imgw/2),int(imgh/2-crop_imgh/2)
else:
print('crop type wrong! expect [RANDOM_CROP,CENTER_CROP,FIVE_CROP]' )
croped_img_name = file.split('.')[0]+'_'+name_suffix +\
str(crop_top_left_x)+'_'+str(crop_top_left_y)+\
'_wh'+str(crop_imgw)+'x'+str(crop_imgh)+\
'.jpg'
croped_img = crop_img(img,crop_top_left_x,crop_top_left_y,crop_imgw,crop_imgh)
cv2.imwrite(os.path.join(imgs_save_dir,croped_img_name),croped_img)
count += 1
if count % 10 == 0:
print('[%d|%d] %d%%'%(count,imgcount,count*100/imgcount))
def main():
imgs_dir ='C:/Users/zxl/Desktop/test/JPEGImages/'
xmls_dir = 'C:/Users/zxl/Desktop/test/Annotations/'
imgs_save_dir= 'C:/Users/zxl/Desktop/test/crop_imgs/'
if not os.path.exists(imgs_save_dir):
os.makedirs(imgs_save_dir)
xmls_save_dir='C:/Users/zxl/Desktop/test/crop_xmls/'
if not os.path.exists(xmls_save_dir):
os.makedirs(xmls_save_dir)
img_suffix=['.jpg','.png','.bmp']
name_suffix='crop' #命名标识
crop_type = 'RANDOM_CROP' #['RANDOM_CROP','CENTER_CROP','FIVE_CROP']
crop_n = 5 #每张原图 crop 5张图
dsize = (400,300) #指定裁剪尺度
fw=0.5
fh=0.7 #指定裁剪尺度比例
random_wh=False #是否随机尺度裁剪,若为True,则dsize指定的尺度失效
iou_thr = 0.5 #裁剪后目标框大小与原框大小的iou值大于该阈值则保留
crop_img_xml_from_dir(imgs_dir,xmls_dir,imgs_save_dir,xmls_save_dir,img_suffix,name_suffix,\
crop_type,crop_n,dsize,fw,fh,random_wh,iou_thr)
# crop_imgs_without_label(imgs_dir,imgs_save_dir,name_suffix,crop_type,\
# crop_n,dsize,fw,fh,random_wh)
if __name__=='__main__':
main()