目标检测图像数据增强(Data Augmentation)—— 旋转

简介: 应用场景 由于业务需求,需要对部分不符合检测结果的图像进行过滤,因此需要对之前的检测项目进行优化。常见问题有如下亮点: 图像中检测目标是倾斜角度; 图像中是通过镜子自拍或者加了滤镜处理后的相片;这两种情况是由于训练样本中含有这两种情况的少,因此需要增加此类样本数。

应用场景

由于业务需求,需要对部分不符合检测结果的图像进行过滤,因此需要对之前的检测项目进行优化。常见问题有如下亮点:

  • 图像中检测目标是倾斜角度;
  • 图像中是通过镜子自拍或者加了滤镜处理后的相片;
    这两种情况是由于训练样本中含有这两种情况的少,因此需要增加此类样本数。本文只针对第一种情况进行数据增强,解决办法——旋转。

素材

项目是对服装进行检测,样本图(来源于用户晒图):
来源于晒图
其对应的xml文件:

<annotation>
    <folder>well</folder>
    <filename>15278480618780.jpg</filename>
    <path>15278480618780.jpg</path>
    <size>
        <width>828</width>
        <height>1104</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>3</name>
        <pose>Unspecified</pose>
        <truncated>1</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>250</xmin>
            <ymin>672</ymin>
            <xmax>531</xmax>
            <ymax>1104</ymax>
        </bndbox>
    </object>
</annotation>

从xml信息中可以看见图像的具体信息,包括图像名称,尺寸以及检测方框的坐标范围。
在这里插入图片描述

处理程序

这里介绍处理批量处理文件夹中的情形,单张图像处理类似。

处理思想

  • 读取对应的图像,解析对应的xml,根据旋转的角度来变换之前检测到的坐标,以及保存变换后的图像。

处理代码

#!/usr/bin/env python

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


class ImgAugemention():
    def __init__(self):
        self.angle = 90

    # rotate_img
    def rotate_image(self, src, angle, scale=1.):
        w = src.shape[1]
        h = src.shape[0]
        # convet angle into rad
        rangle = np.deg2rad(angle)  # angle in radians
        # calculate new image width and height
        nw = (abs(np.sin(rangle)*h) + abs(np.cos(rangle)*w))*scale
        nh = (abs(np.cos(rangle)*h) + abs(np.sin(rangle)*w))*scale
        # ask OpenCV for the rotation matrix
        rot_mat = cv2.getRotationMatrix2D((nw*0.5, nh*0.5), angle, scale)
        # calculate the move from the old center to the new center combined
        # with the rotation
        rot_move = np.dot(rot_mat, np.array([(nw-w)*0.5, (nh-h)*0.5, 0]))
        # the move only affects the translation, so update the translation
        # part of the transform
        rot_mat[0, 2] += rot_move[0]
        rot_mat[1, 2] += rot_move[1]
        # map
        return cv2.warpAffine(
            src, rot_mat, (int(math.ceil(nw)), int(math.ceil(nh))),
            flags=cv2.INTER_LANCZOS4)

    def rotate_xml(self, src, xmin, ymin, xmax, ymax, angle, scale=1.):
        w = src.shape[1]
        h = src.shape[0]
        rangle = np.deg2rad(angle)  # angle in radians
        # now calculate new image width and height
        # get width and heigh of changed image
        nw = (abs(np.sin(rangle)*h) + abs(np.cos(rangle)*w))*scale
        nh = (abs(np.cos(rangle)*h) + abs(np.sin(rangle)*w))*scale
        # ask OpenCV for the rotation matrix
        rot_mat = cv2.getRotationMatrix2D((nw*0.5, nh*0.5), angle, scale)
        # calculate the move from the old center to the new center combined
        # with the rotation
        rot_move = np.dot(rot_mat, np.array([(nw-w)*0.5, (nh-h)*0.5, 0]))
        # the move only affects the translation, so update the translation
        # part of the transform
        rot_mat[0, 2] += rot_move[0]
        rot_mat[1, 2] += rot_move[1]
        # rot_mat: the final rot matrix
        # get the four center of edges in the initial martix,and convert the coord
        point1 = np.dot(rot_mat, np.array([(xmin+xmax)/2, ymin, 1]))
        point2 = np.dot(rot_mat, np.array([xmax, (ymin+ymax)/2, 1]))
        point3 = np.dot(rot_mat, np.array([(xmin+xmax)/2, ymax, 1]))
        point4 = np.dot(rot_mat, np.array([xmin, (ymin+ymax)/2, 1]))
        # concat np.array
        concat = np.vstack((point1, point2, point3, point4))
        # change type
        concat = concat.astype(np.int32)
        print(concat)
        rx, ry, rw, rh = cv2.boundingRect(concat)
        return rx, ry, rw, rh

    def process_img(self, imgs_path, xmls_path, img_save_path, xml_save_path, angle_list):
        # assign the rot angles
        for angle in angle_list:
            for img_name in os.listdir(imgs_path):
                # split filename and suffix
                n, s = os.path.splitext(img_name)
                # for the sake of use yolo model, only process '.jpg'
                if s == ".jpg":
                    img_path = os.path.join(imgs_path, img_name)
                    img = cv2.imread(img_path)
                    rotated_img = self.rotate_image(img, angle)
                    save_name = n + "_" + str(angle) + "d.jpg"
                    # 写入图像
                    cv2.imwrite(img_save_path + save_name, rotated_img)
                    print("log: [%sd] %s is processed." % (angle, img))
                    xml_url = img_name.split('.')[0] + '.xml'
                    xml_path = os.path.join(xmls_path, xml_url)
                    tree = ET.parse(xml_path)
                    file_name = tree.find('filename').text  # it is origin name
                    path = tree.find('path').text  # it is origin path
                    # change name and path
                    tree.find('filename').text = save_name  # change file name to rot degree name
                    tree.find('path').text = save_name  #  change file path to rot degree name
                    root = tree.getroot()
                    for box in root.iter('bndbox'):
                        xmin = float(box.find('xmin').text)
                        ymin = float(box.find('ymin').text)
                        xmax = float(box.find('xmax').text)
                        ymax = float(box.find('ymax').text)
                        x, y, w, h = self.rotate_xml(img, xmin, ymin, xmax, ymax, angle)
                        # change the coord
                        box.find('xmin').text = str(x)
                        box.find('ymin').text = str(y)
                        box.find('xmax').text = str(x+w)
                        box.find('ymax').text = str(y+h)
                        box.set('updated', 'yes')
                    # write into new xml
                    tree.write(xml_save_path + n + "_" + str(angle) + "d.xml")
                print("[%s] %s is processed." % (angle, img_name))


if __name__ == '__main__':
    img_aug = ImgAugemention()
    imgs_path = './image/'
    xmls_path = './xml/'
    img_save_path = './rotate/'
    xml_save_path = './xml_rot/'
    angle_list = [60, 90, 120, 150, 210, 240, 300]
    img_aug.process_img(imgs_path, xmls_path, img_save_path, xml_save_path, angle_list)

处理结果

  • 旋转60度
<annotation>
    <folder>well</folder>
    <filename>15278480618780_60d.jpg</filename>
    <path>15278480618780_60d.jpg</path>
    <size>
        <width>828</width>
        <height>1104</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>3</name>
        <pose>Unspecified</pose>
        <truncated>1</truncated>
        <difficult>0</difficult>
        <bndbox updated="yes">
            <xmin>777</xmin>
            <ymin>701</ymin>
            <xmax>1152</xmax>
            <ymax>945</ymax>
        </bndbox>
    </object>
</annotation>

在这里插入图片描述

  • 旋转90度
<annotation>
    <folder>well</folder>
    <filename>15278480618780_90d.jpg</filename>
    <path>15278480618780_90d.jpg</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>828</width>
        <height>1104</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>3</name>
        <pose>Unspecified</pose>
        <truncated>1</truncated>
        <difficult>0</difficult>
        <bndbox updated="yes">
            <xmin>672</xmin>
            <ymin>297</ymin>
            <xmax>1105</xmax>
            <ymax>579</ymax>
        </bndbox>
    </object>
</annotation>

在这里插入图片描述

参考

目录
相关文章
|
2月前
|
人工智能
【Mixup】探索数据增强技术:深入了解Mixup操作
【Mixup】探索数据增强技术:深入了解Mixup操作
59 0
|
算法 计算机视觉 异构计算
目标检测的Tricks | 【Trick7】数据增强——Mosaic(马赛克)
目标检测的Tricks | 【Trick7】数据增强——Mosaic(马赛克)
1288 0
目标检测的Tricks | 【Trick7】数据增强——Mosaic(马赛克)
|
机器学习/深度学习 存储 编解码
Open3d系列 | 3. Open3d实现点云上采样、点云聚类、点云分割以及点云重建
Open3d系列 | 3. Open3d实现点云上采样、点云聚类、点云分割以及点云重建
6404 0
Open3d系列 | 3. Open3d实现点云上采样、点云聚类、点云分割以及点云重建
|
JSON 数据格式 Python
对Labelme标注图像,进行90、180、270的旋转,实现标注数据的扩充。
对Labelme标注图像,进行90、180、270的旋转,实现标注数据的扩充。
800 0
对Labelme标注图像,进行90、180、270的旋转,实现标注数据的扩充。
|
5月前
|
存储 传感器 数据可视化
3D目标检测数据集 KITTI(标签格式解析、3D框可视化、点云转图像、BEV鸟瞰图)
本文介绍在3D目标检测中,理解和使用KITTI 数据集,包括KITTI 的基本情况、下载数据集、标签格式解析、3D框可视化、点云转图像、画BEV鸟瞰图等,并配有实现代码。
413 0
|
4月前
|
机器学习/深度学习 自动驾驶 安全
使用YOLO检测图像中的对象
使用YOLO检测图像中的对象
|
机器学习/深度学习 传感器 编解码
史上最全 | BEV感知算法综述(基于图像/Lidar/多模态数据的3D检测与分割任务)
以视觉为中心的俯视图(BEV)感知最近受到了广泛的关注,因其可以自然地呈现自然场景且对融合更友好。随着深度学习的快速发展,许多新颖的方法尝试解决以视觉为中心的BEV感知,但是目前还缺乏对该领域的综述类文章。本文对以视觉为中心的BEV感知及其扩展的方法进行了全面的综述调研,并提供了深入的分析和结果比较,进一步思考未来可能的研究方向。如下图所示,目前的工作可以根据视角变换分为两大类,即基于几何变换和基于网络变换。前者利用相机的物理原理,以可解释性的方式转换视图。后者则使用神经网络将透视图(PV)投影到BEV上。
史上最全 | BEV感知算法综述(基于图像/Lidar/多模态数据的3D检测与分割任务)
|
9月前
|
物联网 PyTorch 算法框架/工具
数据增强之图像变换与自定义transforms
数据增强之图像变换与自定义transforms
57 0
|
5月前
|
传感器 机器学习/深度学习 Ubuntu
【论文解读】F-PointNet 使用RGB图像和Depth点云深度 数据的3D目标检测
​F-PointNet 提出了直接处理点云数据的方案,但这种方式面临着挑战,比如:如何有效地在三维空间中定位目标的可能位置,即如何产生 3D 候选框,假如全局搜索将会耗费大量算力与时间。 F-PointNet是在进行点云处理之前,先使用图像信息得到一些先验搜索范围,这样既能提高效率,又能增加准确率。 论文地址:Frustum PointNets for 3D Object Detection from RGB-D Data  开源代码:https://github.com/charlesq34/frustum-pointnets
138 0
|
10月前
|
机器学习/深度学习 编解码 人工智能
深度学习应用篇-计算机视觉-图像增广1:数据增广、图像混叠、图像剪裁类变化类等详解
深度学习应用篇-计算机视觉-图像增广1:数据增广、图像混叠、图像剪裁类变化类等详解
深度学习应用篇-计算机视觉-图像增广1:数据增广、图像混叠、图像剪裁类变化类等详解