自动白平衡(AWB,Automatic White Balance)算法用于调整图像的色彩平衡,使图像中的白色看起来是真正的白色。常见的方法包括灰度世界假设(Gray World Assumption)和完美反射假设(Perfect Reflector Assumption)。本文通过对比只使用灰度世界、只使用完美反射,以及结合两种假设的方法,来展示它们各自的效果。
import cv2
import numpy as np
import matplotlib.pyplot as plt
def gray_world_awb(img):
img = img.astype(np.float32)
avg_r = np.mean(img[:, :, 2])
avg_g = np.mean(img[:, :, 1])
avg_b = np.mean(img[:, :, 0])
avg_gray = (avg_r + avg_g + avg_b) / 3
gain_r = avg_gray / avg_r
gain_g = avg_gray / avg_g
gain_b = avg_gray / avg_b
img[:, :, 2] *= gain_r
img[:, :, 1] *= gain_g
img[:, :, 0] *= gain_b
img = np.clip(img, 0, 255).astype(np.uint8)
return img
def perfect_reflector_awb(img, percentile=95):
img = img.astype(np.float32)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold = np.percentile(gray, percentile)
mask = gray >= threshold
avg_r = np.mean(img[:, :, 2][mask])
avg_g = np.mean(img[:, :, 1][mask])
avg_b = np.mean(img[:, :, 0][mask])
avg_gray = (avg_r + avg_g + avg_b) / 3
gain_r = avg_gray / avg_r
gain_g = avg_gray / avg_g
gain_b = avg_gray / avg_b
img[:, :, 2] *= gain_r
img[:, :, 1] *= gain_g
img[:, :, 0] *= gain_b
img = np.clip(img, 0, 255).astype(np.uint8)
return img
def combined_awb(img):
img = img.astype(np.float32)
avg_r = np.mean(img[:, :, 2])
avg_g = np.mean(img[:, :, 1])
avg_b = np.mean(img[:, :, 0])
max_r = np.max(img[:, :, 2])
max_g = np.max(img[:, :, 1])
max_b = np.max(img[:, :, 0])
K_ave = (avg_r + avg_g + avg_b) / 3
K_max = (max_r + max_g + max_b) / 3
A_r = np.array([[avg_r**2, avg_r], [max_r**2, max_r]])
B_r = np.array([K_ave, K_max])
u_v_prime_r = np.linalg.solve(A_r, B_r)
u_prime_r, v_prime_r = u_v_prime_r
A_g = np.array([[avg_g**2, avg_g], [max_g**2, max_g]])
B_g = np.array([K_ave, K_max])
u_v_prime_g = np.linalg.solve(A_g, B_g)
u_prime_g, v_prime_g = u_v_prime_g
A_b = np.array([[avg_b**2, avg_b], [max_b**2, max_b]])
B_b = np.array([K_ave, K_max])
u_v_prime_b = np.linalg.solve(A_b, B_b)
u_prime_b, v_prime_b = u_v_prime_b
img[:, :, 2] = u_prime_r * img[:, :, 2]**2 + v_prime_r * img[:, :, 2]
img[:, :, 1] = u_prime_g * img[:, :, 1]**2 + v_prime_g * img[:, :, 1]
img[:, :, 0] = u_prime_b * img[:, :, 0]**2 + v_prime_b * img[:, :, 0]
img = np.clip(img, 0, 255).astype(np.uint8)
return img
# 读取图像
file_path = '../images/NikonD5200_0001_G_AS.png'
img = cv2.imread(file_path)
# 应用灰度世界假设的AWB
awb_gray_world_img = gray_world_awb(img)
# 应用完美反射假设的AWB
awb_perfect_reflector_img = perfect_reflector_awb(img)
# 应用结合最大反射和灰度世界假设的AWB
awb_combined_img = combined_awb(img)
# 显示原始图像和校正后的图像
fig, axes = plt.subplots(2, 2, figsize=(12, 12))
axes[0, 0].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
axes[0, 0].set_title('Original Image')
axes[0, 1].imshow(cv2.cvtColor(awb_gray_world_img, cv2.COLOR_BGR2RGB))
axes[0, 1].set_title('Gray World AWB Image')
axes[1, 0].imshow(cv2.cvtColor(awb_perfect_reflector_img, cv2.COLOR_BGR2RGB))
axes[1, 0].set_title('Perfect Reflector AWB Image')
axes[1, 1].imshow(cv2.cvtColor(awb_combined_img, cv2.COLOR_BGR2RGB))
axes[1, 1].set_title('Combined AWB Image')
plt.show()
结果展示:
结论
通过对比三种自动白平衡算法——灰度世界假设、完美反射假设及其结合方法,我们可以观察到它们在不同场景下的表现和效果。针对这张图像,以下是具体的分析和结论:
-
灰度世界假设:
- 优点:算法简单,适用于大多数图像,能够有效地平衡颜色,使图像整体色调更均匀。
- 缺点:在测试图像中,应用灰度世界假设后,图像出现明显的绿色偏色。这是因为灰度世界假设对所有像素平均处理,可能会在颜色分布不均匀的图像中出现颜色偏差。
-
完美反射假设:
- 优点:能够很好地处理高亮度区域,适用于包含高反射物体的场景,能显著改善图像的白平衡。
- 缺点:在测试图像中,应用完美反射假设后,图像出现明显的红色偏色。这是因为完美反射假设主要依赖于图像中高亮度区域的颜色信息,如果高亮度区域本身存在颜色偏差,可能会导致整个图像的颜色校正不准确。
-
结合灰度世界和完美反射假设:
- 优点:结合两种方法的优势,能够在大多数场景中提供更准确的白平衡校正。结合方法综合考虑了图像中所有像素的平均颜色信息和高亮度区域的颜色信息,能够在颜色平衡和色彩还原方面达到更好的效果。
- 缺点:计算复杂度较高,但相对于效果提升,这一缺点是可以接受的。
通过上述对比,结合灰度世界假设和完美反射假设的自动白平衡算法在多种场景下表现出了更优异的效果,能够显著改善图像的视觉效果。这种方法适用于大多数自然场景中的白平衡调整,是一种有效的自动白平衡解决方案。