• Python_code_使用ImageFilter库对图片实现数据增强


    Python_code_使用ImageFilter库对图片实现数据增强_28种

    代码:

    import os
    import glob
    from PIL import Image
    from PIL import ImageFilter
    from PIL import ImageEnhance
    import matplotlib.pyplot as plt
    #以下两行实现了在plt画图时,可以输出中文字符
    plt.rcParams['font.sans-serif']=['SimHei']
    plt.rcParams['axes.unicode_minus'] = False


    #对图片进行数据增强
    def Image_Enhancement_img(img):
    print(img) #输出图片的路径
    #读取图像
    im = Image.open(img)

    #显示图像
    #plt.figure()
    #plt.subplot(121),plt.title("原图"),plt.imshow(im),plt.xticks([]),plt.yticks([]);

    #1. 对图像进行颜色通道的变换(5种;1-5)
    r,g,b=im.split() #获得RGB通道数据

    #rbg
    om = Image.merge("RGB",(r,b,g)) #合并通道,参数:(色彩,新的色彩通道)
    new_filename = img[:-4] + '_rbg.png' #print(new_filename)
    om.save(new_filename)
    #plt.subplot(122),plt.title("rbg"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    #gbr
    om = Image.merge("RGB",(g,b,r))
    new_filename = img[:-4] + '_gbr.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("gbr"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    #grb
    om = Image.merge("RGB",(g,r,b))
    new_filename = img[:-4] + '_0.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("grb"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    #bgr
    om = Image.merge("RGB",(b,g,r))
    new_filename = img[:-4] + '_1.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("bgr"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    #brg
    om = Image.merge("RGB",(b,r,g))
    new_filename = img[:-4] + '_2.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("brg"),plt.imshow(om),plt.xticks([]),plt.yticks([]);


    #第二部分:非常规的数据增强
    om = im.filter(ImageFilter.DETAIL)
    new_filename = img[:-4] + '_细节.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("细节"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    om = im.filter(ImageFilter.SMOOTH)
    new_filename = img[:-4] + '_平滑.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("平滑"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    om = im.filter(ImageFilter.SMOOTH_MORE)
    new_filename = img[:-4] + '_阈值平滑.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("阈值平滑"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    #后面带参数的锐度调整重复了
    om = im.filter(ImageFilter.SHARPEN)
    new_filename = img[:-4] + '_锐化.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("锐化"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    om = im.filter(ImageFilter.BLUR)
    new_filename = img[:-4] + '_3.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("模糊"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    om = im.filter(ImageFilter.CONTOUR) #图像的轮廓效果
    new_filename = img[:-4] + '_4.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("轮廓"),plt.imshow(om),plt.xticks([]),plt.yticks([]);


    om = im.filter(ImageFilter.FIND_EDGES)
    new_filename = img[:-4] + '_5.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("边界"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    om = im.filter(ImageFilter.EDGE_ENHANCE)
    new_filename = img[:-4] + '_6.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("边界加强"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    om = im.filter(ImageFilter.EDGE_ENHANCE_MORE)
    new_filename = img[:-4] + '_7.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("阈值边界加强"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    om = im.filter(ImageFilter.EMBOSS)
    new_filename = img[:-4] + '_8.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("浮雕"),plt.imshow(om),plt.xticks([]),plt.yticks([]);


    #第三部分:调整图像的颜色平衡,0是灰度图像,1是原图(建议在-13~13的范围内;-11.52
    om = ImageEnhance.Color(im).enhance(-1)
    new_filename = img[:-4] + '_9.png'
    om.save(new_filename)
    #plt.subplot(122),plt.title("色彩平衡调整_参数2"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    om = ImageEnhance.Color(im).enhance(1.5)
    new_filename = img[:-4] + '_10.png'
    om.save(new_filename)

    om = ImageEnhance.Color(im).enhance(2)
    new_filename = img[:-4] + '_11.png'
    om.save(new_filename)



      #第四部分:调整图像的亮度,enhance 中的参数可变(建议在0.3~2的范围内;0.30.71.5
    om = ImageEnhance.Brightness(im).enhance(0.3)
    new_filename = img[:-4] + '_12.png'
    om.save(new_filename)
      #plt.subplot(122),plt.title("图像亮度调整_参数1.3"),plt.imshow(om),plt.xticks([]),plt.yticks([]);

    om = ImageEnhance.Brightness(im).enhance(0.7)
    new_filename = img[:-4] + '_13.png'
    om.save(new_filename)

    om = ImageEnhance.Brightness(im).enhance(1.5)
    new_filename = img[:-4] + '_14.png'
    om.save(new_filename)


      # 第五部分:调整图像的对比度,enhance 中的参数可变(建议在-2~4的范围内;-2-10.51.5
    om = ImageEnhance.Contrast(im).enhance(-2)
    new_filename = img[:-4] + '_15.png'
    om.save(new_filename)
      # plt.subplot(122), plt.title("图像对比度调整_参数1.5"), plt.imshow(om), plt.xticks([]), plt.yticks([]);

    om = ImageEnhance.Contrast(im).enhance(-1)
    new_filename = img[:-4] + '_16.png'
    om.save(new_filename)

    om = ImageEnhance.Contrast(im).enhance(0.5)
    new_filename = img[:-4] + '_17.png'
    om.save(new_filename)

    om = ImageEnhance.Contrast(im).enhance(1.5)
    new_filename = img[:-4] + '_18.png'
    om.save(new_filename)


      # 第六部分:调整图像的锐度,锐度为0是原图,参数为负是感觉和模糊效果差不多(建议选3930
    om = ImageEnhance.Sharpness(im).enhance(3)
    new_filename = img[:-4] + '_19.png'
    om.save(new_filename)
      #plt.subplot(122), plt.title("图像锐度调整_参数30"), plt.imshow(om), plt.xticks([]), plt.yticks([]);

    om = ImageEnhance.Sharpness(im).enhance(9)
    new_filename = img[:-4] + '_20.png'
    om.save(new_filename)

    om = ImageEnhance.Sharpness(im).enhance(30)
    new_filename = img[:-4] + '_21.png'
    om.save(new_filename)

      #plt.show()

    '''主函数部分:进行函数调用'''
    #图片数据所在的根目录
    dataset_dir = '..\data'


    for root, dirs, files in os.walk(dataset_dir):
      '''
    root 所指的是当前正在遍历的这个文件夹的本身的地址
    dirs 是一个 list ,内容是该文件夹中所有的目录的名字(不包括子目录)
    files 同样是 list , 内容是该文件夹中所有的文件(不包括子目录)
    '''

      for sDir in dirs:
      # 当前文件夹下子目录的绝对路径
    imgs_list = glob.glob(os.path.join(root, sDir) + '/*.png')
    imgs_list_xml = glob.glob(os.path.join(root, sDir) + '/*.xml')
      #print(imgs_list)

    #处理png图片
      for i in imgs_list:
        #print(i)
        if '_' in i or 'png' not in i:
      continue
    Image_Enhancement_img(i)
  • 相关阅读:
    java并发:简单面试问题集锦
    Java:泛型
    Java:注解(元数据)
    Java:反射
    Java:静态代理 and 动态代理
    华为机试题——该警醒了,骚年
    java并发:线程同步机制之Lock
    java并发:中断一个正在运行的线程
    java中String类型变量的赋值问题
    java中的自增问题
  • 原文地址:https://www.cnblogs.com/lyj0123/p/11245592.html
Copyright © 2020-2023  润新知