Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

239. Image normalization

easy
GeneralGeneral
senior

Image normalization is a simple but important preprocessing step in computer vision that makes pixel values comparable across images. In this problem, you’ll implement per-channel normalization for an RGB image represented as a NumPy array.

The normalization formula for each channel cc is:

yh,w,c=xh,w,c−μcσcy_{h,w,c} = \frac{x_{h,w,c} - \mu_c}{\sigma_c}

Requirements

Implement the function

python

Rules:

  • Normalize each channel using the given mean and std.
  • Treat mean and std as length-3 arrays corresponding to [R, G, B].
  • Use NumPy for computation.
  • Do not use any prebuilt normalization utilities (e.g., torchvision.transforms.Normalize).
  • Keep the original shape (H, W, 3).

Example

python

Output:

python
Input Signature
ArgumentType
stdnp.ndarray
meannp.ndarray
imagenp.ndarray
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • Use NumPy.

  • Keep output shape exactly (H, W, 3).

  • No prebuilt normalization utilities.

Hint 1

Use NumPy broadcasting: (H, W, 3) array minus (3,) array works automatically.

Hint 2

result = (image - mean) / std is all you need if shapes align correcty (last dim matches).

Hint 3

No loops are needed.

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
numpy-broadcasting
image-preprocessing
array-shapes
normalization
33 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit