optocalib-lab 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
optocalib/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ from .core import calibrate_image, measure_roi_lab
2
+ from .generator import generate_calibration_card
3
+
4
+ __version__ = "0.1.0"
5
+ __all__ = ["calibrate_image", "measure_roi_lab", "generate_calibration_card"]
optocalib/core.py ADDED
@@ -0,0 +1,61 @@
1
+ import cv2
2
+ import numpy as np
3
+
4
+ def calibrate_image(bgr_img: np.ndarray, out_w: int = 820, out_h: int = 360, ref_box=(35, 85, 95, 275)):
5
+ h, w = bgr_img.shape[:2]
6
+ target_w = 600
7
+ scale = target_w / float(w) if w > target_w else 1.0
8
+ small = cv2.resize(bgr_img, (int(w * scale), int(h * scale)), interpolation=cv2.INTER_AREA) if scale < 1.0 else bgr_img
9
+ gray = cv2.cvtColor(small, cv2.COLOR_BGR2GRAY)
10
+
11
+ aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_4X4_50)
12
+ params = cv2.aruco.DetectorParameters()
13
+ params.cornerRefinementMethod = cv2.aruco.CORNER_REFINE_NONE
14
+ detector = cv2.aruco.ArucoDetector(aruco_dict, params)
15
+ corners, ids, _ = detector.detectMarkers(gray)
16
+
17
+ locked = False
18
+ aligned = cv2.resize(bgr_img, (out_w, out_h))
19
+
20
+ if ids is not None and len(ids) >= 4:
21
+ centers = {}
22
+ for i, mid in enumerate(ids.flatten()):
23
+ if int(mid) in (0, 1, 2, 3):
24
+ pts = corners[i].reshape(4, 2) / scale
25
+ centers[int(mid)] = pts.mean(axis=0)
26
+ if all(k in centers for k in (0, 1, 2, 3)):
27
+ src_pts = np.array((centers[0], centers[1], centers[2], centers[3]), dtype=np.float32)
28
+ dst_pts = np.array(((0, 0), (out_w - 1, 0), (out_w - 1, out_h - 1), (0, out_h - 1)), dtype=np.float32)
29
+ matrix = cv2.getPerspectiveTransform(src_pts, dst_pts)
30
+ aligned = cv2.warpPerspective(bgr_img, matrix, (out_w, out_h))
31
+ locked = True
32
+
33
+ x1, y1, x2, y2 = ref_box
34
+ ref_patch = aligned[y1:y2, x1:x2].astype(np.float32)
35
+ mean_bgr = np.maximum(ref_patch.mean(axis=(0, 1)), 1.0)
36
+ gains = 240.0 / mean_bgr
37
+ calibrated = np.clip(aligned.astype(np.float32) * gains, 0, 255).astype(np.uint8)
38
+
39
+ lab_before = cv2.cvtColor(aligned[y1:y2, x1:x2], cv2.COLOR_BGR2LAB).astype(np.float32).mean(axis=(0, 1))
40
+ lab_after = cv2.cvtColor(calibrated[y1:y2, x1:x2], cv2.COLOR_BGR2LAB).astype(np.float32).mean(axis=(0, 1))
41
+ target_lab = np.array([240.0, 128.0, 128.0])
42
+ de_before = float(np.linalg.norm(lab_before - target_lab))
43
+ de_after = float(np.linalg.norm(lab_after - target_lab))
44
+
45
+ return {
46
+ "image": calibrated,
47
+ "aruco_locked": locked,
48
+ "delta_e_before": round(de_before, 2),
49
+ "delta_e_after": round(de_after, 2),
50
+ "gains_bgr": [round(float(g), 3) for g in gains]
51
+ }
52
+
53
+ def measure_roi_lab(calibrated_bgr: np.ndarray, box: tuple):
54
+ x1, y1, x2, y2 = box
55
+ roi = calibrated_bgr[y1:y2, x1:x2]
56
+ lab = cv2.cvtColor(roi, cv2.COLOR_BGR2LAB).astype(np.float32).mean(axis=(0, 1))
57
+ return {
58
+ "L": round(float(lab[0] * 100.0 / 255.0), 2),
59
+ "a": round(float(lab[1] - 128.0), 2),
60
+ "b": round(float(lab[2] - 128.0), 2),
61
+ }
optocalib/generator.py ADDED
@@ -0,0 +1,17 @@
1
+ import cv2
2
+ import numpy as np
3
+
4
+ def generate_calibration_card(filename: str = "calibration_card.png", width: int = 820, height: int = 360):
5
+ """Generates a printable calibration mat with 4 ArUco markers and a white reference strip."""
6
+ card = np.full((height, width, 3), 255, dtype=np.uint8)
7
+ cv2.rectangle(card, (25, 70), (105, height - 70), (245, 245, 245), -1)
8
+ cv2.rectangle(card, (25, 70), (105, height - 70), (200, 200, 200), 1)
9
+
10
+ aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_4X4_50)
11
+ markers = ((0, 4, 4), (1, width - 54, 4), (2, width - 54, height - 54), (3, 4, height - 54))
12
+ for mid, mx, my in markers:
13
+ m_img = cv2.cvtColor(cv2.aruco.generateImageMarker(aruco_dict, mid, 50), cv2.COLOR_GRAY2BGR)
14
+ card[my:my+50, mx:mx+50] = m_img
15
+
16
+ cv2.imwrite(filename, card)
17
+ return filename
@@ -0,0 +1,38 @@
1
+ Metadata-Version: 2.4
2
+ Name: optocalib-lab
3
+ Version: 0.1.0
4
+ Summary: Lightweight ArUco-locked perspective alignment and Delta-E76 colorimetric calibration for scientific and industrial computer vision.
5
+ Project-URL: Homepage, https://github.com
6
+ Requires-Python: >=3.8
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: numpy
9
+ Requires-Dist: opencv-contrib-python-headless
10
+
11
+ # OptoCalib
12
+
13
+ **Lightweight ArUco-locked perspective alignment and Δ E₇₆ colorimetric calibration for scientific and industrial computer vision.**
14
+
15
+ `optocalib` solves two major challenges in smartphone-based optical sensing (microfluidics, LAMP/PCR assays, water quality strips, agriculture leaf colorimetry, and industrial QC):
16
+ 1. **Perspective & Scale Drift:** Automatically locks onto 4 printed ArUco markers (`IDs 0–3`) and rectifies skewed images in `<5 ms`.
17
+ 2. **Ambient Lighting Bias:** Normalizes illumination using a built-in white reference patch and quantifies color accuracy via perceptual Δ E₇₆ and CIELAB (\(L^*, a^*, b^*\)).
18
+
19
+ ## Quick Start
20
+
21
+ ```python
22
+ import cv2
23
+ from optocalib import calibrate_image, measure_roi_lab, generate_calibration_card
24
+
25
+ # 1. Generate a printable calibration template
26
+ generate_calibration_card("my_lab_mat.png")
27
+
28
+ # 2. Calibrate a skewed/unevenly lit photo
29
+ img = cv2.imread("sample_photo.jpg")
30
+ result = calibrate_image(img)
31
+
32
+ print("Locked:", result["aruco_locked"])
33
+ print("Delta-E76 Improved:", result["delta_e_before"], "->", result["delta_e_after"])
34
+
35
+ # 3. Measure perceptual CIELAB color in any ROI
36
+ lab_values = measure_roi_lab(result["image"], box=(145, 145, 199, 295))
37
+ print("CIELAB:", lab_values)
38
+ ```
@@ -0,0 +1,7 @@
1
+ optocalib/__init__.py,sha256=SPrc6RCkFCKQzaAVel-gI91BzepoVWSlBEY2i6E1src,201
2
+ optocalib/core.py,sha256=_WwigMnrs4sBT4Cs6N96AmV4fC0KGNln-Szhe9-qhfk,2736
3
+ optocalib/generator.py,sha256=JWWFgDGcuRiqZdNb4FMS1ZbETZRO7HARNEp9mNTOqH8,845
4
+ optocalib_lab-0.1.0.dist-info/METADATA,sha256=KHAXYZDbc8laJ1Q2Hynfh94ZvTf7UrqsEF4Dt2-rvZA,1628
5
+ optocalib_lab-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
6
+ optocalib_lab-0.1.0.dist-info/top_level.txt,sha256=_1hVCpT8M42NsF0vyUqfVWPA5owSVj2KQwmvUgucQgE,10
7
+ optocalib_lab-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ optocalib