图片增强组件实现

设计并实现了一个图片增强的组件,具体功能如下:

  • 图片数据增强,包括且不限于:图片旋转、比例增强、高斯噪声、饱和度变换等
  • 若图片包含对应标注boundingbox,也支持对应变换,保证圈选内容的不变性
  • 实现多种方式random组合,增加增强效果

具体依赖

import cv2
import numpy as np
import copy
import random
from PIL import Image
import os

数据增强方法

@classmethod
def resize_image(cls, image, mode: str = "img", notes: [[dict]] = None, scale_percent: int = None):
    if scale_percent is None:
        scale_percent = random.randrange(30, 80)
    width = int(image.shape[1] * scale_percent / 100)
    height = int(image.shape[0] * scale_percent / 100)
    resized_image = cv2.resize(image, (width, height))
    if mode == 'notes' and notes is None:
        raise ValueError("When mode is set to 'notes', you must provide notes data.")
    elif mode == 'notes':
        points = [i.get('points') for i in notes]
        resized_points = []
        for box in points:
            resized_box = []
            for point in box:
                x = int(point[0] * scale_percent / 100)
                y = int(point[1] * scale_percent / 100)
                resized_box.append([x, y])
            resized_points.append(resized_box)

        resized_notes = copy.deepcopy(notes)
        for i in range(len(resized_notes)):
            resized_notes[i]['points'] = resized_points[i]

        return resized_image, resized_notes
    elif mode == 'img':
        return resized_image
@classmethod
def add_gaussian_noise(cls, image, mode: str = "img", notes: [[dict]] = None, mean=70, std=20):
    noise = np.random.normal(mean, std, image.shape).astype(np.uint8)
    noisy_image = cv2.add(image, noise)
    if mode == 'notes' and notes is None:
        raise ValueError("When mode is set to 'notes', you must provide notes data.")
    elif mode == 'notes':
        gaussian_notes = copy.deepcopy(notes)
        return noisy_image, gaussian_notes
    elif mode == 'img':
        return noisy_image

完整代码

source

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值