欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > 目标检测数据集图片及标签同步旋转

目标检测数据集图片及标签同步旋转

2024/10/24 16:22:59 来源:https://blog.csdn.net/qq_67105081/article/details/143136401  浏览:    关键词:目标检测数据集图片及标签同步旋转

在目标检测任务中,图像的旋转是一种常见且有效的增强策略。通过对图像进行旋转,能够增加数据集的多样性,帮助模型更好地应对不同角度下的目标。然而,在进行图像旋转时,如果不对标签(标注框)进行同步调整,旋转后的图像将与标签信息不匹配,导致训练数据需要再次标注,提高了任务量。

本篇文章将介绍如何在对目标检测数据集的图片进行旋转的同时,确保标签的同步旋转。通过这一增强手段,不仅可以扩展数据集的多样性,还能保证旋转后的图像与标签信息的精确对应,提升目标检测模型的鲁棒性和性能。

代码如下:

import cv2
import math
import os
import xml.etree.ElementTree as ETdef getRotatedImg(img_path,img_write_path):img = cv2.imread(img_path)rows, cols = img.shape[:2]a, b = cols / 2, rows / 2M = cv2.getRotationMatrix2D((a, b), angle, 1)rotated_img = cv2.warpAffine(img, M, (cols, rows))  # 旋转后的图像保持大小不变cv2.imwrite(img_write_path,rotated_img)return a,bdef getRotatedAnno(Pi_angle,a,b,anno_path,anno_write_path):tree = ET.parse(anno_path)root = tree.getroot()objects = root.findall("object")for obj in objects:bbox = obj.find('bndbox')x1 = float(bbox.find('xmin').text) - 1y1 = float(bbox.find('ymin').text) - 1x2 = float(bbox.find('xmax').text) - 1y2 = float(bbox.find('ymax').text) - 1x3=x1y3=y2x4=x2y4=y1X1 = (x1 - a) * math.cos(Pi_angle) - (y1 - b) * math.sin(Pi_angle) + aY1 = (x1 - a) * math.sin(Pi_angle) + (y1 - b) * math.cos(Pi_angle) + bX2 = (x2 - a) * math.cos(Pi_angle) - (y2 - b) * math.sin(Pi_angle) + aY2 = (x2 - a) * math.sin(Pi_angle) + (y2 - b) * math.cos(Pi_angle) + bX3 = (x3 - a) * math.cos(Pi_angle) - (y3 - b) * math.sin(Pi_angle) + aY3 = (x3 - a) * math.sin(Pi_angle) + (y3 - b) * math.cos(Pi_angle) + bX4 = (x4 - a) * math.cos(Pi_angle) - (y4 - b) * math.sin(Pi_angle) + aY4 = (x4 - a) * math.sin(Pi_angle) + (y4 - b) * math.cos(Pi_angle) + bX_MIN=min(X1,X2,X3,X4)X_MAX = max(X1, X2, X3, X4)Y_MIN = min(Y1, Y2, Y3, Y4)Y_MAX = max(Y1, Y2, Y3, Y4)bbox.find('xmin').text=str(int(X_MIN))bbox.find('ymin').text=str(int(Y_MIN))bbox.find('xmax').text=str(int(X_MAX))bbox.find('ymax').text=str(int(Y_MAX))tree.write(anno_write_path)  # 保存修改后的XML文件def rotate(angle,img_dir,anno_dir,img_write_dir,anno_write_dir):if not os.path.exists(img_write_dir):os.makedirs(img_write_dir)if not os.path.exists(anno_write_dir):os.makedirs(anno_write_dir)Pi_angle = -angle * math.pi / 180.0  # 弧度制,后面旋转坐标需要用到,注意负号!!!img_names=os.listdir(img_dir)for img_name in img_names:print(img_name)img_path=os.path.join(img_dir,img_name)img_write_path=os.path.join(img_write_dir,img_name[:-4]+'_R'+str(angle)+'.jpg')#anno_path=os.path.join(anno_dir,img_name[:-4]+'.xml')anno_write_path = os.path.join(anno_write_dir, img_name[:-4]+'_R'+str(angle)+'.xml')#a,b=getRotatedImg(img_path,img_write_path)getRotatedAnno(Pi_angle,a,b,anno_path,anno_write_path)angle = 180  #修改角度
img_dir=r' '  #输入图片文件夹
anno_dir=r' '  #输入标签文件夹
img_write_dir=r' '  #输出图片文件夹
anno_write_dir=r' '  #输出标签文件夹
if not os.path.exists(img_write_dir):os.makedirs(img_write_dir)
if not os.path.exists(anno_write_dir):os.makedirs(anno_write_dir)rotate(angle,img_dir,anno_dir,img_write_dir,anno_write_dir)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com