语义分割之图像增强工具

# -*- coding:utf-8 -*-
"""数据增强
   1. 翻转变换 flip
   2. 随机修剪 random crop
   3. 色彩抖动 color jittering
   4. 平移变换 shift
   5. 尺度变换 scale
   6. 对比度变换 contrast
   7. 噪声扰动 noise
   8. 旋转变换/反射变换 Rotation/reflection
"""
 
from PIL import Image, ImageEnhance, ImageOps, ImageFile
import numpy as np
import random
import threading, os, time
import logging
import json
import shutil
import glob
logger = logging.getLogger(__name__)
ImageFile.LOAD_TRUNCATED_IMAGES = True
 
 
class DataAugmentation:
    """
    包含数据增强的八种方式
    """
 
 
    def __init__(self):
        pass
 
    @staticmethod
    def openImage(image):
        return Image.open(image, mode="r")


    @staticmethod
    def randomRotation(image, label, mode=Image.BICUBIC):
        """
         对图像进行随机任意角度(0~360度)旋转
        :param mode 邻近插值,双线性插值,双三次B样条插值(default)
        :param image PIL的图像image
        :return: 旋转转之后的图像
        """
        random_angle = np.random.randint(1, 360)
        return image.rotate(random_angle, mode) , label.rotate(random_angle, Image.NEAREST)
 
    #暂时未使用这个函数
    @staticmethod
    def randomCrop(image, label):
        """
        对图像随意剪切,考虑到图像大小范围(68,68),使用一个一个大于(36*36)的窗口进行截图
        :param image: PIL的图像image
        :return: 剪切之后的图像
        """
        image_width = image.size[0]
        image_height = image.size[1]
        crop_win_size = np.random.randint(40, 68)
        random_region = (
            (image_width - crop_win_size) >> 1, (image_height - crop_win_size) >> 1, (image_width + crop_win_size) >> 1,
            (image_height + crop_win_size) >> 1)
        return image.crop(random_region), label
 
    @staticmethod
    def randomColor(image, label):
        """
        对图像进行颜色抖动
        :param image: PIL的图像image
        :return: 有颜色色差的图像image
        """
        random_factor = np.random.randint(0, 31) / 10.  # 随机因子
        color_image = ImageEnhance.Color(image).enhance(random_factor)  # 调整图像的饱和度
        random_factor = np.random.randint(10, 21) / 10.  # 随机因子
        brightness_image = ImageEnhance.Brightness(color_image).enhance(random_factor)  # 调整图像的亮度
        random_factor = np.random.randint(10, 21) / 10.  # 随机因1子
        contrast_image = ImageEnhance.Contrast(brightness_image).enhance(random_factor)  # 调整图像对比度
        random_factor = np.random.randint(0, 31) / 10.  # 随机因子
        return ImageEnhance.Sharpness(contrast_image).enhance(random_factor) ,label # 调整图像锐度
 
    @staticmethod
    def randomGaussian(image, label, mean=0.2, sigma=0.3):
        """
         对图像进行高斯噪声处理
        :param image:
        :return:
        """
 
        def gaussianNoisy(im, mean=0.2, sigma=0.3):
            """
            对图像做高斯噪音处理
            :param im: 单通道图像
            :param mean: 偏移量
            :param sigma: 标准差
            :return:
            """
            for _i in range(len(im)):
                im[_i] += random.gauss(mean, sigma)
            return im
 
        # 将图像转化成数组
        img = np.asarray(image)
        img.flags.writeable = True  # 将数组改为读写模式
        width, height = img.shape[:2]
        img_r = gaussianNoisy(img[:, :, 0].flatten(), mean, sigma)
        img_g = gaussianNoisy(img[:, :, 1].flatten(), mean, sigma)
        img_b = gaussianNoisy(img[:, :, 2].flatten(), mean, sigma)
        img[:, :, 0] = img_r.reshape([width, height])
        img[:, :, 1] = img_g.reshape([width, height])
        img[:, :, 2] = img_b.reshape([width, height])
        return Image.fromarray(np.uint8(img)), label
 
    @staticmethod
    def saveImage(image, path):
        # print("image",image)
        # print ("path",path)
        image.save(path)
    @staticmethod
    def saveJson(json_file, path):
        # print("json_file",json_file)
        # print ("path",path)
        with open(json_file, 'w') as f:
            json.dump(json_file, f)  
 
def makeDir(path):
    try:
        if not os.path.exists(path):
            if not os.path.isfile(path):
                # os.mkdir(path)
                os.makedirs(path)
            return 0
        else:
            return 1
    except Exception as ex:
        # print str(ex)
        return -2
 
 
def imageOps(func_name, image, label, img_des_path, label_des_path , img_file_name, label_file_name, times=5):
    funcMap = {"randomRotation": DataAugmentation.randomRotation,
               "randomCrop": DataAugmentation.randomCrop,
               "randomColor": DataAugmentation.randomColor,
               "randomGaussian": DataAugmentation.randomGaussian
               }
    if funcMap.get(func_name) is None:
        logger.error("%s is not exist", func_name)
        return -1
 
    for _i in range(0, times, 1):
        new_image , new_label = funcMap[func_name](image,label)
        DataAugmentation.saveImage(new_image, os.path.join(img_des_path, func_name + str(_i) + img_file_name))
        shutil.copy(new_label, os.path.join(label_des_path, func_name + str(_i) + label_file_name))
 
 
opsList = {  "randomColor", "randomGaussian"}
 
 
def threadOPS(img_path, new_img_path, label_path, new_label_path):
    """
    多线程处理事务
    :param src_path: 资源文件
    :param des_path: 目的地文件
    :return:
    """
    #img path 
    img_names = glob.glob(img_path + "/*.jpg", recursive=True)

 
    #label path 
    label_names = glob.glob(label_path + "/*.json", recursive=True)

    img_num = 0
    label_num = 0
    img_num = len(img_names)
    label_num = len(label_names)

    if img_num != label_num:
        # print('the num of img and label is not equl')
        exit()
    else: 
        num = img_num
 
 
    for i in range(num):
        img_name = img_names[i]
        # print (img_name)
        label_name = label_names[i]
        print (label_name)
 
        tmp_img_name = os.path.join(img_path, img_name)
        tmp_label_name = os.path.join(label_path, label_name)
        print(tmp_label_name)
        # 读取文件并进行操作
        image = DataAugmentation.openImage(tmp_img_name)
        # label = DataAugmentation.openJson(tmp_label_name)
        # print(image)
        # print(label)
        print(new_img_path)
        print(new_label_path)
        print(img_name)
        print(label_name)
         
        # print(os.path.split(img_name)[-1])
        # print(os.path.split(label_name)[-1])
        for ops_name in opsList:
            imageOps(ops_name, image, label_name, new_img_path, new_label_path , os.path.split(img_name)[-1], os.path.split(label_name)[-1], times=5)
        # threadImage = [0] * 5
        # _index = 0
        # for ops_name in opsList:
        #     threadImage[_index] = threading.Thread(target=imageOps,
        #                                         args=(ops_name, image, label_name, new_img_path, new_label_path,  os.path.split(img_name)[-1], os.path.split(label_name)[-1]))
        #     threadImage[_index].start()
        #     _index += 1
        #     time.sleep(0.2)
 
 
if __name__ == '__main__':
    threadOPS(r"F:\workspace\code\pspnet-keras-master\datasets\before",
              r"F:\workspace\code\pspnet-keras-master\datasets\before1",
              r"F:\workspace\code\pspnet-keras-master\datasets\before2",
              r"F:\workspace\code\pspnet-keras-master\datasets\before3")

            # 第一个test1是图像所在的路径
            # 第二个test1是数据增强后保存的路径
            # 第一个test2是标签所在的路径
            # 第二个test2是数据增强后保存的路径"

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
语义分割图像增强算法是为了丰富图像训练集,提取图像特征和泛化模型而设计的。常见的图像增强方式包括旋转图像、剪切图像、改变图像色差、扭曲图像特征、改变图像尺寸大小和增强图像噪音等。然而,对于语义分割任务,需要同时对原始图像和掩码图进行增强,因此现有的深度学习框架自带的图像增强工具并不能直接使用。但是可以使用Augmentor工具库来实现该功能。通过定义图像的旋转、翻转、缩放等操作,并指定执行的概率和参数,可以在训练集中生成更多的增强样本。例如,可以使用以下Python代码对图像进行数据增强: ``` import Augmentor p = Augmentor.Pipeline("test1") p.ground_truth("test2") p.rotate(probability=0.8, max_left_rotation=10, max_right_rotation=10) p.flip_left_right(probability=0.5) p.zoom_random(probability=0.3, percentage_area=0.85) p.sample(20) ``` 这段代码使用Augmentor库导入数据增强工具并设置图像存储路径和掩码文件存储路径。然后通过调用旋转、翻转和缩放等函数来增强图像,可以根据需要设置执行的概率和参数。最后,调用sample函数生成扩充后的数据样本。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [结合图像语义分割增强现实型平视显示系统设计与研究](https://download.csdn.net/download/weixin_38595473/14937134)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [语义分割图像增强新方法](https://blog.csdn.net/niuxuerui11/article/details/114866772)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值