Created
May 31, 2024 14:37
-
-
Save pedrol2b/d1a8b713b096a9b4de2d89fd19102d75 to your computer and use it in GitHub Desktop.
Calculate avarage luminance from an YUV Frame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getAverageLuminance(frame: Frame): Double { | |
require(frame.pixelFormat == PixelFormat.YUV) { "Unsupported pixel format. Expected YUV." } | |
var totalLuminance = 0.0 | |
val yBuffer = frame.image.planes[0].buffer | |
while (yBuffer.hasRemaining()) { | |
@Suppress("MagicNumber") | |
totalLuminance += yBuffer.get().toInt() and 0xFF | |
} | |
return totalLuminance / (frame.width * frame.height) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment