根据LabelImg标注的方框大小批量裁剪图片

根据LabelImg标注的方框大小批量裁剪图片

用LabelImg软件在windows下标注了图片了后,可以根据标注的大小进行图片的裁剪,下图是LabelImg软件图
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>
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值