Normalize Image
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 I using the minimum Iminβ and maximum Imaxβ values in the image. Here are the steps to achieve this:
- Find the minimum and maximum pixel values in the image.
- Apply the normalization formula to each pixel value. The key formula for normalization is Inormβ=ImaxββIminβIβIminββ This technique is widely used in image processing pipelines.
Example:
normalize([[0, 128, 255]])
[[0.0, 0.502, 1.0]]
(128-0)/(255-0) β 0.502
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
More from CV: Introduction to Computer Vision
- Background Knowledge
In computer vision, a digital image can be thought of as a matrix (for grayscale) or a tensor (for color) of pixel values. Depending on how the image is stored, these pixel values might be in different ranges: for example, integers in [0,255] for 8-bit images, or possibly other numeric ranges if the image has been processed before. Many algorithms (especially in machine learning and deep learning) work best when inputs are scaled to a standard numeric range.
Normalization is a simple linear transformation that rescales values into a target range, often [0,1]. The formula given,
Inormβ=ImaxββIminβIβIminββ,takes the minimum pixel value in the image to 0 and the maximum to 1, and maps all other pixels proportionally in between. This is called min-max normalization and helps make different images comparable and numerically stable for downstream processing.
- Algorithm / General Approach
The general pattern for this type of problem is:
- Compute global statistics over the input array (here, the minimum and maximum pixel values).
- Apply a vectorized transformation to all elements using those statistics.
- Return the transformed array with the same shape as the input, but with values now guaranteed to lie in the desired range [0,1] (assuming a non-degenerate case where Imaxβξ =Iminβ).
This is typically implemented using array operations (e.g., with NumPy, PyTorch, etc.) rather than looping pixel by pixel.
- Step-by-Step Strategy
Assume the input is something like a 2D (grayscale) or 3D (color) array image:
- Compute min and max
- imin = image.min()
- imax = image.max()
- Handle edge case where all pixels are equal
- If imax == imin, then the image is constant. There is no range to scale.
- In this case, you might:
- Return an array of zeros (all pixels become 0), or
- Return the original image, depending on the problem specification.
- Apply normalization formula
- Compute the denominator: range_val = imax - imin
- Normalize:
norm_image = (image - imin) / range_val
- Ensure numeric type
- If the original was integer, ensure you cast to a floating type before division so you donβt get integer division.
- Return the normalized image
- Shape should be unchanged; only values are rescaled.
- Common Pitfalls
- Integer division: If image is an integer array and you do (image - imin) / (imax - imin) in a language that defaults to integer division, all values may become 0 or 1 incorrectly. Convert to float first.
- Zero denominator: When imax == imin (flat image), division by zero will occur unless you handle that case explicitly.
- Per-image vs. global normalization: The formula here is for normalizing per image. Donβt mistakenly use a fixed I_min and I_max from somewhere else unless the problem explicitly asks for it.
- In-place modification: If you modify the input array in place, it may affect other parts of the code that rely on the original image. Decide whether you should create a copy or safely work in place.
- Time & Space Complexity
-
Time complexity:
-
Computing min and max over all pixels: O(N), where N is the total number of pixels.
-
Applying the normalization to each pixel: O(N).
-
Overall: O(N).
-
Space complexity:
-
If you create a new normalized image array: O(N) extra space.
-
If you normalize in place (and allow changing the original), extra space is O(1) beyond the input storage.