v9.1.1
Menu

Normalize Image

easyFREEBook OverviewPreprocessingCV: Introduction to Computer Vision

Implement a function to normalize image pixel values to the range [0, 1]. Normalization is a crucial preprocessing step in Computer Vision that scales the intensity values of an image to a common range, which helps in reducing the impact of illumination changes and improving the robustness of subsequent processing tasks. The normalization process involves shifting and scaling the original pixel values II using the minimum IminI_{min} and maximum ImaxI_{max} values in the image. Here are the steps to achieve this:

  1. Find the minimum and maximum pixel values in the image.
  2. Apply the normalization formula to each pixel value. The key formula for normalization is Inorm=I−IminImax−IminI_{norm} = \frac{I - I_{min}}{I_{max} - I_{min}} This technique is widely used in image processing pipelines.

Example

Input
normalize([[0, 128, 255]])
Output
[[0.0, 0.502, 1.0]]
Reasoning
  • Input: image is a 2D grayscale image with one row of three pixels, 00, 128128 and 255255. These are 8-bit intensities, where 00 is black and 255255 is white.
  • Range: the darkest pixel gives Imin=0I_{min} = 0 and the brightest gives Imax=255I_{max} = 255, so the spread is Imax−Imin=255I_{max} - I_{min} = 255.
  • Math: each pixel is shifted down by IminI_{min} and then divided by the spread:
    • 0−0255=0.0\frac{0 - 0}{255} = 0.0
    • 128−0255≈0.50196\frac{128 - 0}{255} \approx 0.50196, which rounds to 0.5020.502 at 4 decimal places (the trailing zero is dropped)
    • 255−0255=1.0\frac{255 - 0}{255} = 1.0
  • Output: [[0.0, 0.502, 1.0]] has the same shape as the input, a 1×31 \times 3 grid, and now holds floats instead of integers.
  • Notice: the minimum pixel always maps to 0.00.0 and the maximum to 1.01.0, whatever the original range. If every pixel is equal the spread is 00, so the formula would divide by zero. That is why the constraints say to return all 0.00.0 in that case.

Constraints

  • Input is a 2D grayscale image
  • If all pixels are identical, return array of 0.0 values
  • 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.