← back

Calculate Image Brightness

#70 · Computer Vision · Easy

⊣ Solve on deep-ml.com

Problem

Calculate the brightness of an image. Given a 3D array representing an RGB image (height x width x 3), compute the average brightness across all pixels using the luminance formula.

Solution

1
2
3
4
5
6
7
8
import numpy as np

def calculate_brightness(image):
    image = np.array(image, dtype=float)
    # Luminance formula: 0.299*R + 0.587*G + 0.114*B
    luminance = 0.299 * image[:, :, 0] + 0.587 * image[:, :, 1] + 0.114 * image[:, :, 2]
    brightness = np.mean(luminance)
    return round(float(brightness), 4)

Explanation

  1. Convert the image to a float array for precise computation.
  2. Apply the luminance formula: L = 0.299R + 0.587G + 0.114B. These weights reflect human eye sensitivity to different colors.
  3. Compute the luminance for every pixel to get a 2D luminance map.
  4. Average all luminance values to get a single brightness score.
  5. The result is a scalar between 0 (black) and 255 (white) for standard 8-bit images.

Complexity

  • Time: O(H * W) where H is height and W is width
  • Space: O(H * W) for the luminance array