v9.1.1
Menu

Image as Matrix

easyFREEWhat is Computer Vision?Image BasicsCV: Introduction to Computer Vision

Implement a function to compute basic statistics of a grayscale image represented as a 2D matrix of pixel values. In computer vision, images are often treated as 2D arrays where each element represents the intensity of a pixel, ranging from 0 (black) to 255 (white), and can be described using xx and yy coordinates.

The mean pixel value is a measure of the average intensity, calculated as the sum of all pixel values divided by the total number of pixels nn. The minimum and maximum pixel values represent the darkest and brightest points in the image. The standard deviation measures the spread of pixel values from the mean, calculated using the variance formula.

Here are the steps to compute these statistics:

  1. Flatten the 2D array into a 1D array of pixel values.
  2. Calculate the mean pixel value.
  3. Find the minimum and maximum pixel values.
  4. Compute the variance and standard deviation.
Mean=1n∑i=1nxi\text{Mean} = \frac{1}{n} \sum_{i=1}^{n} x_i σ=1n∑i=1n(xi−Mean)2\sigma = \sqrt{\frac{1}{n} \sum_{i=1}^{n} (x_i - \text{Mean})^2}

Use the population variance (divide by nn, not n−1n-1). Return a dictionary with keys 'mean', 'min', 'max' and 'std'; round the mean and standard deviation to 4 decimal places, and return min and max as the original integer pixel values.

This technique is widely used in image preprocessing and normalization.

Example

Input
image_stats([[100, 150], [200, 50]])
Output
{'mean': 125.0, 'min': 50, 'max': 200, 'std': 55.9017}
Reasoning
  • Input: image is a 2×22 \times 2 grid of grayscale intensities: top row 100,150100, 150, bottom row 200,50200, 50. All four pixels count equally, so n=4n = 4 and the row/column layout plays no part in the statistics.
  • Mean: 100+150+200+504=5004=125\frac{100 + 150 + 200 + 50}{4} = \frac{500}{4} = 125, reported as 125.0 because it is an average.
  • Min and max: the darkest pixel is 5050 (bottom right) and the brightest is 200200 (bottom left). These are actual pixel values, so they stay integers: 50 and 200.
  • Standard deviation: deviations from the mean are −25,25,75,−75-25, 25, 75, -75; their squares are 625,625,5625,5625625, 625, 5625, 5625, summing to 1250012500. Dividing by n=4n = 4 gives variance 31253125, and 3125≈55.90170\sqrt{3125} \approx 55.90170, rounded to 4 decimal places gives 55.9017.
  • Notice: dividing by n−1=3n - 1 = 3 instead would give ≈64.5497\approx 64.5497, a different answer. And a uniform image such as all-255255 has std 00, since every pixel sits exactly on the mean.

Constraints

  • Image is a 2D array with dimensions H×W where 1 ≤ H, W ≤ 100
  • Pixel values are integers in range [0, 255]
  • Return values rounded to 4 decimal places

Learn this in

More from CV: Introduction to Computer Vision

solution.py

Test results

Sign in to run your code against the test cases.