根据LabelImg标注的方框大小批量裁剪图片
用LabelImg软件在windows下标注了图片了后,可以根据标注的大小进行图片的裁剪,下图是LabelImg软件图
代码
代码可实现对图片批量裁剪处理,并且裁剪后的图片进行了重新命名
import os
import xml.dom.minidom
from xml.etree import ElementTree as ET
import cv2
xml_path = 'Datasets/test/contamination'
pic_path = 'Datasets/test/contamination'
count = 0
s = {}
# 对xml文件和图片文件进行排序,不排序可能导致图片和xml文件对应不上
xml_files = os.listdir(xml_path)
xml_files.sort()
pic_files = os.listdir(pic_path)
pic_files.sort()
for xmlFile in xml_files:
if not os.path.isdir(xmlFile):
print(xmlFile)
# 找到xml文件中的bndbox属性
if xmlFile.endswith('.xml'):
per = ET.parse(os.path.join(xml_path, xmlFile))
p_value = per.findall('./object/bndbox')
# 获得完整xml中的bndbox的路径
dom = xml.dom.minidom.parse(os.path.join(xml_path, xmlFile))
root = dom.documentElement
# 通过元素名获取name的属性值
xml_name = root.getElementsByTagName('name')
# xml_object = root.getElementByTagName('object')
# 这里case实际为name的第一个child_data
for i in range(len(xml_name)):
if xml_name[i].firstChild.data == 'case':
values = p_value[i].getchildren()
# 获取坐标值
for child in values:
s[child.tag] = child.text
# 分别将四个点的坐标赋值给四个变量
x_min = dict.get(s, 'xmin')
y_min = dict.get(s, 'ymin')
x_max = dict.get(s, 'xmax')
y_max = dict.get(s, 'ymax')
# 处理图片
for bmpFile in pic_files:
if not os.path.isdir(bmpFile):
print(bmpFile)
if bmpFile.endswith('.bmp'):
if count < 10:
# 读取图片
img = cv2.imread(pic_path + '/' + '000' + str(count) + '.bmp')
# 根据获得的xml中的坐标信息裁剪图片
crop = img[int(y_min):int(y_max), int(x_min):int(x_max)]
# 裁剪完成,写入图像
cv2.imwrite('./Datasets/Test_of_pic_crop/contamination/' + '00' + str(count) + '.bmp', crop)
elif 10 <= count < 100:
img = cv2.imread(pic_path + '/' + '00' + str(count) + '.bmp')
crop = img[int(y_min):int(y_max), int(x_min):int(x_max)]
cv2.imwrite('./Datasets/Test_of_pic_crop/contamination/' + '0' + str(count) + '.bmp', crop)
else:
img = cv2.imread(pic_path + '/' + '0' + str(count) + '.bmp')
crop = img[int(y_min):int(y_max), int(x_min):int(x_max)]
cv2.imwrite('./Datasets/Test_of_pic_crop/contamination/' + str(count) + '.bmp', crop)
count += 1
break
示例图片
xml文件:
<annotation>
<folder>broken_large</folder>
<filename>0002.bmp</filename>
<path>C:\Users\13357\Desktop\test\broken_large\0002.bmp</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>2592</width>
<height>1944</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>case</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>1554</xmin>
<ymin>343</ymin>
<xmax>2195</xmax>
<ymax>1810</ymax>
</bndbox>
</object>
</annotation>
截切前图片:
裁剪后图片:
这里裁剪用到了上面xml文件的xmin、ymin、xmax、ymax四个点的坐标:
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>1554</xmin>
<ymin>343</ymin>
<xmax>2195</xmax>
<ymax>1810</ymax>
</bndbox>