数据增强,原始图像和xml文件旋转

博客介绍了让image和xml按相同角度翻转的操作,可在目标识别中针对小样本进行数据增强。还提到若要更改保存路径,修改rotated_path即可,同时指出旋转后图像大小会改变,并展示了原始与翻转后的图像及xml。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       使得image和xml按着相同的角度进行翻转,可以在目标识别中针对小样本进行一定的数据增强操作

import os
import numpy as np
import cv2
import math
import xml.etree.ElementTree as ET
from PIL import Image


# 按角度翻转图片
def rotate_img(src, angle, scale=1):
    width = src.shape[1]      # 原始图像的宽
    height = src.shape[0]     # 原始图像的高
    # 角度变弧度
    re_angle = np.deg2rad(angle)
    # 计算新图片的高度和宽度
    new_width = (abs(np.sin(re_angle) * height) + abs(np.cos(re_angle) * width)) * scale
    new_height = (abs(np.cos(re_angle) * height) + abs(np.sin(re_angle) * width)) * scale

    rotate_matrix = cv2.getRotationMatrix2D((new_width * 0.5, new_height * 0.5), angle, scale)
    rotate_move = np.dot(rotate_matrix, np.array([(new_width - width) * 0.5, (new_height - height) * 0.5, 0]))

    # update translation
    rotate_matrix[0, 2] += rotate_move[0]
    rotate_matrix[1, 2] += rotate_move[1]

    dst = cv2.warpAffine(img, rotate_matrix, (int(math.ceil(new
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值