crabbly commited on
Commit
c70e841
·
verified ·
1 Parent(s): a1ee9a0

Delete color_calibration.py

Browse files
Files changed (1) hide show
  1. color_calibration.py +0 -140
color_calibration.py DELETED
@@ -1,140 +0,0 @@
1
- """
2
- Color calibration via X-Rite ColorChecker Classic (24 patches).
3
-
4
- Detects the checker in both a reference and target image, computes a
5
- white-balance + CCM + luminance correction pipeline, and applies it to
6
- the full target image.
7
- """
8
-
9
- import cv2 as cv
10
- import numpy as np
11
-
12
-
13
- # ── linear / sRGB helpers ──────────────────────────────────────────
14
-
15
- def to_linear_srgb(u8_bgr):
16
- rgb = cv.cvtColor(u8_bgr, cv.COLOR_BGR2RGB).astype(np.float32) / 255.0
17
- a = 0.055
18
- lin = np.where(rgb <= 0.04045, rgb / 12.92, ((rgb + a) / (1 + a)) ** 2.4)
19
- return lin
20
-
21
-
22
- def to_srgb_u8(lin_rgb):
23
- a = 0.055
24
- srgb = np.where(lin_rgb <= 0.0031308, 12.92 * lin_rgb, (1 + a) * np.power(lin_rgb, 1/2.4) - a)
25
- srgb = np.clip(srgb, 0, 1)
26
- return cv.cvtColor((srgb * 255.0).astype(np.uint8), cv.COLOR_RGB2BGR)
27
-
28
-
29
- # ── checker geometry ───────────────────────────────────────────────
30
-
31
- def warp_checker(img, corners, out_w=600, out_h=400):
32
- dst = np.float32([[0, 0], [out_w-1, 0], [out_w-1, out_h-1], [0, out_h-1]])
33
- H = cv.getPerspectiveTransform(np.float32(corners), dst)
34
- warped = cv.warpPerspective(img, H, (out_w, out_h), flags=cv.INTER_CUBIC)
35
- return warped
36
-
37
-
38
- def detect_checker_corners(img_bgr):
39
- det = cv.mcc.CCheckerDetector_create()
40
- ok = det.process(img_bgr, cv.mcc.MCC24)
41
- if not ok:
42
- raise RuntimeError("ColorChecker not found")
43
- lst = det.getListColorChecker()
44
- cc = lst[0]
45
- if hasattr(cc, "getBox"):
46
- corners = np.array(cc.getBox(), dtype=np.float32)
47
- else:
48
- corners = np.array(cc.getCorners(), dtype=np.float32)
49
- return corners
50
-
51
-
52
- def sample_24_patches(warped, margin=12):
53
- H, W = warped.shape[:2]
54
- cell_w, cell_h = W / 6.0, H / 4.0
55
- lin = to_linear_srgb(warped)
56
- means = []
57
- for r in range(4):
58
- for c in range(6):
59
- x0 = int(c * cell_w + margin); x1 = int((c+1) * cell_w - margin)
60
- y0 = int(r * cell_h + margin); y1 = int((r+1) * cell_h - margin)
61
- roi = lin[y0:y1, x0:x1]
62
- means.append(np.median(roi.reshape(-1, 3), axis=0))
63
- return np.stack(means, 0)
64
-
65
-
66
- # ── calibration math ──────────────────────────────────────────────
67
-
68
- def white_balance_neutrals(src24, ref24):
69
- idx = np.arange(18, 24)
70
- src_g = src24[idx].mean(0)
71
- ref_g = ref24[idx].mean(0)
72
- gains = ref_g / np.maximum(src_g, 1e-6)
73
- return gains
74
-
75
-
76
- def solve_ccm_no_bias(src24, ref24, use_indices):
77
- A = src24[use_indices]
78
- B = ref24[use_indices]
79
- X, *_ = np.linalg.lstsq(A, B, rcond=None)
80
- M = X.T
81
- return M
82
-
83
-
84
- def fit_monotone_luma_curve_midgrays(src24_lin, ref24_lin):
85
- mid_idx = np.array([19, 20, 21, 22])
86
- w = np.array([0.2126, 0.7152, 0.0722], np.float32)
87
-
88
- Ls = (src24_lin[mid_idx] @ w).astype(np.float32)
89
- Lt = (ref24_lin[mid_idx] @ w).astype(np.float32)
90
-
91
- eps_lo, eps_hi = 0.01, 0.98
92
- Ls = np.concatenate([[eps_lo], np.sort(Ls), [eps_hi]])
93
- Lt = np.concatenate([[eps_lo], np.sort(Lt), [eps_hi]])
94
-
95
- def map_luma(L):
96
- L = np.clip(L, 0, 1)
97
- j = np.searchsorted(Ls, L, side='right') - 1
98
- j = np.clip(j, 0, len(Ls)-2)
99
- t = (L - Ls[j]) / np.maximum(Ls[j+1] - Ls[j], 1e-6)
100
- return (1 - t) * Lt[j] + t * Lt[j+1]
101
- return map_luma
102
-
103
-
104
- def soft_highlight_rolloff(L, knee=0.90, strength=0.6):
105
- below = L < knee
106
- out = np.empty_like(L, dtype=np.float32)
107
- out[below] = L[below]
108
- x = (L[~below] - knee) / max(1e-6, (1.0 - knee))
109
- out[~below] = knee + (1.0 - knee) * (1.0 - np.exp(-strength * x))
110
- return out
111
-
112
-
113
- # ── full correction pipeline ─────────────────────────────────────
114
-
115
- def apply_pipeline(target_bgr_u8, ref24, tgt24):
116
- gains = white_balance_neutrals(tgt24, ref24)
117
-
118
- lin = to_linear_srgb(target_bgr_u8)
119
- lin_wb = lin * gains.reshape(1, 1, 3)
120
-
121
- tgt24_wb = tgt24 * gains
122
-
123
- chroma_idx = np.arange(0, 18)
124
- M = solve_ccm_no_bias(tgt24_wb, ref24, chroma_idx)
125
-
126
- H, W = lin_wb.shape[:2]
127
- corrected = lin_wb.reshape(-1, 3) @ M.T
128
- corrected = corrected.reshape(H, W, 3)
129
-
130
- map_luma = fit_monotone_luma_curve_midgrays(tgt24_wb, ref24)
131
- w = np.array([0.2126, 0.7152, 0.0722], np.float32)
132
- L = np.clip(np.tensordot(corrected, w, axes=([2], [0])), 0, 1)
133
- Lt = map_luma(L)
134
- Lt = soft_highlight_rolloff(Lt, knee=0.90, strength=0.6)
135
- eps = 1e-6
136
- scale = (Lt + eps) / (L + eps)
137
- corrected = corrected * scale[..., None]
138
-
139
- corrected = np.clip(corrected, 0, 1)
140
- return to_srgb_u8(corrected)