feat: noise model adaptive
This commit is contained in:
1 parent
f5577f9119
commit
029ff25ab1
9 files changed
+283
-6
No files matched your search
@@ -3200,9 +3200,6 @@ object GalleryManager {
|
||||
captureResult = captureResult,
|
||||
)
|
||||
|
||||
val noiseProfileSelection = ContentRepository.getInstance(context)
|
||||
.rawNoiseProfileManager
|
||||
.resolveSelection(resolveRawNoiseProfileId(context, metadata))
|
||||
val captureRawMetadata = RawMetadata.create(
|
||||
width = firstImageWidth,
|
||||
height = firstImageHeight,
|
||||
@@ -3214,6 +3211,12 @@ object GalleryManager {
|
||||
).let(physicalRawCrop::rebase).copy(
|
||||
exposureCompensation = captureExposureCompensationEv,
|
||||
)
|
||||
val noiseProfileSelection = ContentRepository.getInstance(context)
|
||||
.rawNoiseProfileManager
|
||||
.resolveSelection(
|
||||
requestedId = resolveRawNoiseProfileId(context, metadata),
|
||||
metadata = captureRawMetadata,
|
||||
)
|
||||
val rawMetadata = captureRawMetadata.withNoiseProfileSelection(noiseProfileSelection)
|
||||
val captureRawDefaultCrop = physicalRawCrop.outputBounds
|
||||
val captureBlackBorderCrop =
|
||||
|
||||
@@ -11,8 +11,11 @@ sealed interface RawNoiseProfileSelection {
|
||||
data class Camera2(
|
||||
/** Calibrated model used only when no usable Camera2 sensor model is available. */
|
||||
val fallbackProfile: CalibratedRawNoiseProfile,
|
||||
override val id: String = SYSTEM_CAMERA2_ID,
|
||||
) : RawNoiseProfileSelection {
|
||||
override val id: String = SYSTEM_CAMERA2_ID
|
||||
init {
|
||||
require(id.isNotBlank())
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.hinnka.mycamera.raw
|
||||
|
||||
import com.hinnka.mycamera.processor.CalibratedRawNoiseProfile
|
||||
import kotlin.math.pow
|
||||
|
||||
internal data class AdaptiveRawNoiseProfileTarget(
|
||||
val sensorPhysicalWidthMm: Float,
|
||||
val sensorPhysicalHeightMm: Float,
|
||||
val sensorPixelArrayWidth: Int,
|
||||
val sensorPixelArrayHeight: Int,
|
||||
val aperture: Float,
|
||||
val maximumAnalogSensitivityIso: Int,
|
||||
)
|
||||
|
||||
internal data class AdaptiveRawNoiseProfileEstimate(
|
||||
val profile: CalibratedRawNoiseProfile,
|
||||
val relativeNoiseDriver: Double,
|
||||
val shotScale: Double,
|
||||
val readQuadraticScale: Double,
|
||||
val readFloorScale: Double,
|
||||
)
|
||||
|
||||
/**
|
||||
* Estimates a per-lens noise model from the bundled X9 Ultra main-camera calibration.
|
||||
*
|
||||
* The hardware proxy is the inverse per-photosite light collection at a fixed scene exposure:
|
||||
* sensor area / photosite count / f-number^2. The response exponents are anchored by the known
|
||||
* X9 Ultra main/3X calibration pair. They preserve the template's channel ratios while matching
|
||||
* the pair's mean A, C and D coefficient changes. B follows A so the template's shot-noise zero
|
||||
* crossing is preserved instead of treating a fitted negative intercept as an independent noise
|
||||
* source.
|
||||
*
|
||||
* This is deliberately an estimate. Sensor process, quantum efficiency, conversion gain and ADC
|
||||
* read noise are not exposed by Camera2 and cannot be inferred from lens geometry.
|
||||
*/
|
||||
internal object AdaptiveRawNoiseProfileEstimator {
|
||||
private const val REFERENCE_SENSOR_OPTICAL_DENOMINATOR = 1.12
|
||||
private const val OPTICAL_FORMAT_ONE_INCH_DIAGONAL_MM = 16.0
|
||||
private const val FOUR_BY_THREE_AREA_FROM_DIAGONAL = 12.0 / 25.0
|
||||
private const val REFERENCE_SENSOR_PIXEL_COUNT = 200_000_000.0
|
||||
private const val REFERENCE_APERTURE = 1.5
|
||||
|
||||
// Derived from the mean X9Ultra3X / X9Ultra coefficient ratios and the corresponding
|
||||
// 1/1.28-inch, 200 MP, f/2.2 versus 1/1.12-inch, 200 MP, f/1.5 light-collection ratio.
|
||||
private const val SHOT_RESPONSE_EXPONENT = 0.5718385613683723
|
||||
private const val READ_QUADRATIC_RESPONSE_EXPONENT = 1.0935780217134652
|
||||
private const val READ_FLOOR_RESPONSE_EXPONENT = 0.3686003701156199
|
||||
|
||||
fun estimate(
|
||||
id: String,
|
||||
template: CalibratedRawNoiseProfile,
|
||||
target: AdaptiveRawNoiseProfileTarget,
|
||||
): AdaptiveRawNoiseProfileEstimate? {
|
||||
val physicalWidth = target.sensorPhysicalWidthMm.toDouble()
|
||||
val physicalHeight = target.sensorPhysicalHeightMm.toDouble()
|
||||
val aperture = target.aperture.toDouble()
|
||||
if (
|
||||
!physicalWidth.isFinite() || physicalWidth <= 0.0 ||
|
||||
!physicalHeight.isFinite() || physicalHeight <= 0.0 ||
|
||||
target.sensorPixelArrayWidth <= 0 || target.sensorPixelArrayHeight <= 0 ||
|
||||
!aperture.isFinite() || aperture <= 0.0
|
||||
) {
|
||||
return null
|
||||
}
|
||||
|
||||
val targetPixelCount =
|
||||
target.sensorPixelArrayWidth.toDouble() * target.sensorPixelArrayHeight.toDouble()
|
||||
val targetCollection = physicalWidth * physicalHeight / targetPixelCount / (aperture * aperture)
|
||||
val referenceDiagonal =
|
||||
OPTICAL_FORMAT_ONE_INCH_DIAGONAL_MM / REFERENCE_SENSOR_OPTICAL_DENOMINATOR
|
||||
val referenceArea =
|
||||
referenceDiagonal * referenceDiagonal * FOUR_BY_THREE_AREA_FROM_DIAGONAL
|
||||
val referenceCollection =
|
||||
referenceArea / REFERENCE_SENSOR_PIXEL_COUNT / (REFERENCE_APERTURE * REFERENCE_APERTURE)
|
||||
val relativeNoiseDriver = referenceCollection / targetCollection
|
||||
if (!relativeNoiseDriver.isFinite() || relativeNoiseDriver <= 0.0) return null
|
||||
|
||||
val shotScale = relativeNoiseDriver.pow(SHOT_RESPONSE_EXPONENT)
|
||||
val readQuadraticScale = relativeNoiseDriver.pow(READ_QUADRATIC_RESPONSE_EXPONENT)
|
||||
val readFloorScale = relativeNoiseDriver.pow(READ_FLOOR_RESPONSE_EXPONENT)
|
||||
if (sequenceOf(shotScale, readQuadraticScale, readFloorScale).any { !it.isFinite() || it <= 0.0 }) {
|
||||
return null
|
||||
}
|
||||
|
||||
val profile = CalibratedRawNoiseProfile(
|
||||
id = id,
|
||||
shotSlopeA = template.shotSlopeA.scaledBy(shotScale),
|
||||
shotInterceptB = template.shotInterceptB.scaledBy(shotScale),
|
||||
readQuadraticC = template.readQuadraticC.scaledBy(readQuadraticScale),
|
||||
readDigitalGainD = template.readDigitalGainD.scaledBy(readFloorScale),
|
||||
maxAnalogSensitivity = target.maximumAnalogSensitivityIso
|
||||
.takeIf { it > 0 }
|
||||
?: template.maxAnalogSensitivity,
|
||||
)
|
||||
return AdaptiveRawNoiseProfileEstimate(
|
||||
profile = profile,
|
||||
relativeNoiseDriver = relativeNoiseDriver,
|
||||
shotScale = shotScale,
|
||||
readQuadraticScale = readQuadraticScale,
|
||||
readFloorScale = readFloorScale,
|
||||
)
|
||||
}
|
||||
|
||||
private fun DoubleArray.scaledBy(scale: Double): DoubleArray =
|
||||
DoubleArray(size) { index -> this[index] * scale }
|
||||
}
|
||||
@@ -203,6 +203,10 @@ class RawDemosaicProcessor {
|
||||
frameAperture = dngRawData.aperture,
|
||||
inheritedAperture = baseMetadata?.aperture,
|
||||
),
|
||||
sensorPhysicalWidthMm = baseMetadata?.sensorPhysicalWidthMm ?: 0f,
|
||||
sensorPhysicalHeightMm = baseMetadata?.sensorPhysicalHeightMm ?: 0f,
|
||||
sensorPixelArrayWidth = baseMetadata?.sensorPixelArrayWidth ?: 0,
|
||||
sensorPixelArrayHeight = baseMetadata?.sensorPixelArrayHeight ?: 0,
|
||||
activeArray = activeArray,
|
||||
channelNoiseProfile = channelNoiseProfile,
|
||||
noiseProfileLayout = noiseProfileLayout,
|
||||
@@ -2490,7 +2494,7 @@ class RawDemosaicProcessor {
|
||||
actualMetadata = actualMetadata?.withNoiseProfileSelection(
|
||||
ContentRepository.getInstance(context.applicationContext)
|
||||
.rawNoiseProfileManager
|
||||
.resolveSelection(rawNoiseProfileId),
|
||||
.resolveSelection(rawNoiseProfileId, actualMetadata),
|
||||
)
|
||||
val captureProfilePass = sceneExposureRequest != null ||
|
||||
legacyAutoExposureRequest != null || captureProfilePreparationRequested
|
||||
|
||||
@@ -163,6 +163,11 @@ data class RawMetadata(
|
||||
val maxAnalogSensitivity: Int = 0,
|
||||
val shutterSpeed: Long = 0L,
|
||||
val aperture: Float = FALLBACK_APERTURE_F_NUMBER,
|
||||
/** CameraCharacteristics sensor geometry used only for adaptive noise-model estimation. */
|
||||
val sensorPhysicalWidthMm: Float = 0f,
|
||||
val sensorPhysicalHeightMm: Float = 0f,
|
||||
val sensorPixelArrayWidth: Int = 0,
|
||||
val sensorPixelArrayHeight: Int = 0,
|
||||
val frameCount: Int = 1,
|
||||
/**
|
||||
* MGC's normalized 128-bin power-correlation spectrum after spatial merge.
|
||||
@@ -460,6 +465,12 @@ data class RawMetadata(
|
||||
CameraCharacteristics.LENS_INFO_AVAILABLE_APERTURES,
|
||||
),
|
||||
)
|
||||
val sensorPhysicalSize = characteristics.get(
|
||||
CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE,
|
||||
)
|
||||
val sensorPixelArraySize = characteristics.get(
|
||||
CameraCharacteristics.SENSOR_INFO_PIXEL_ARRAY_SIZE,
|
||||
)
|
||||
|
||||
return RawMetadata(
|
||||
width = width,
|
||||
@@ -497,6 +508,10 @@ data class RawMetadata(
|
||||
maxAnalogSensitivity = maxAnalogSensitivity,
|
||||
shutterSpeed = shutterSpeed,
|
||||
aperture = aperture,
|
||||
sensorPhysicalWidthMm = sensorPhysicalSize?.width ?: 0f,
|
||||
sensorPhysicalHeightMm = sensorPhysicalSize?.height ?: 0f,
|
||||
sensorPixelArrayWidth = sensorPixelArraySize?.width ?: 0,
|
||||
sensorPixelArrayHeight = sensorPixelArraySize?.height ?: 0,
|
||||
sceneHasFace = captureResult.get(CaptureResult.STATISTICS_FACES)
|
||||
?.isNotEmpty() == true,
|
||||
)
|
||||
|
||||
@@ -37,14 +37,28 @@ class RawNoiseProfileManager(context: Context) {
|
||||
}
|
||||
RawNoiseProfileSelection.Camera2(fallbackProfile)
|
||||
}
|
||||
private val adaptiveTemplate: CalibratedRawNoiseProfile by lazy(
|
||||
LazyThreadSafetyMode.SYNCHRONIZED,
|
||||
) {
|
||||
val templateInfo = checkNotNull(
|
||||
BUILT_IN_PROFILES.firstOrNull { it.id == X9_ULTRA_PROFILE_ID },
|
||||
) { "Bundled X9 Ultra adaptive RAW noise template is not registered" }
|
||||
checkNotNull(loadCalibratedProfile(templateInfo)) {
|
||||
"Bundled X9 Ultra adaptive RAW noise template could not be loaded"
|
||||
}
|
||||
}
|
||||
|
||||
fun getAvailableProfiles(): List<RawNoiseProfileInfo> =
|
||||
(BUILT_IN_PROFILES + customImportManager.getCustomRawNoiseProfiles())
|
||||
.distinctBy(RawNoiseProfileInfo::id)
|
||||
|
||||
fun resolveSelection(requestedId: String?): RawNoiseProfileSelection {
|
||||
fun resolveSelection(
|
||||
requestedId: String?,
|
||||
metadata: RawMetadata? = null,
|
||||
): RawNoiseProfileSelection {
|
||||
val id = requestedId?.takeIf { it.isNotBlank() } ?: DEFAULT_PROFILE_ID
|
||||
if (id == SYSTEM_PROFILE_ID) return systemCamera2Selection
|
||||
if (id == ADAPTIVE_PROFILE_ID) return resolveAdaptiveSelection(metadata)
|
||||
val info = getAvailableProfiles().firstOrNull { it.id == id }
|
||||
val calibrated = info?.let(::loadCalibratedProfile)
|
||||
if (calibrated != null) return RawNoiseProfileSelection.Calibrated(calibrated, id)
|
||||
@@ -57,6 +71,53 @@ class RawNoiseProfileManager(context: Context) {
|
||||
return systemCamera2Selection
|
||||
}
|
||||
|
||||
private fun resolveAdaptiveSelection(metadata: RawMetadata?): RawNoiseProfileSelection {
|
||||
val estimate = metadata?.let { rawMetadata ->
|
||||
AdaptiveRawNoiseProfileEstimator.estimate(
|
||||
id = ADAPTIVE_PROFILE_ID,
|
||||
template = adaptiveTemplate,
|
||||
target = AdaptiveRawNoiseProfileTarget(
|
||||
sensorPhysicalWidthMm = rawMetadata.sensorPhysicalWidthMm,
|
||||
sensorPhysicalHeightMm = rawMetadata.sensorPhysicalHeightMm,
|
||||
sensorPixelArrayWidth = rawMetadata.sensorPixelArrayWidth,
|
||||
sensorPixelArrayHeight = rawMetadata.sensorPixelArrayHeight,
|
||||
aperture = rawMetadata.aperture,
|
||||
maximumAnalogSensitivityIso = rawMetadata.maxAnalogSensitivity,
|
||||
),
|
||||
)
|
||||
}
|
||||
if (estimate != null) {
|
||||
val targetMetadata = checkNotNull(metadata)
|
||||
PLog.i(
|
||||
TAG,
|
||||
"Adaptive RAW noise profile generated: " +
|
||||
"sensor=${targetMetadata.sensorPhysicalWidthMm}x" +
|
||||
"${targetMetadata.sensorPhysicalHeightMm}mm " +
|
||||
"pixels=${targetMetadata.sensorPixelArrayWidth}x" +
|
||||
"${targetMetadata.sensorPixelArrayHeight} " +
|
||||
"aperture=f/${targetMetadata.aperture} " +
|
||||
"maxAnalogIso=${targetMetadata.maxAnalogSensitivity} " +
|
||||
"driver=${estimate.relativeNoiseDriver} shotScale=${estimate.shotScale} " +
|
||||
"readQuadraticScale=${estimate.readQuadraticScale} " +
|
||||
"readFloorScale=${estimate.readFloorScale}",
|
||||
)
|
||||
return RawNoiseProfileSelection.Calibrated(
|
||||
profile = estimate.profile,
|
||||
id = ADAPTIVE_PROFILE_ID,
|
||||
)
|
||||
}
|
||||
|
||||
PLog.w(
|
||||
TAG,
|
||||
"Adaptive RAW noise profile lacks complete sensor geometry; using the source " +
|
||||
"Camera2/DNG profile with the X9 Ultra template as fallback",
|
||||
)
|
||||
return RawNoiseProfileSelection.Camera2(
|
||||
fallbackProfile = adaptiveTemplate,
|
||||
id = ADAPTIVE_PROFILE_ID,
|
||||
)
|
||||
}
|
||||
|
||||
private fun loadCalibratedProfile(info: RawNoiseProfileInfo): CalibratedRawNoiseProfile? {
|
||||
calibratedCache[info.id]?.let { return it }
|
||||
val path = info.filePath ?: return null
|
||||
@@ -79,6 +140,7 @@ class RawNoiseProfileManager(context: Context) {
|
||||
companion object {
|
||||
private const val TAG = "RawNoiseProfileManager"
|
||||
const val SYSTEM_PROFILE_ID = RawNoiseProfileSelection.SYSTEM_CAMERA2_ID
|
||||
const val ADAPTIVE_PROFILE_ID = "adaptive_x9_ultra"
|
||||
const val PIXEL8_PRO_PROFILE_ID = "builtin_noise_pixel8_pro"
|
||||
const val X9_ULTRA_PROFILE_ID = "builtin_noise_x9_ultra"
|
||||
const val X9_ULTRA_3X_PROFILE_ID = "builtin_noise_x9_ultra_3x"
|
||||
@@ -92,6 +154,13 @@ class RawNoiseProfileManager(context: Context) {
|
||||
isBuiltIn = true,
|
||||
nameResId = R.string.raw_noise_profile_system,
|
||||
),
|
||||
RawNoiseProfileInfo(
|
||||
id = ADAPTIVE_PROFILE_ID,
|
||||
nameMap = emptyMap(),
|
||||
filePath = null,
|
||||
isBuiltIn = true,
|
||||
nameResId = R.string.raw_noise_profile_adaptive,
|
||||
),
|
||||
RawNoiseProfileInfo(
|
||||
id = PIXEL8_PRO_PROFILE_ID,
|
||||
nameMap = emptyMap(),
|
||||
|
||||
@@ -292,6 +292,7 @@
|
||||
<string name="raw_noise_profile_lens_override_value">单独配置:%1$s</string>
|
||||
<string name="raw_noise_profile_use_unified">使用统一模型(%1$s)</string>
|
||||
<string name="raw_noise_profile_system">系统默认(Camera2 API)</string>
|
||||
<string name="raw_noise_profile_adaptive">自适应(按当前镜头预估)</string>
|
||||
<string name="raw_noise_profile_pixel8_pro">Pixel 8 Pro</string>
|
||||
<string name="raw_noise_profile_x9_ultra">X9 Ultra</string>
|
||||
<string name="raw_noise_profile_x9_ultra_3x">X9 Ultra 3X</string>
|
||||
|
||||
@@ -296,6 +296,7 @@
|
||||
<string name="raw_noise_profile_lens_override_value">Override: %1$s</string>
|
||||
<string name="raw_noise_profile_use_unified">Use unified model (%1$s)</string>
|
||||
<string name="raw_noise_profile_system">System default (Camera2 API)</string>
|
||||
<string name="raw_noise_profile_adaptive">Adaptive (estimated for current lens)</string>
|
||||
<string name="raw_noise_profile_pixel8_pro">Pixel 8 Pro</string>
|
||||
<string name="raw_noise_profile_x9_ultra">X9 Ultra</string>
|
||||
<string name="raw_noise_profile_x9_ultra_3x">X9 Ultra 3X</string>
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.hinnka.mycamera.raw
|
||||
|
||||
import com.hinnka.mycamera.processor.CalibratedRawNoiseProfile
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class AdaptiveRawNoiseProfileEstimatorTest {
|
||||
private val template = CalibratedRawNoiseProfile(
|
||||
id = "template",
|
||||
shotSlopeA = doubleArrayOf(1.0, 2.0, 3.0, 4.0),
|
||||
shotInterceptB = doubleArrayOf(-1.0, -2.0, -3.0, -4.0),
|
||||
readQuadraticC = doubleArrayOf(5.0, 6.0, 7.0, 8.0),
|
||||
readDigitalGainD = doubleArrayOf(9.0, 10.0, 11.0, 12.0),
|
||||
maxAnalogSensitivity = 9_100,
|
||||
)
|
||||
|
||||
@Test
|
||||
fun x9UltraReferenceGeometryPreservesTemplate() {
|
||||
val estimate = requireNotNull(
|
||||
AdaptiveRawNoiseProfileEstimator.estimate(
|
||||
id = "adaptive",
|
||||
template = template,
|
||||
target = targetForOpticalFormat(
|
||||
denominator = 1.12,
|
||||
aperture = 1.5f,
|
||||
maximumAnalogSensitivityIso = 9_100,
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(1.0, estimate.relativeNoiseDriver, 1e-6)
|
||||
assertArrayEquals(template.shotSlopeA, estimate.profile.shotSlopeA, 1e-6)
|
||||
assertArrayEquals(template.shotInterceptB, estimate.profile.shotInterceptB, 1e-6)
|
||||
assertArrayEquals(template.readQuadraticC, estimate.profile.readQuadraticC, 1e-6)
|
||||
assertArrayEquals(template.readDigitalGainD, estimate.profile.readDigitalGainD, 1e-6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun x9Ultra3xGeometryReproducesCalibrationPairMeanRatios() {
|
||||
val estimate = requireNotNull(
|
||||
AdaptiveRawNoiseProfileEstimator.estimate(
|
||||
id = "adaptive",
|
||||
template = template,
|
||||
target = targetForOpticalFormat(
|
||||
denominator = 1.28,
|
||||
aperture = 2.2f,
|
||||
maximumAnalogSensitivityIso = 12_700,
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(2.8096145124716556, estimate.relativeNoiseDriver, 1e-6)
|
||||
assertEquals(1.8053171378702446, estimate.shotScale, 1e-6)
|
||||
assertEquals(3.0947830646211503, estimate.readQuadraticScale, 1e-6)
|
||||
assertEquals(1.4634279773805543, estimate.readFloorScale, 1e-6)
|
||||
assertEquals(12_700, estimate.profile.maxAnalogSensitivity)
|
||||
}
|
||||
|
||||
private fun targetForOpticalFormat(
|
||||
denominator: Double,
|
||||
aperture: Float,
|
||||
maximumAnalogSensitivityIso: Int,
|
||||
): AdaptiveRawNoiseProfileTarget {
|
||||
val diagonal = 16.0 / denominator
|
||||
return AdaptiveRawNoiseProfileTarget(
|
||||
sensorPhysicalWidthMm = (diagonal * 4.0 / 5.0).toFloat(),
|
||||
sensorPhysicalHeightMm = (diagonal * 3.0 / 5.0).toFloat(),
|
||||
sensorPixelArrayWidth = 20_000,
|
||||
sensorPixelArrayHeight = 10_000,
|
||||
aperture = aperture,
|
||||
maximumAnalogSensitivityIso = maximumAnalogSensitivityIso,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user