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.clip 、np.sqrt 等 NumPy 函數一起用時更一致、少坑 |
?