统一数据集大小

import os
import cv2
import numpy as np
import xml.etree.ElementTree as ET

img_path_old = r'E:\data\statistical_distribution\JPEGImages'  # 原图片文件夹路径
img_path_new = r'E:\data\statistical_distribution\image_resize'  # 修改大小后的图片文件夹路径
xml_path_old = r'E:\data\statistical_distribution\Annotations'  # 原xml的文件夹路径
xml_path_new = r'E:\data\statistical_distribution\xml_resize'  # 新xml的文件夹路径
if not os.path.exists(img_path_new): os.makedirs(img_path_new)
if not os.path.exists(xml_path_new): os.makedirs(xml_path_new)
c_w, c_h = 640, 640  # 目标图片的尺寸


def edit_xml(xml_file, ratio, i):
    all_xml_file = os.path.join(xml_path_old, xml_file)
    # print(all_xml_file)
    tree = ET.parse(all_xml_file)
    size_o = tree.find('size')  # 修改xml文件中的图像尺寸大小为新的尺寸大小
    size_width = size_o.find('width')
    size_height = size_o.find('height')
    size_width.text = str(c_w)
    size_height.text = str(c_h)

    objs = tree.findall('object')  # 修改每个目标对应的坐标
    for obj in objs:
        obj_bnd = obj.find('bndbox')
        obj_bnd = obj.find('bndbox')
        obj_xmin = obj_bnd.find('xmin')
        obj_ymin = obj_bnd.find('ymin')
        obj_xmax = obj_bnd.find('xmax')
        obj_ymax = obj_bnd.find('ymax')
        xmin = float(obj_xmin.text)
        ymin = float(obj_ymin.text)
        xmax = float(obj_xmax.text)
        ymax = float(obj_ymax.text)
        obj_xmin.text = str(round(xmin * ratio))
        obj_ymin.text = str(round(ymin * ratio))
        obj_xmax.text = str(round(xmax * ratio))
        obj_ymax.text = str(round(ymax * ratio))

    newfile = os.path.join(xml_path_new, '%05d' % (0 + i) + '.xml')
    tree.write(newfile, method='xml', encoding='utf-8')  # 更新xml文件


if __name__ == '__main__':
    files = os.listdir(img_path_old)  # 获取文件名列表
    i = 0
    for file in files:
        img_zeros = np.zeros((c_w, c_h, 3), np.uint8)  # 创建全黑的图像
        if file.endswith('.png'):
            imgName = os.path.join(img_path_old, file)  # 获取文件完整路径
            xml_file = file.replace('.png', '.xml')
            img = cv2.imread(imgName)  # 读图
            h, w, _ = img.shape  # 获取图像宽高
            # 缩放图像,宽高大于c_w的按长边等比例缩放,小于c_w的保持原图像大小:
            if max(w, h) > c_w:
                ratio = c_w / max(w, h)
                imgcrop = cv2.resize(img, (round(w * ratio), round(h * ratio)))
                # 将缩放后的图像复制进全黑图像里
                img_zeros[0:round(h * ratio), 0:round(w * ratio)] = imgcrop
                edit_xml(xml_file, ratio, i)
            else:
                img_zeros[0:h, 0:w] = img
                edit_xml(xml_file, 1, i)

            # 设置新的文件名:
            newName = os.path.join(img_path_new, '%05d' % (0 + i) + '.jpg')
            i += 1
            print(newName)
            cv2.imwrite(newName, img_zeros)  # 存储按新文件名命令的图片

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值