An adaptive gamma correction for image enhancement 低照度图像自适应gamma矫正
找代码没找到,自己用matlab写了一个,算是成功。看图对比,我判断是否是高对比度还是低对比度图像时,一开始用了用了4*sigma<=1/3 %
低对比度;如图1,但是越弄越暗。倒换了一下,
if 4*sigma>=1/3 %
低对比度,这样就对了,如图2。
**论文里是标准差,博主用的是方差。用标准差就正确了。
if 4 * sigma <= 1 / 3:
# 低对比度
附带python代码
流程图如下,流程图并没有细分,明亮和偏暗。
:
return np.where(x > 0, 1, 0)
# 读取RGB图像
rgb_image = cv2.imread('ACE/1/1.jpg') # 替换为你的图像文件路径
# 将RGB图像转换为HSV
hsv_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2HSV)
# 提取V通道
v_channel = hsv_image[:, :, 2]
# 将V通道转换为双精度类型
Iin = v_channel / 255.0
# 获取 Iin 的大小
m, n = Iin.shape
# 打印矩阵的形状
print(f'Iin 的形状是 {m} 行 {n} 列')
# 创建与 Iin 大小相同的全为1的矩阵
one_matrix = np.ones((m, n))
# 计算V通道的均值和方差
mu = np.mean(Iin) # 均值 μ, 判断亮暗
sigma = np.std(Iin) # 方差
# 显示结果
print(f'V通道的均值: {mu}')
print(f'V通道的标准差: {sigma}')
# 判断对比度并计算 Iout
if 4 * sigma <= 1 / 3: # 低对比度
print("低对比度")
gamma = -np.log2(sigma)
k = Iin**gamma + (one_matrix - Iin**gamma) * mu**gamma # 保持按元素运算
c = 1.0 / (1 + heaviside_custom(0.5 - mu) * (k - 1)) # 使用均值来简化
if mu >= 0.5:
print("亮图像")
Iout = c*Iin**gamma # 保持按元素运算
else:
print("暗图像")
Iout = c*(Iin**gamma) / k # 使用均值来简化
else: # 高对比度
print("高对比度")
gamma = np.exp((1 - (mu + sigma)) / 2)
k = Iin**gamma + (one_matrix - Iin**gamma) * mu**gamma # 保持按元素运算
c = 1.0 / (1 + heaviside_custom(0.5 - mu) * (k - 1)) # 使用均值来简化
Iout = c*Iin**gamma # 保持按元素运算
Iout1 = (Iout * 255).astype(np.uint8)
# 合并处理后的 V 通道与原始的 H 和 S 通道
hsv_image[:, :, 2] = Iout1
# 将HSV图像转换回RGB
rgb_output = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
# 显示和保存结果图像
cv2.imshow('原始图像', rgb_image)
cv2.imshow('结果图像', rgb_output)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 保存输出图像
os.makedirs('ACE/2', exist_ok=True) # 确保保存路径存在
cv2.imwrite('ACE/2/Iout.jpg', rgb_output) # 替换为你的保存路径