1,详细信息 https://github.com/jack16888/imgaug color_brightness.py 要联合其他脚本使用。
2,该github中还有另外一种图像增强方法,请看github介绍imgaug。
# coding=utf-8
import os
import os
import cv2
import math
import numpy as np
import shutil
from PIL import Image
from PIL import ImageEnhance
"""
1、对比度:白色画面(最亮时)下的亮度除以黑色画面(最暗时)下的亮度;
2、色彩饱和度::彩度除以明度,指色彩的鲜艳程度,也称色彩的纯度;
3、色调:向负方向调节会显现红色,正方向调节则增加黄色。适合对肤色对象进行微调;
4、锐度:是反映图像平面清晰度和图像边缘锐利程度的一个指标。
"""
def compute(img):
per_image_Rmean = []
per_image_Gmean = []
per_image_Bmean = []
per_image_Bmean.append(np.mean(img[:, :, 0]))
per_image_Gmean.append(np.mean(img[:, :, 1]))
per_image_Rmean.append(np.mean(img[:, :, 2]))
R_mean = np.mean(per_image_Rmean)
G_mean = np.mean(per_image_Gmean)
B_mean = np.mean(per_image_Bmean)
return math.sqrt(0.241 * (R_mean ** 2) + 0.691 * (G_mean ** 2) + 0.068 * (B_mean ** 2))
def fun_color(image, coefficient, path_save):
# 色度,增强因子为1.0是原始图像
# 色度增强 1.5
# 色度减弱 0.8
enh_col = ImageEnhance.Color(image)
image_colored1 = enh_col.enhance(coefficient)
image_colored1.save(path_save)
def fun_Contrast(image, coefficient, path_save):
# 对比度,增强因子为1.0是原始图片
# 对比度增强 1.5
# 对比度减弱 0.8
enh_con = ImageEnhance.Contrast(image)
image_contrasted1 = enh_con.enhance(coefficient)
image_contrasted1.save(path_save)
def fun_Sharpness(image, coefficient, path_save):
# 锐度,增强因子为1.0是原始图片
# 锐度增强 3
# 锐度减弱 0.8
enh_sha = ImageEnhance.Sharpness(image)
image_sharped1 = enh_sha.enhance(coefficient)
image_sharped1.save(path_save)
def fun_bright(image, coefficient, path_save):
# 变亮 1.5
# 变暗 0.8
# 亮度增强,增强因子为0.0将产生黑色图像; 为1.0将保持原始图像。
enh_bri = ImageEnhance.Brightness(image)
image_brightened1 = enh_bri.enhance(coefficient)
image_brightened1.save(path_save)
def show_all():
file_root = "/home/zhangwanchun/data/VOCdevkit/VOC2007_aug/JPEGImages/"
xml_root = "/home/zhangwanchun/data/VOCdevkit/VOC2007_aug/Annotations/"
save_root = "/home/zhangwanchun/data/VOCdevkit/VOC2007_aug/save/"
xml_save = "/home/zhangwanchun/data/VOCdevkit/VOC2007_aug/xml/"
list_file = os.listdir(file_root)
cnt = 0
for img_name in list_file:
cnt += 1
print("cnt=%d,img_name=%s" % (cnt, img_name))
path = file_root + img_name
name = img_name.replace(".jpg", "")
image = Image.open(path)
list_coe = [0.5,1,3]
for val in list_coe:
path_save_bright = save_root + name + "_bri_" + str(val) + ".jpg"
fun_bright(image, val, path_save_bright)
path_save_color = save_root + name + "_color_" + str(val) + ".jpg"
fun_color(image, val, path_save_color)
path_save_contra = save_root + name + "_contra_" + str(val) + ".jpg"
fun_Contrast(image, val, path_save_contra)
path_save_sharp = save_root + name + "_sharp_" + str(val) + ".jpg"
fun_Sharpness(image, val, path_save_sharp)
def my_aug():
file_root = "/home/zhangwanchun/data/VOCdevkit/VOC2007_aug/JPEGImages/"
save_root = "/home/zhangwanchun/data/VOCdevkit/VOC2007_aug/save/"
xml_save = "/home/zhangwanchun/data/VOCdevkit/VOC2007_aug/xml/"
xml_root = "/home/zhangwanchun/data/VOCdevkit/VOC2007_aug/Annotations/"
list_file = os.listdir(file_root)
cnt = 0
for img_name in list_file:
cnt += 1
print("cnt=%d,img_name=%s" % (cnt, img_name))
path = file_root + img_name
name = img_name.replace(".jpg", "")
image = Image.open(path)
img = cv2.imread(path)
mean_1 = compute(img)
cof = 0.0
if mean_1 <40:
cof &#61; 3.5
elif mean_1 <60:
cof &#61; 3
elif mean_1 <80:
cof &#61; 2
elif mean_1 <90:
cof &#61; 1.5
elif mean_1 <110:
cof &#61; 1.1
elif mean_1 > 130:
cof &#61; 0.5
else:
cof &#61; 0.75
cof_contrast &#61; 0.0
if cof>1:
cof_contrast &#61; 1.5
else:
cof_contrast &#61; 0.8
xmlpath &#61; xml_root &#43; name &#43; &#39;.xml&#39;
path_save_bright &#61; save_root &#43; name &#43; "_bri_" &#43; str(cof) &#43; &#39;.jpg&#39;
path_save_bright_xml &#61; xml_save &#43; name &#43; "_bri_" &#43; str(cof) &#43; &#39;.jpg&#39;
shutil.copy(xmlpath,path_save_bright_xml.replace(".jpg", ".xml"))
fun_bright(image, cof, path_save_bright)
path_save_sharp &#61; save_root &#43; name &#43; "_sharp_" &#43; str(2) &#43; &#39;.jpg&#39;
path_save_sharp_xml &#61; xml_save &#43; name &#43; "_sharp_" &#43; str(2) &#43; &#39;.jpg&#39;
shutil.copy(xmlpath, path_save_sharp_xml.replace(".jpg", ".xml"))
fun_Sharpness(image, 2, path_save_sharp)
path_save_contra &#61; save_root &#43; name &#43; "_contra_" &#43; str(cof_contrast) &#43; ".jpg"
path_save_contra_xml &#61; xml_save &#43; name &#43; "_contra_" &#43; str(cof_contrast) &#43; ".jpg"
shutil.copy(xmlpath, path_save_contra_xml.replace(".jpg", ".xml"))
fun_Contrast(image, cof_contrast, path_save_contra)
path_save_color &#61; save_root &#43; name &#43; "_color_" &#43; str(1.5) &#43; ".jpg"
path_save_color_xml &#61; xml_save &#43; name &#43; "_color_" &#43; str(1.5) &#43; ".jpg"
shutil.copy(xmlpath,path_save_color_xml.replace(".jpg", ".xml"))
fun_color(image, 1.5, path_save_color)
if __name__ &#61;&#61; "__main__":
#show_all()
my_aug()