Loading content...
In digital image processing and computer vision, luminance represents the perceived brightness of an image. For grayscale images, luminance is directly encoded in the pixel intensity values, where 0 represents pure black and 255 represents pure white.
Analyzing the mean luminance of an image is a foundational operation used extensively in image preprocessing, exposure correction, histogram equalization, and content-based image retrieval. This metric provides a single scalar value that characterizes the overall brightness level of an entire image.
Problem Definition: You are given a 2D matrix representing a grayscale image, where each cell contains an integer pixel intensity value. Your task is to compute the mean luminance (average pixel intensity) across all pixels in the image.
Mathematical Formulation: For an image with H rows and W columns, the mean luminance L̄ is calculated as:
$$\bar{L} = \frac{1}{H \times W} \sum_{i=0}^{H-1} \sum_{j=0}^{W-1} I(i, j)$$
where I(i, j) represents the intensity value at pixel position (i, j).
Your Task: Implement a function that computes the mean luminance of a grayscale image. The function must:
Input Validation Requirements:
pixel_matrix = [
[100, 200],
[50, 150]
]125.0The image is a 2×2 matrix containing four pixels with intensity values 100, 200, 50, and 150.
Mean luminance calculation: • Total sum = 100 + 200 + 50 + 150 = 500 • Number of pixels = 2 × 2 = 4 • Mean luminance = 500 / 4 = 125.0
The result 125.0, rounded to two decimal places, represents a mid-gray image (approximately 49% brightness on the 0-255 scale).
pixel_matrix = [
[128, 128, 128],
[128, 128, 128]
]128.0This is a 2×3 uniform gray image where every pixel has the same intensity value of 128.
Mean luminance calculation: • Total sum = 128 × 6 = 768 • Number of pixels = 2 × 3 = 6 • Mean luminance = 768 / 6 = 128.0
A uniform image always has a mean luminance equal to its constant pixel value. The value 128 represents exactly 50% gray on the intensity scale.
pixel_matrix = [
[100, 200],
[50, 150, 200]
]-1This input is invalid because the rows have inconsistent lengths: • Row 0 has 2 elements: [100, 200] • Row 1 has 3 elements: [50, 150, 200]
A valid image matrix must be rectangular, meaning all rows must have the same number of columns. Since this is a jagged array, the function returns -1 to indicate invalid input.
Constraints