9、Python对Labeme数据集进行扩充--数据裁剪及坐标对应映射

针对labelme标注的图片样本不足的问题,通过Python实现对已有图片的随机裁剪,同时生成相应的json标注文件,以此扩充数据集。

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

问题描述:使用labelme标注的picture和json 总数量太少(即样本太少,无法满足训练的要求),因此预先对已经标注的图片进行随机裁剪和创建对应的json串,以增加数据量;

# -*- coding: utf-8 -*-
import os
import sys
import json
import io
import random
import re
from PIL import Image

source_path = r'F:\sxj\20210518\result'
destination_path = r'F:\sxj\20210518\crop'
n = 1  # 每一张图片需要裁剪的次数

article_info = {}
data_json = json.loads(json.dumps(article_info))
data_json['version'] = '3.6.16'
data_json['flags'] = {}

data_json["lineColor"] = [
    0,
    255,
    0,
    128
]
data_json["fillColor"] = [
    255,
    0,
    0,
    128
]


def file_name(file_dir):
    L = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == '.json':
                L.append
### LabelMe手动打标签后的数据增强方法 在完成LabelMe的手动标注后,可以通过多种方式对已有的数据集进行增强。以下是具体的方法技术: #### 数据增强的意义 数据增强是一种通过变换现有数据来生成新样本的技术[^2]。它不仅能够扩充训练数据的数量,还能有效减少过拟合的风险并提升模型的泛化能力。 #### 常见的数据增强方法 1. **几何变换** 几何变换是最常用的一种数据增强手段,主要包括平移(translation)、旋转(rotation)、翻转(flip)缩放(scaling)。 ```python import cv2 import numpy as np # 加载图片 image = cv2.imread('image.jpg') # 平移操作 rows, cols = image.shape[:2] M_translation = np.float32([[1, 0, 50], [0, 1, 30]]) translated_image = cv2.warpAffine(image, M_translation, (cols, rows)) # 旋转操作 center = (cols / 2, rows / 2) angle = 45 scale = 1.0 M_rotation = cv2.getRotationMatrix2D(center, angle, scale) rotated_image = cv2.warpAffine(image, M_rotation, (cols, rows)) # 翻转操作 flipped_image_horizontal = cv2.flip(image, 1) # 水平翻转 flipped_image_vertical = cv2.flip(image, 0) # 垂直翻转 ``` 2. **颜色空间转换** 颜色空间的变化也是重要的增强策略之一,比如调整亮度(brightness)、对比度(contrast)、饱度(saturation),以及改变色调(hue)。 ```python from PIL import ImageEnhance, Image img = Image.open('image.jpg') enhancer_brightness = ImageEnhance.Brightness(img) enhanced_img_bright = enhancer_brightness.enhance(1.5) # 提高亮度 enhancer_contrast = ImageEnhance.Contrast(img) enhanced_img_contrast = enhancer_contrast.enhance(1.8) # 调整对比度 ``` 3. **裁剪(cropping)** 填充(padding) 对图像的不同部分进行随机裁剪或扩展边界可以模拟不同的视角效果。 ```python cropped_image = image[50:150, 50:150] # 定义裁剪区域 padded_image = cv2.copyMakeBorder(image, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=[0, 0, 0]) ``` 4. **噪声添加(noise addition)** 向原始图像中加入一定量的随机噪声可以帮助网络学习到更鲁棒的特征表示。 ```python noise = np.random.normal(loc=0, scale=1, size=image.shape).astype(np.uint8) noisy_image = cv2.add(image, noise) ``` 5. **透视变换(perspective transformation)** 这种变化能模仿相机角度的变化,从而进一步丰富数据多样性。 ```python pts1 = np.float32([[50, 50], [200, 50], [50, 200]]) pts2 = np.float32([[10, 100], [200, 50], [100, 250]]) M_perspective = cv2.getPerspectiveTransform(pts1, pts2) perspective_transformed_image = cv2.warpPerspective(image, M_perspective, (300, 300)) ``` 以上提到的各种方法都可以单独使用也可以组合起来形成更加复杂的增强方案。对于已经由LabelMe工具标记好的数据集来说,在实施上述任何一种增强之前都需要确保相应的标注信息也同步更新以保持一致性[^1]。 ### JSON文件处理注意事项 当利用LabelMe生成JSON格式的标注文件时,如果进行了任何形式的数据增强,则需要相应修改这些JSON中的坐标位置或其他属性值以便它们继续准确描述经过变换之后的对象状态。 ```python import json def update_json(json_file_path, new_coords): with open(json_file_path, 'r') as f: data = json.load(f) shapes = data['shapes'] for shape in shapes: points = shape["points"] updated_points = [] for point in points: x_new = new_coords[point][0] y_new = new_coords[point][1] updated_point = [x_new, y_new] updated_points.append(updated_point) shape["points"] = updated_points with open(json_file_path, 'w') as file: json.dump(data, file, indent=4) ``` 此脚本展示了如何读取并编辑一个标准LabelMe输出的JSON文件的内容,其中`new_coords`应该包含所有旧点映射至其对应的新坐标的字典。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sxj731533730

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值