三分钟教会你用Python+OpenCV批量裁剪xml格式标注的图片
作者:18岁小白想成大牛 时间:2023-07-09 14:11:01
前言
在目标检测中,数据集常常使用labelimg标注,会生成xml文件。本文旨在根据xml标注文件来裁剪目标,以达到去除背景信息的目的。
xml文件格式
以下是一个标注好的图片生成的xml文件。具体含义见代码注释。
<annotation> <!--xml所属文件夹-->
<folder>JPEGImages</folder> <!--对应图片所属文件夹-->
<filename>408.bmp</filename>
<path>E:\JPEGImages\408.bmp</path>
<source>
<database>Unknown</database>
</source>
<size> <!--整张图片宽,高,3通道彩色-->
<width>413</width>
<height>342</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object><!--标注的目标,一共5个框-->
<name>bad_part</name><!--名称,也就是类别-->
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox> <!--坐标,左上,左下,右上,右下-->
<xmin>339</xmin>
<ymin>103</ymin>
<xmax>398</xmax>
<ymax>173</ymax>
</bndbox>
</object>
<object>
<name>bad_part</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>265</xmin>
<ymin>15</ymin>
<xmax>364</xmax>
<ymax>74</ymax>
</bndbox>
</object>
<object>
<name>bad_part</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>118</xmin>
<ymin>33</ymin>
<xmax>195</xmax>
<ymax>52</ymax>
</bndbox>
</object>
<object>
<name>bad_part</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>113</xmin>
<ymin>103</ymin>
<xmax>177</xmax>
<ymax>147</ymax>
</bndbox>
</object>
<object>
<name>bad_part</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>224</xmin>
<ymin>298</ymin>
<xmax>287</xmax>
<ymax>326</ymax>
</bndbox>
</object>
</annotation>
代码思想
获取到标签文件路径和图片文件路径;
遍历图片文件夹获取到图片,并分割文件名;
根据图片的文件名加后缀xml得到具体的标签文件;
通过xml.etree.ElementTree读取xml文件;
遍历所有的目标框,获取到[
];
通过cv2裁剪目标(先高后宽 参考文章:https://www.jb51.net/article/211183.htm中的第二中裁剪方法);
将裁剪好的图片保存到指定文件目录。
完整代码
'''
根据xml标签裁剪目标
1.文件夹中可以有除了图片格式或标签格式的文件,有判断可以忽略其他文件
2.图片未标注没有对应的xml文件自动忽略不会报错
3.同一个图片有多个真实框,会按名称_0,名称_1 …… 排列
4.图片本身的命名无规律会按3中的命名规则加上名称后缀重新命名。
'''
import cv2
import xml.etree.ElementTree as ET
import os
img_path = r'E:/JPEGImages' #图片路径
xml_path = r'E:/Annotations' #标签路径
obj_img_path = r'E:/cut' #目标裁剪图片存放路径
for img_file in os.listdir(img_path): #遍历图片文件夹
if img_file[-4:] in ['.bmp', '.jpg','.png']: #判断文件是否为图片格式
img_filename = os.path.join(img_path, img_file) #将图片路径与图片名进行拼接
img_cv = cv2.imread(img_filename) #读取图片
img_name = (os.path.splitext(img_file)[0]) #分割出图片名,如“000.png” 图片名为“000”
xml_name = xml_path + '\\' + '%s.xml'%img_name #利用标签路径、图片名、xml后缀拼接出完整的标签路径名
if os.path.exists(xml_name): #判断与图片同名的标签是否存在,因为图片不一定每张都打标
root = ET.parse(xml_name).getroot() #利用ET读取xml文件
count = 0 #目标框个数统计,防止目标文件覆盖
for obj in root.iter('object'): #遍历所有目标框
name = obj.find('name').text #获取目标框名称,即label名
xmlbox = obj.find('bndbox') #找到框目标
x0 = xmlbox.find('xmin').text #将框目标的四个顶点坐标取出
y0 = xmlbox.find('ymin').text
x1 = xmlbox.find('xmax').text
y1 = xmlbox.find('ymax').text
obj_img = img_cv[int(y0):int(y1), int(x0):int(x1)] #cv2裁剪出目标框中的图片
cv2.imwrite(obj_img_path + '\\' + '%s_%s'%(img_name, count) + '.jpg', obj_img) #保存裁剪图片
count += 1 #目标框统计值自增1
print("裁剪完成!")
效果展示
总结
ET解析树基本上是xml标注文件必备的解析库。
labelimg→ \rightarrow→xml→ \rightarrow→VOC数据集
labelme→ \rightarrow→json→ \rightarrow→COCO数据集
json是字典形式,内存小;xml是DTD文档,格式统一跨平台
去除背景信息是很重要的一环,可以避免模型监督学习时学习到相似的错误信息。
来源:https://blog.csdn.net/qq_36584673/article/details/117301833
标签:opencv,批量,裁剪
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用TFRecord存取多个数据案例
2023-07-14 22:26:34
oracle移植到mysql注意事项
2008-12-25 13:09:00
MySQL中InnoDB存储引擎的锁的基本使用教程
2024-01-21 09:27:01
![](https://img.aspxhome.com/file/2023/9/80009_0s.png)
ASP编程入门进阶教程
2008-06-29 18:00:00
![](https://img.aspxhome.com/file/UploadPic/20086/29/2008629183150594s.jpg)
phpStorm2020 注册码
2024-05-11 10:07:19
HTML的form表单和django的form表单
2021-08-03 07:04:42
![](https://img.aspxhome.com/file/2023/6/119426_0s.png)
基于Python实现骰子小游戏
2023-07-11 22:39:58
![](https://img.aspxhome.com/file/2023/3/128443_0s.jpg)
基于bootstrap实现收缩导航条
2024-04-28 09:53:16
![](https://img.aspxhome.com/file/2023/1/132721_0s.jpg)
Python实现判断并移除列表指定位置元素的方法
2023-03-21 03:06:19
![](https://img.aspxhome.com/file/2023/0/67270_0s.png)
python中SSH远程登录设备的实现方法
2023-10-24 02:30:58
![](https://img.aspxhome.com/file/2023/7/80787_0s.png)
python统计cpu利用率的方法
2022-02-05 23:33:44
Python的Django应用程序解决AJAX跨域访问问题的方法
2023-08-07 16:00:24
关于Python排序问题(冒泡/选择/插入)
2023-11-17 19:43:37
![](https://img.aspxhome.com/file/2023/7/124147_0s.gif)
SQL Server误区30日谈 第5天 AWE在64位SQL SERVER中必须开启
2024-01-17 15:25:36
asp 随机字符串函数
2011-04-04 11:01:00
Python实现监控Nginx配置文件的不同并发送邮件报警功能示例
2023-09-22 11:07:33
![](https://img.aspxhome.com/file/2023/2/131052_0s.png)
使用Math.max,Math.min获取数组中的最值实例
2023-09-03 23:23:59
Python人工智能之波士顿房价数据分析
2021-09-23 19:43:35
![](https://img.aspxhome.com/file/2023/8/131548_0s.jpg)
python3.9和pycharm的安装教程并创建简单项目的步骤
2021-06-18 05:34:16
![](https://img.aspxhome.com/file/2023/9/72189_0s.png)
zabbix监控Nginx/Tomcat/MySQL的详细教程
2024-01-15 04:31:33
![](https://img.aspxhome.com/file/2023/3/93893_0s.png)