Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -80,26 +80,20 @@ def compute_deltaE_00(lin_src, lin_ref):
|
|
| 80 |
|
| 81 |
def apply_color_pipeline(target_bgr, ref24, tgt24):
|
| 82 |
tgt_lin = to_linear_srgb(target_bgr)
|
|
|
|
|
|
|
| 83 |
gains = np.median(ref24[18:24], axis=0) / np.maximum(np.median(tgt24[18:24], axis=0), 1e-6)
|
| 84 |
tgt_lin_wb = tgt_lin * gains.reshape(1, 1, 3)
|
| 85 |
tgt24_wb = tgt24 * gains
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
pred_lin = np.clip(tgt24_wb @ W, 0, 1)
|
| 90 |
-
return np.mean(compute_deltaE_00(pred_lin, ref24))
|
| 91 |
-
|
| 92 |
X, Y = tgt24_wb, ref24
|
| 93 |
-
|
| 94 |
-
res = minimize(
|
| 95 |
-
objective,
|
| 96 |
-
W_init.flatten(),
|
| 97 |
-
method='Powell',
|
| 98 |
-
options={"maxiter": 150, "xtol": 1e-4, "ftol": 1e-4},
|
| 99 |
-
)
|
| 100 |
-
W_opt = res.x.reshape(3, 3)
|
| 101 |
|
|
|
|
| 102 |
corrected_lin = (tgt_lin_wb.reshape(-1, 3) @ W_opt).reshape(tgt_lin_wb.shape)
|
|
|
|
| 103 |
return to_srgb_u8(np.clip(corrected_lin, 0, 1))
|
| 104 |
|
| 105 |
# ==============================================================================
|
|
|
|
| 80 |
|
| 81 |
def apply_color_pipeline(target_bgr, ref24, tgt24):
|
| 82 |
tgt_lin = to_linear_srgb(target_bgr)
|
| 83 |
+
|
| 84 |
+
# 1. White Balance (Von Kries scaling)
|
| 85 |
gains = np.median(ref24[18:24], axis=0) / np.maximum(np.median(tgt24[18:24], axis=0), 1e-6)
|
| 86 |
tgt_lin_wb = tgt_lin * gains.reshape(1, 1, 3)
|
| 87 |
tgt24_wb = tgt24 * gains
|
| 88 |
|
| 89 |
+
# 2. INSTANT 3x3 Matrix Solver (Ridge Regression)
|
| 90 |
+
# The 0.01 alpha prevents extreme matrix values so colors never explode or warp
|
|
|
|
|
|
|
|
|
|
| 91 |
X, Y = tgt24_wb, ref24
|
| 92 |
+
W_opt = np.linalg.inv(X.T @ X + 0.01 * np.eye(3)) @ X.T @ Y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# 3. Apply to full image
|
| 95 |
corrected_lin = (tgt_lin_wb.reshape(-1, 3) @ W_opt).reshape(tgt_lin_wb.shape)
|
| 96 |
+
|
| 97 |
return to_srgb_u8(np.clip(corrected_lin, 0, 1))
|
| 98 |
|
| 99 |
# ==============================================================================
|