对Labelme标注图像,进行90、180、270的旋转,实现标注数据的扩充。

简介: 对Labelme标注图像,进行90、180、270的旋转,实现标注数据的扩充。

在制作做遥感图像物体检测数据集的时候,遥感图像的物体都是平面的,有角度的问题,

可以对被检测物体实现不同角度的旋转,丰富数据集同时减少标注的工作量。

tt.png


比如上图中的飞机,机头的朝向是斜向下的,现实中的飞机可能有各种的朝向,如果不做旋转,就会降低模型的检测能力。下图是旋转90度的效果。


tt.png

需要安装的包:


labelme


scipy1.0.0版本


pyqt5



旋转最大的难点在于旋转后,需要对标注的点重新计算,保证标注的坐标不出现错乱。


旋转90度后,坐标转化:


 points=shapelabel['points']#获取初始的坐标。

 newPoints = [[float(points[0][1]), w-float(points[1][0])],

                [float(points[1][1]), w-float(points[0][0])]]#旋转90度,重新对应坐标。w表示原始图像的宽度。

选旋转180度后,坐标转化:


points = shapelabel['points']

newPoints = [[w-float(points[1][0]), h - float(points[1][1])],

                    [w-float(points[0][0]), h - float(points[0][1])]] #旋转180度,重新对应坐标。h表示原始图像的高度。

旋转270度,坐标转化:


points = shapelabel['points']


newPoints = [[h - float(points[1][1]), float(points[0][0])],

                    [h - float(points[0][1]),  float(points[1][0])]]


完整代码如下:


#scipy的版本为1.0.0


import scipy


from scipy import misc


import os


import glob


import PIL.Image


from labelme.logger import logger


from labelme import PY2


from labelme import QT4


import io


import json


import os.path as osp


import PIL.Image


from scipy import ndimage


import base64


from labelme import utils


def load_image_file(filename):

   try:

       image_pil = PIL.Image.open(filename)

   except IOError:

       logger.error('Failed opening image file: {}'.format(filename))

       return


   # apply orientation to image according to exif

   image_pil = utils.apply_exif_orientation(image_pil)


   with io.BytesIO() as f:

       ext = osp.splitext(filename)[1].lower()

       if PY2 and QT4:

           format = 'PNG'

       elif ext in ['.jpg', '.jpeg']:

           format = 'JPEG'

       else:

           format = 'PNG'

       image_pil.save(f, format=format)

       f.seek(0)

       return f.read()


def dict_json(flags,imageData,shapes,imagePath,fillColor=None,lineColor=None,imageHeight=100,imageWidth=100):

   '''

   :param imageData: str

   :param shapes: list

   :param imagePath: str

   :param fillColor: list

   :param lineColor: list

   :return: dict

   '''

   return {"version":"3.16.4","flags":flags,"shapes":shapes,'lineColor':lineColor,"fillColor":fillColor,'imagePath':imagePath.split('\\')[1],"imageData":imageData,'imageHeight':imageHeight,'imageWidth':imageWidth}



def get_image_paths(folder):

   return glob.glob(os.path.join(folder, '*.jpg'))


def create_read_img(filename):

   data = json.load(open(filename.split('.')[0]+'.json'))

   shapes = data["shapes"]

   version = data["version"]

   flags = data["flags"]

   lineColor = data["lineColor"]

   fillColor = data['fillColor']

   newshapes = []

   im = misc.imread(filename)

   h,w,d=im.shape

   img_rote_90 = ndimage.rotate(im, 90)

   img_path_90=filename[:-4]+'_90.jpg'

   scipy.misc.imsave(img_path_90,img_rote_90)

   imageData_90 = load_image_file(img_path_90)

   imageData_90 = base64.b64encode(imageData_90).decode('utf-8')

   imageHeight =w

   imageWidth = h

   for shapelabel in shapes:

       newLabel=shapelabel['label']

       newline_color=shapelabel['line_color']

       newfill_color=shapelabel['fill_color']

       points=shapelabel['points']

       newPoints = [[float(points[0][1]), w-float(points[1][0])],

                [float(points[1][1]), w-float(points[0][0])]]

       newshape_type=shapelabel['shape_type']

       newflags=shapelabel['flags']

       newshapes.append({'label':newLabel,'line_color':newline_color,'fill_color':newfill_color,'points':newPoints,'shape_type':newshape_type,'flags':newflags})

   data_90 = dict_json(flags, imageData_90, newshapes, img_path_90, fillColor, lineColor, imageHeight, imageWidth)

   json_file = img_path_90[:-4] + '.json'

   json.dump(data_90, open(json_file, 'w'))

   img_rote_180 = ndimage.rotate(im, 180)

   img_path_180=filename[:-4]+'_180.jpg'

   scipy.misc.imsave(img_path_180,img_rote_180)

   imageData_180 = load_image_file(img_path_180)

   imageData_180 = base64.b64encode(imageData_180).decode('utf-8')

   imageHeight = h

   imageWidth = w

   newshapes = []

   for shapelabel in shapes:

       newLabel = shapelabel['label']

       newline_color = shapelabel['line_color']

       newfill_color = shapelabel['fill_color']

       points = shapelabel['points']

       newPoints = [[w-float(points[1][0]), h - float(points[1][1])],

                    [w-float(points[0][0]), h - float(points[0][1])]]

       newshape_type = shapelabel['shape_type']

       newflags = shapelabel['flags']

       newshapes.append(

           {'label': newLabel, 'line_color': newline_color, 'fill_color': newfill_color, 'points': newPoints,

            'shape_type': newshape_type, 'flags': newflags})

   data_180 = dict_json(flags, imageData_180, newshapes, img_path_180, fillColor, lineColor, imageHeight, imageWidth)

   json_file = img_path_180[:-4] + '.json'

   json.dump(data_180, open(json_file, 'w'))

   img_rote_270 = ndimage.rotate(im, 270)

   img_path_270=filename[:-4]+'_270.jpg'

   scipy.misc.imsave(img_path_270,img_rote_270)


   imageData_270 = load_image_file(img_path_270)

   imageData_270 = base64.b64encode(imageData_270).decode('utf-8')

   imageHeight = w

   imageWidth = h

   newshapes = []

   for shapelabel in shapes:

       newLabel = shapelabel['label']

       newline_color = shapelabel['line_color']

       newfill_color = shapelabel['fill_color']

       points = shapelabel['points']

       newPoints = [[h - float(points[1][1]), float(points[0][0])],

                    [h - float(points[0][1]),  float(points[1][0])]]

       newshape_type = shapelabel['shape_type']

       newflags = shapelabel['flags']

       newshapes.append(

           {'label': newLabel, 'line_color': newline_color, 'fill_color': newfill_color, 'points': newPoints,

            'shape_type': newshape_type, 'flags': newflags})

   data_270 = dict_json(flags, imageData_270, newshapes, img_path_270, fillColor, lineColor, imageHeight, imageWidth)

   json_file = img_path_270[:-4] + '.json'

   json.dump(data_270, open(json_file, 'w'))

   print(filename)

img_path = 'USA'  #这个路径是所有图片在的位置

imgs = get_image_paths(img_path)

print (imgs)

for i in imgs:

   create_read_img(i)



目录
相关文章
|
4月前
|
数据挖掘 计算机视觉 Python
Python数据分析中图像处理的实用技术点:图像加载与保存、图像转换与增强、特征提取与描述
Python数据分析中图像处理的实用技术点:图像加载与保存、图像转换与增强、特征提取与描述
44 1
Python数据分析中图像处理的实用技术点:图像加载与保存、图像转换与增强、特征提取与描述
|
7月前
|
编解码 自然语言处理 数据挖掘
Recognize Anything:一个强大的图像标记模型
Recognize Anything是一种新的图像标记基础模型,与传统模型不同,它不依赖于手动注释进行训练
89 2
|
5月前
|
存储 传感器 数据可视化
3D目标检测数据集 KITTI(标签格式解析、3D框可视化、点云转图像、BEV鸟瞰图)
本文介绍在3D目标检测中,理解和使用KITTI 数据集,包括KITTI 的基本情况、下载数据集、标签格式解析、3D框可视化、点云转图像、画BEV鸟瞰图等,并配有实现代码。
445 0
|
3月前
图像表示方法
图像表示方法
17 0
|
3月前
|
机器学习/深度学习 数据处理 文件存储
使用Labelimg进行数据标注
数据标注是计算机视觉和机器学习项目中至关重要的一步,而使用工具进行标注是提高效率的关键。本文介绍了LabelImg,一款常用的开源图像标注工具。用户可以在图像中方便而准确地标注目标区域,为训练机器学习模型提供高质量的标注数据。LabelImg已经成为研究者和开发者在计算机视觉项目中不可或缺的工具之一。
69 0
|
4月前
|
算法 机器人
[3D&Halcon] 3D鞋点胶的点云边界提取
[3D&Halcon] 3D鞋点胶的点云边界提取
213 0
|
4月前
[Halcon&图像] 图像、区域和轮廓相互转化
[Halcon&图像] 图像、区域和轮廓相互转化
80 1
|
4月前
|
C++
[Halcon&定位] 解决Roi区域外的模板匹配成功
[Halcon&定位] 解决Roi区域外的模板匹配成功
60 0
|
9月前
|
数据挖掘
ArcGIS:如何进行栅格数据的拼接和裁剪、坡度坡向的提取、地形透视图的建立、等高线的提取、剖面图的创建?
ArcGIS:如何进行栅格数据的拼接和裁剪、坡度坡向的提取、地形透视图的建立、等高线的提取、剖面图的创建?
257 0
|
10月前
|
机器学习/深度学习 编解码 人工智能
深度学习应用篇-计算机视觉-图像增广1:数据增广、图像混叠、图像剪裁类变化类等详解
深度学习应用篇-计算机视觉-图像增广1:数据增广、图像混叠、图像剪裁类变化类等详解
深度学习应用篇-计算机视觉-图像增广1:数据增广、图像混叠、图像剪裁类变化类等详解