通过旋转角度扩充数据集

本文介绍了一种在遥感图像检测和血细胞检测项目中,通过旋转图像来增强数据集的方法。由于原始TensorFlow Object Detection API不包含旋转操作,作者选择直接在数据集上进行旋转,每张图像旋转20次,每次18度,以增加网络的定向鲁棒性。同时,还调整了XML注释文件中boundingbox的参数,确保旋转后的目标仍然被正确框定。
摘要由CSDN通过智能技术生成

       tensorflow 的object detection api有一系列预处理方法(如flip等),但不包括旋转rotation,这可能是因为常规项目不会涉及到图像的360度旋转。但旋转在某些特定任务中尤为重要,笔者最近的遥感图像检测和血细胞检测项目采用的都是俯瞰视角,这种情况下,训练数据集的旋转操作可以使得网络具有方向的鲁棒性。

       初步发现,api中常规的预处理操作是封装在.proto文件里面了,再通过.config文件设置、选择,preprogress.py等文件集成、调用。要直接添加,需要修改最初的配置文件,笔者水平有限,程序改不了那么深。。。

       因此,直接在数据集上进行操作,对图像进行旋转,并对相应的annotation中的.xml文件进行修改,使得图像和目标按特定角度旋转。暂定每张图像旋转20次,每次18度。这样,原来的一张图像就生成了20张旋转后的图像,同时也进行了数据集的扩充。方法也比较粗暴,但简单可行。     

      首先是图像旋转的程序,

# -*- coding: utf-8 -*-
"""
Created on Tue Oct  9 09:57:04 2018

@author: PC
"""
import tensorflow as tf
from scipy import misc
import numpy as np
import os 
from skimage import transform

global angle
angle = 0.0    
#文件名
def file_name_original(file): 
    return os.path.splitext(file)[0] 

#扩展名
def file_name_extension(file): 
    return os.path.splitext(file)[1] 

#随机旋转图片
def random_rotate_image(image_input_path, image_output_path, image_file, num):
    global angle
    with tf.Graph().as_default():
#        tf.set_random_seed(666)
        file_contents = tf.read_file(os.path.join(image_input_path, image_file))
        image = tf.image.decode_image(file_contents, channels=3)
        image_rotate_en_list = []
        def random_rotate_image_func(image, angle):
            return misc.imrotate(image, angle, 'bicubic')
#            return transform.rotate(image, angle,resize=True)
        for i in range(num):
            image_rotate = tf.py_func(random_rotate_image_func, [image, angle], tf.uint8)
            angle = (angle + 18)
#            image_rotate = misc.imrotate(image, 90, 'bicubic')
  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值