目标检测小数据集使用旋转(90度、180度、270度)进行图像增广

1. 图像旋转简介

       目标检测图像旋转是一种常见的数据增强方法,它可以增加训练数据的多样性,提高模型的泛化能力。在求边界时,假定图像进行顺时针旋转,因此需要进行反推新像素位置在原图像中的对应位置,需要用逆时针计算
顺时针计算方法:
X = x c o s ( θ ) + y s i n ( θ ) Y = y c o s ( θ ) − x s i n ( θ ) \mathrm{X=xcos(\theta)+ysin(\theta)} \\ \mathrm{Y=ycos(\theta)-xsin(\theta)} X=xcos(θ)+ysin(θ)Y=ycos(θ)xsin(θ)逆时针计算方法
X = x c o s ( θ ) − y s i n ( θ ) Y = x s i n ( θ ) + y c o s ( θ ) \mathrm{X=xcos(\theta)-ysin(\theta)} \\ \mathrm{Y=xsin(\theta)+ycos(\theta)} X=xcos(θ)ysin(θ)Y=xsin(θ)+ycos(θ)
x , y {x,y} xy 以图像的中点为坐标原点

2. 代码及详解

import cv2, glob
import math
import numpy as np

# 图像路径获取
paths = glob.glob('./dateset/images/*')
paths.sort()
# 图像标签路径获取
lables = glob.glob('./dateset/labels/*')
lables.sort()

# 对图像进行旋转
# 旋转角度 rotate_angle 必须为 90 / 180 / 270
def rotate_im_poly(im, box, rotate_angle):
    
    im_w, im_h = im.shape[1], im.shape[0]
    dst_im = im.copy()
    dst_polys = []
    rand_degree_cnt = int(rotate_angle/90)
    # np.rot90() 将图像矩阵 逆时针 旋转90°
    for i in range(rand_degree_cnt):
        dst_im = np.rot90(dst_im)

    # 将角度转换为 π,注意一下这里的负号
    rot_degree = -90 * rand_degree_cnt
    rot_angle = rot_degree * math.pi / 180.0
    # 图像的中心点
    cx, cy = 0.5 * im_w, 0.5 * im_h
    # 转换后图像的中心点
    ncx, ncy = 0.5 * dst_im.shape[1], 0.5 * dst_im.shape[0]
    # 进行坐标的转换
    x1 = math.cos(rot_angle) * (box[0] - cx) - math.sin(rot_angle) * (box[1] - cy) + ncx
    y1 = math.sin(rot_angle) * (box[0] - cx) + math.cos(rot_angle) * (box[1] - cy) + ncy
    x2 = math.cos(rot_angle) * (box[2] - cx) - math.sin(rot_angle) * (box[3] - cy) + ncx
    y2 = math.sin(rot_angle) * (box[2] - cx) + math.cos(rot_angle) * (box[3] - cy) + ncy
    dst_polys = [x1, y1, x2, y2]

    # 左上角位置标签要小于右下角位置标签
    if dst_polys[0] > dst_polys[2]:
        sw = dst_polys[0]
        dst_polys[0] = dst_polys[2]
        dst_polys[2] = sw
    if dst_polys[1] > dst_polys[3]:
        sw = dst_polys[1]
        dst_polys[1] = dst_polys[3]
        dst_polys[3] = sw

    return dst_im, dst_polys

# Yolo格式数据集
for path, label in zip(paths, lables):
    # 图像读取
    img = cv2.imread(path)
    with open('./' + path.split('\\')[-1][:-3] + 'txt', 'w') as up:
        # 标签读取
        lines = open(label).readlines()
        for line in lines:
            line = line.strip()
            line = line.split()
            cls = line[0]
            box = [int(line[1]), int(line[2]), int(line[3]), int(line[4])]
            # 进行旋转
            img_, box = rotate_im_poly(img, box, 90)
            # 旋转后标签写入标签文件
            up.write(str(cls)+ ' ' + " ".join([str(int(a)) for a in box]) + '\n')
    path = './' + path.split('\\')[-1][:-3] + 'jpg'
    # 图像存储
    cv2.imwrite(path, img_)
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在机器学习中,图像增广(Augmentation)是一种常用的技术,可以通过对原始图像进行一系列变换,生成多样性的新图像,从而扩充数据集,提高模型的泛化能力。在图像增广中,增强图像和对比是比较常用的操作之一。 增强图像(Brightness Enhancement)通常使用以下两种方式实现: 1. 调整像素值:对图像中的所有像素值进行加法或乘法操作,从而提高整个图像的亮。例如,对于灰图像,可以使用以下公式进行调整: ``` I_new = a * I_old + b ``` 其中,I_old是原始图像中的像素值,I_new是调整后的像素值,a和b是调整系数。 2. 调整亮通道:对图像中的不同通道进行调整,从而提高图像的亮。例如,对于RGB图像,可以将R、G、B通道中的像素值分别进行加法或乘法操作,从而实现亮调整。 增强图像对比(Contrast Enhancement)通常使用以下两种方式实现: 1. 直方图均衡化:直方图均衡化是一种常用的增强图像对比的方法,可以通过对图像的像素值进行统计分析,将像素值转换为更适合显示的值,从而提高图像的对比。 2. 自适应直方图均衡化:自适应直方图均衡化是一种基于直方图均衡化的改进方法,可以对图像中的不同区域进行自适应的直方图均衡化,从而提高图像的对比。 在Python中,可以使用OpenCV库实现图像增广中的亮和对比增强操作。示例代码如下: ```python import cv2 import numpy as np # 读取图像 img = cv2.imread('image.jpg') # 增强亮 a = 1.5 # 增强系数 b = 50 # 偏移量 img_bright = np.uint8(np.clip((a * img + b), 0, 255)) # 增强对比 clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_contrast = clahe.apply(img_gray) # 显示图像 cv2.imshow('Original Image', img) cv2.imshow('Brightness Enhanced Image', img_bright) cv2.imshow('Contrast Enhanced Image', img_contrast) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在上述示例代码中,使用cv2.imread()方法读取原始图像,然后分别使用增强和对比增强的方法对图像进行处理,最后使用cv2.imshow()方法显示处理后的图像。需要注意的是,不同的图像增广方法可能会对模型的训练效果产生不同的影响,需要根据具体应用场景进行选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值