ISP Pipeline(7): Gamma Correction 伽马校正

AI_Plays/ISP/Fast_ISP_Progress.ipynb at main · ameengee/AI_Plays · GitHub

Gamma Correction(伽马校正)是图像处理中的一个重要步骤,目的是调整图像的亮度,使其更符合人眼的感知显示设备的特性

为什么需要 Gamma Correction?

人眼对亮度的感知是非线性的

  • 低亮度更敏感,对高亮度不敏感

  • 如果我们不进行校正,线性存储的图像在显示设备上会显得过暗或不自然。

多数显示设备(如显示器)也不是线性显示亮度的,而是有自身的非线性响应曲线(通常是 2.2 的幂函数)。

Gamma Correction 原理

Gamma 校正的核心公式:

 ➤ 从线性亮度 → 非线性(编码):

I_corrected = I_input ** (1 / gamma)

 ➤ 从非线性(解码) → 线性亮度(反伽马):

I_linear = I_encoded ** gamma

其中:

  • I_input 是归一化的像素值(0.0 ~ 1.0)

  • gamma 是伽马值,通常为 2.2(sRGB 标准)

  • I_corrected 是校正后的值(也归一化)

 代码:

def GAC(cfa_img, gamma):"""Gamma correction for demosaiced RGB image.Args:cfa_img: np.ndarray, dtype=uint8 or float, range [0,255] or [0,1]gamma: float, e.g., 2.2Returns:gac_img: np.ndarray, dtype=uint8, gamma-corrected RGB image"""# 1. Convert to float and normalizecfa_img = cfa_img.astype(np.float32) / 255.0# 2. Apply gamma correctiongac_img = np.power(cfa_img, 1.0 / gamma)# 3. Scale back to 0~255gac_img = np.clip(gac_img * 255.0, 0, 255).astype(np.uint8)return gac_img

np.power()

np.power(a, b)a ** b 对于 NumPy 数组来说功能是一样的,作用都是逐元素幂运算,但:

np.power更明确、健壮、推荐的方式,尤其是在处理多维数组、广播、类型转换时更可控。

np.power 的优势

场景为什么更好
类型控制更安全会自动广播并转换成合适的 dtype(比如 float64)避免整型幂出错
更显式语义在写图像处理/科学计算时,用 np.power 一看就知道是「逐元素幂运算」,可读性强
兼容广播比如 np.power(array, scalar)np.power(array1, array2) 都支持
链式操作稳定np.clipnp.sqrt 等 NumPy 函数一起用时更一致、少坑