measure: camera intrinsics are now crop-aware when the analysis aspect differs
- Commit
b9424ff9305f1d2976a5d08df8e31091eae4f7ad- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 04:53 UTC (5 days ago)
- Parents
5ac026d
When the delivered analysis frame's aspect differs from the sensor active array (4000x3000 sensor delivering 640x360), CameraIntrinsicsResolver applied independent per-axis scales and never offset the principal point by the crop origin — modelling an anamorphic stretch the camera never performs. For fx=3000/fy=3100/cx=2100/cy=1600 the correct crop-aware result is fy=496, cy=196 (4000x2250 centre crop at top=375, uniform 0.16 scale); the resolver produced fy=372 (-25%) and cy=192, skewing every vertical bearing fed to the tracker, triangulation, pose chaining and IMU scale — distances and areas corrupted exactly on the devices whose delivered aspect differs. Both the factory-calibration and focal-fallback paths now go through a CropTransform (centred crop rect matching the analysis aspect + one uniform scale; principal point offset by the crop origin first; non-positive analysis size guarded). Matching-aspect devices (the common 4:3->640x480 case) get numerically identical results. Two regression tests failed pre-fix on the exact values. Units, polygon math, VIO and IMU were audited and cleared. Verified: :measure:testDevUnitTest 96 + 52 native tests pass; lint errors are pre-existing on an untouched file.
Diff
measure/src/main/java/com/vayunmathur/measure/platform/CameraIntrinsics.kt
diff --git a/measure/src/main/java/com/vayunmathur/measure/platform/CameraIntrinsics.kt b/measure/src/main/java/com/vayunmathur/measure/platform/CameraIntrinsics.ktindex 6a945de39c..10a77b7c07 100644--- a/measure/src/main/java/com/vayunmathur/measure/platform/CameraIntrinsics.kt+++ b/measure/src/main/java/com/vayunmathur/measure/platform/CameraIntrinsics.kt@@ -26,7 +26,12 @@ data class Intrinsics( * and sensor size when the calibration tag is absent. * * The calibration is expressed against the sensor's **active array**, so it must be- * rescaled to whatever resolution the analysis stream actually delivers.+ * rescaled to whatever resolution the analysis stream actually delivers. The stream+ * is centre-cropped to its own aspect ratio and then uniformly scaled, so a single+ * scale factor (plus the crop origin offset for the principal point) maps active-array+ * pixels to analysis pixels — per-axis scales would describe an anamorphic stretch+ * the camera never performs, and mis-state `fy`/`cy` whenever the delivered aspect+ * differs from the active array's. */ object CameraIntrinsicsResolver { @@ -44,33 +49,59 @@ object CameraIntrinsicsResolver { val arrayW = activeArray.width().toDouble() val arrayH = activeArray.height().toDouble() if (arrayW <= 0 || arrayH <= 0) return null- // The analysis stream is centre-cropped to its own aspect ratio before being- // scaled down, so the two axes generally do not share a scale factor.- val scales = Scales(- x = analysisSize.width / arrayW,- y = analysisSize.height / arrayH,- )- return calibrationIntrinsics(chars, scales)- ?: fallbackIntrinsics(chars, arrayW, arrayH, scales, analysisSize)+ if (analysisSize.width <= 0 || analysisSize.height <= 0) return null+ val crop = CropTransform.of(arrayW, arrayH, analysisSize)+ return calibrationIntrinsics(chars, crop)+ ?: fallbackIntrinsics(chars, arrayW, arrayH, crop, analysisSize) } - private data class Scales(val x: Double, val y: Double)+ /**+ * Active-array → analysis-stream mapping: the centred crop of the active array+ * matching the analysis aspect ratio, and the one uniform scale from that crop+ * to the delivered size.+ */+ private data class CropTransform(+ val scale: Double,+ val left: Double,+ val top: Double,+ ) {+ companion object {+ fun of(arrayW: Double, arrayH: Double, analysisSize: Size): CropTransform {+ val analysisAspect = analysisSize.width.toDouble() / analysisSize.height+ val arrayAspect = arrayW / arrayH+ val cropW: Double+ val cropH: Double+ if (arrayAspect > analysisAspect) {+ cropH = arrayH+ cropW = arrayH * analysisAspect+ } else {+ cropW = arrayW+ cropH = arrayW / analysisAspect+ }+ return CropTransform(+ scale = analysisSize.width / cropW,+ left = (arrayW - cropW) / 2.0,+ top = (arrayH - cropH) / 2.0,+ )+ }+ }+ } private fun calibrationIntrinsics( chars: CameraCharacteristics,- scales: Scales,+ crop: CropTransform, ): Intrinsics? { val calibration = chars.get(CameraCharacteristics.LENS_INTRINSIC_CALIBRATION) ?: return null if (calibration.size < MIN_CALIBRATION_VALUES) return null- val fx = calibration[0].toDouble() * scales.x- val fy = calibration[1].toDouble() * scales.y+ val fx = calibration[0].toDouble() * crop.scale+ val fy = calibration[1].toDouble() * crop.scale if (fx <= 1.0 || fy <= 1.0) return null return Intrinsics( fx = fx, fy = fy,- cx = calibration[2].toDouble() * scales.x,- cy = calibration[3].toDouble() * scales.y,+ cx = (calibration[2].toDouble() - crop.left) * crop.scale,+ cy = (calibration[3].toDouble() - crop.top) * crop.scale, calibrated = true, ) }@@ -79,18 +110,20 @@ object CameraIntrinsicsResolver { chars: CameraCharacteristics, arrayW: Double, arrayH: Double,- scales: Scales,+ crop: CropTransform, analysisSize: Size, ): Intrinsics? { // Fallback: focal length in millimetres against physical sensor size.+ // The focal length in active-array pixels is unchanged by the centred+ // crop (crop and focal share the cropped extent), then scales uniformly. val focalMm = chars.get(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS) ?.firstOrNull()?.toDouble() ?: return null val physical = chars.get(CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE) ?: return null if (physical.width <= 0f || physical.height <= 0f) return null return Intrinsics(- fx = focalMm / physical.width * arrayW * scales.x,- fy = focalMm / physical.height * arrayH * scales.y,+ fx = focalMm / physical.width * arrayW * crop.scale,+ fy = focalMm / physical.height * arrayH * crop.scale, cx = analysisSize.width / 2.0, cy = analysisSize.height / 2.0, calibrated = false,
measure/src/test/java/com/vayunmathur/measure/platform/CameraIntrinsicsTest.kt
diff --git a/measure/src/test/java/com/vayunmathur/measure/platform/CameraIntrinsicsTest.kt b/measure/src/test/java/com/vayunmathur/measure/platform/CameraIntrinsicsTest.ktindex 979160ee4d..f23aebbef4 100644--- a/measure/src/test/java/com/vayunmathur/measure/platform/CameraIntrinsicsTest.kt+++ b/measure/src/test/java/com/vayunmathur/measure/platform/CameraIntrinsicsTest.kt@@ -69,6 +69,47 @@ class CameraIntrinsicsTest { assertTrue(CameraIntrinsicsResolver.timestampsAlignWithSensors(context)) } + @Test fun factoryCalibrationIsCropAwareWhenAnalysisAspectDiffers() {+ // Regression: the resolver applied per-axis scales (640/4000, 360/3000),+ // as if the sensor were anamorphically stretched to the analysis size.+ // The stream is instead centre-cropped to 16:9 (4000x2250 at top=375)+ // and uniformly scaled by 0.16, so fy must scale by 0.16 too and cy+ // must be offset by the crop origin. Per-axis scaling gave fy=372+ // (25% low) and cy=192, skewing every vertical bearing.+ addCamera {+ activeArray(4000, 3000)+ set(+ CameraCharacteristics.LENS_INTRINSIC_CALIBRATION,+ floatArrayOf(3000f, 3100f, 2100f, 1600f),+ )+ }+ val intr = CameraIntrinsicsResolver.resolve(context, Size(640, 360))+ assertNotNull(intr)+ assertTrue(intr.calibrated)+ assertEquals(480.0, intr.fx, 0.001)+ assertEquals(496.0, intr.fy, 0.001)+ assertEquals(336.0, intr.cx, 0.001)+ assertEquals(196.0, intr.cy, 0.001)+ }++ @Test fun focalFallbackIsCropAwareWhenAnalysisAspectDiffers() {+ // Same crop as above, through the focal-length fallback: fy in+ // active-array pixels is 4/3.75*3000 = 3200, uniformly scaled by+ // 0.16 to 512 — per-axis scaling gave 384.+ addCamera {+ activeArray(4000, 3000)+ set(CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS, floatArrayOf(4.0f))+ set(CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE, SizeF(5.0f, 3.75f))+ }+ val intr = CameraIntrinsicsResolver.resolve(context, Size(640, 360))+ assertNotNull(intr)+ assertFalse(intr.calibrated)+ assertEquals(512.0, intr.fx, 0.001)+ assertEquals(512.0, intr.fy, 0.001)+ assertEquals(320.0, intr.cx, 0.001)+ assertEquals(180.0, intr.cy, 0.001)+ }+ @Test fun focalFallbackWhenNoCalibrationTag() { addCamera { activeArray(4000, 3000)