非线性方式
调整图像的方法有很多,最常用的方法就是对图像像素点的R、G、B三个分量同时进行增加(减少)某个值,达到调整亮度的目的。即改变图像的亮度,实际就是对像素点的各颜色分量值做一个平移。这种方法属于非线性的亮度调整,优点是代码简单、速度快,缺点是在调整亮度的同时,也损失了图像的色彩的纯度。
def adjust_brightness_avg(img, brightness=0.35):
[avg_b, avg_g, avg_r] = np.array(cv2.mean(img))[:-1] / 3
k = np.ones((img.shape))
k[:, :, 0] *= avg_b
k[:, :, 1] *= avg_g
k[:, :, 2] *= avg_r
img = img + brightness * k
# img = img + (1 + brightness) * img
img[img < 0] = 255
img[img > 255] = 255
return img.astype(np.uint8)
HSL颜色空间方式(线性方式)
利用HSL颜色空间,通过只对其L(亮度)部分调整,可达到图像亮度的线性调整。但是RGB和HSL颜色空间的转换很繁琐,一般还需要浮点数的运算,不仅增加了代码的复杂度,更重要的是要逐点将RGB转换为HSL,然后确定新的L值,再将HSL转换为RGB,运行速度慢。要想提高图像线性调整的速度,应该将浮点运算变为整数运算,只提取HSL的L部分进行调整。优点是调整过的图像层次感很强,缺点是代码复杂,调整速度慢,而且当图像亮度增减量较大时有很大的失真。
# def adjust_brightness_rgb(img, brightness=0.35):
# #[0-100]->[-255, 255]
# # brightness = brightness * (255 - (-255)) + (-255)
# brightness = brightness * 255
# print(brightness)
# brightness = -100
# img = img * 1.0
# r = img[:, :, 0]
# g = img[:, :, 1]
# b = img[:, :, 2]
#
# #求出原始图像亮度分量
# l = (img[:, :, 0] + img[:, :, 1] + img[:, :, 2]) / 3.0 + 0.001
#
# mask_1 = l > 128.0
# #利用原始图像的亮度分量结合R, G, B求出HSL空间的H, S;
# rhs = (r * 128.0 - (l - 128.0) * 256.0) / (256.0 - l)
# ghs = (g * 128.0 - (l - 128.0) * 256.0) / (256.0 - l)
# bhs = (b * 128.0 - (l - 128.0) * 256.0) / (256.0 - l)
#
# rhs = rhs * mask_1 + (r * 128.0 / l) * (1 - mask_1)
# ghs = ghs * mask_1 + (g * 128.0 / l) * (1 - mask_1)
# bhs = bhs * mask_1 + (b * 128.0 / l) * (1 - mask_1)
# #然后求出新的亮度值 亮度的调整增量(-255,255)
# l_new = l + brightness - 128.0
# #再利用新的亮度值结合H,S,求出新的R,G,B分量
# mask_2 = l_new > 0.0
#
# r_new = rhs + (256.0 - rhs) * l_new / 128.0
# g_new = ghs + (256.0 - ghs) * l_new / 128.0
# b_new = bhs + (256.0 - bhs) * l_new / 128.0
#
# r_new = r_new * mask_2 + (rhs + rhs * l_new / 128.0) * (1 - mask_2)
# g_new = g_new * mask_2 + (ghs + ghs * l_new / 128.0) * (1 - mask_2)
# b_new = b_new * mask_2 + (bhs + bhs * l_new / 128.0) * (1 - mask_2)
#
# img_out = img * 1.0
#
# img_out[:, :, 0] = r_new
# img_out[:, :, 1] = g_new
# img_out[:, :, 2] = b_new
#
# img_out = img_out / 255.0
#
# # 饱和处理
# mask_3 = img_out < 0
# mask_4 = img_out > 1
#
# img_out = img_out * (1 - mask_3)
# img_out = img_out * (1 - mask_4) + mask_4
#
# return img_out
def adjust_brightness_hls(img, brightness):#img is [0-1]
img = img.astype(np.float32) / 255.0
# BGR2HLS
img_hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
# adjust light(linear transform)
img_hls[:, :, 1] = (1.0 + 0.35) * img_hls[:, :, 1]
img_hls[:, :, 1][img_hls[:, :, 1] > 1] = 1
# #adjust saturation
# img_hls[:, :, 2] = (1.0 + 0.2) * img_hls[:, :, 2]
# img_hls[:, :, 2][img_hls[:, :, 2] > 1] = 1
# HLS2BGR
img_ls = cv2.cvtColor(img_hls, cv2.COLOR_HLS2BGR) * 255
# img_ls = np.clip(img_ls, 0, 255).astype(np.uint8)
return img_ls
alpha合成方式(线性方式)
def adjust_brightness_linear(img, brightness):#brightness arange [-1, 1]
if brightness <= 0:
img_out = img * (1 - brightness) + brightness * 255
else:
img_out = img * (1 + brightness) + brightness * 0
return img_out
亮度和对比度同时调整
总结
import cv2
import sys
import numpy as np
import matplotlib.pyplot as plt
"""
基于RGB空间亮度调整算法:
主要是对RGB空间进行亮度调整。计算出调整系数后,调整手段主要有两种:
1) 基于当前RGB值大小进行调整,即R、G、B值越大,调整的越大,
例如:当前像素点为(100,200,50),调整系数1.1,则调整后为(110,220,55);
2) 不考虑RGB值大小的影响,即始终对各个点R、G、B值进行相同的调整,
例如:当前像素点为(100,200,50),调整系数10/255,则调整后为(110,210,60)。
"""
def RGBAlgorithm(rgb_img, value=0.5, basedOnCurrentValue=True):
img = rgb_img * 1.0
img_out = img
# 基于当前RGB进行调整(RGB*alpha)
if basedOnCurrentValue:
# 增量大于0,指数调整
if value >= 0 :
alpha = 1 - value
alpha = 1/alpha
# 增量小于0,线性调整
else:
alpha = value + 1
img_out[:, :, 0] = img[:, :, 0] * alpha
img_out[:, :, 1] = img[:, :, 1] * alpha
img_out[:, :, 2] = img[:, :, 2] * alpha
# 独立于当前RGB进行调整(RGB+alpha*255)
else:
alpha = value
img_out[:, :, 0] = img[:, :, 0] + 255.0 * alpha
img_out[:, :, 1] = img[:, :, 1] + 255.0 * alpha
img_out[:, :, 2] = img[:, :, 2] + 255.0 * alpha
img_out = img_out/255.0
# RGB颜色上下限处理(小于0取0,大于1取1)
mask_3 = img_out < 0
mask_4 = img_out > 1
img_out = img_out * (1-mask_3)
img_out = img_out * (1-mask_4) + mask_4
return img_out
"""
基于HSV空间亮度调整算法:
主要是对HSV空间的亮度V值进行调整。计算出调整系数后,调整手段主要有两种:
1) 基于当前V值大小进行调整,即V值越大,调整的越大,
例如:当前像素点V值为200,调整系数1.1,则调整后为220;
2) 不考虑V值大小的影响,即始终对各个V值进行相同的调整,
例如:当前像素点V值为200,调整系数10/255,则调整后为210。
"""
def HSVAlgorithm(rgb_img, value=0.5, basedOnCurrentValue=True):
hsv_img = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2HSV)
img = hsv_img * 1.0
img_out = img
# 基于当前亮度进行调整(V*alpha)
if basedOnCurrentValue:
# 增量大于0,指数调整
if value >= 0 :
alpha = 1 - value
alpha = 1/alpha
# 增量小于0,线性调整
else:
alpha = value + 1
img_out[:, :, 2] = img[:, :, 2] * alpha
else :
alpha = value
img_out[:, :, 2] = img[:, :, 2] + 255.0 * alpha
# HSV亮度上下限处理(小于0取0,大于1取1)
img_out = img_out/255.0
mask_1 = img_out < 0
mask_2 = img_out > 1
img_out = img_out * (1-mask_1)
img_out = img_out * (1-mask_2) + mask_2
img_out = img_out * 255.0
# HSV转RGB
img_out = np.round(img_out).astype(np.uint8)
img_out = cv2.cvtColor(img_out, cv2.COLOR_HSV2RGB)
img_out = img_out/255.0
return img_out
path = './resource/fruit.bmp'
value = 0.3 # 范围-1至1
basedOnCurrentValue = True # 0或者1
# run : python Lightness.py (path) (value) (basedOnCurrentValue)
if __name__ == "__main__":
len = len(sys.argv)
if len >= 2 :
path = sys.argv[1]
if len >= 3 :
value = float(sys.argv[2])
if len >= 4 :
basedOnCurrentValue = bool(int(sys.argv[3]))
img = cv2.imread(path)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img_rgb = RGBAlgorithm(img, value, basedOnCurrentValue)
img_hsv = HSVAlgorithm(img, value, basedOnCurrentValue)
plt.figure("img_original")
plt.imshow(img/255.0)
plt.axis('off')
plt.figure("img_light_rgb")
plt.imshow(img_rgb)
plt.axis('off')
plt.figure("img_light_hsv")
plt.imshow(img_hsv)
plt.axis('off')
plt.show()
参考资料
GDI+ 在Delphi程序的应用 – 调整图像亮度
GDI+ 在Delphi程序的应用 – ColorMatrix与图像亮度
Python实现PS图像明亮度调整效果示例(python调节图片亮度)
OpenCV图像处理|1.7 调整图像亮度与对比度
改进的图像线性亮度调整方法
OpenCV 基于RGB三原色的基本线性变换 改变图像颜色和亮度 对比度增强算法
图像处理——亮度调整算法(python语言)
图像处理——对比度调整算法(python语言)